在 PHP 文件中将 cURL 与 Google Apps Script Web

我试图弄清楚为什么此代码不适用于 Google Apps Script Web 应用程序 URL,但它会与随机 Web API URL 一起使用?

第一个代码块从虚拟 API 获取数据。第二个代码块不会从 Google Apps 脚本网络应用 URL 中返回任何内容。代码中唯一的区别是 $url 变量值。

GAS 应用程序设置为 Web 应用程序,因此任何人,甚至匿名者都可以访问它(下面的屏幕截图)。如果我直接转到 GAS url GAS Output,它会返回 JSON。

Google Apps 脚本代码在下面的第三个代码块中。

有任何想法吗?

感谢 Tanaike 解决了问题!工作代码粘贴在问题的底部。

http://img4.mukewang.com/62da67f90001f88f05040522.jpg

第一个代码块


<?php

// Initiate curl session in a variable (resource)

$curl_handle = curl_init();


$url = "http://dummy.restapiexample.com/api/v1/employees";


// Set the curl URL option

curl_setopt($curl_handle, CURLOPT_URL, $url);


// This option will return data as a string instead of direct output

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);


// Execute curl & store data in a variable

$curl_data = curl_exec($curl_handle);


curl_close($curl_handle);


// Decode JSON into PHP array

$user_data = json_decode($curl_data);


// Print all data if needed

print_r($user_data);


?>

第二个代码块


<?php

// Initiate curl session in a variable (resource)

$curl_handle = curl_init();


$url = "https://script.google.com/macros/s/AKfycbyWSDzUyFa41fu_6QWP7h8ToklwWysGZsuSPaRnu649DmPNYG8/exec";


// Set the curl URL option

curl_setopt($curl_handle, CURLOPT_URL, $url);


// This option will return data as a string instead of direct output

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);


// Execute curl & store data in a variable

$curl_data = curl_exec($curl_handle);


curl_close($curl_handle);


// Decode JSON into PHP array

$user_data = json_decode($curl_data);


// Print all data if needed

print_r($user_data);


?>

第一个代码块:GAS 代码


function doGet(e) {

  var data = {

    status: 'success',

    dataSet: 'Some Info'


  };


  var output = JSON.stringify(data);

  return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON);

}

工作代码,谢谢Tanaike!


<?php

// Initiate curl session in a variable (resource)

$curl_handle = curl_init();


$url = "https://script.google.com/macros/s/AKfycbyWSDzUyFa41fu_6QWP7h8ToklwWysGZsuSPaRnu649DmPNYG8/exec";


梦里花落0921
浏览 148回答 2
2回答

桃花长相依

您想使用 php 访问 Web 应用程序。您想知道脚本运行时没有显示任何值的原因。如果我的理解是正确的,这个答案怎么样?请认为这只是几个可能的答案之一。您的问题原因:在您的 Web 应用程序中,Execute the app as:和Who has access to the app:分别设置为User accessing the web app和Anyone。在这种情况下,当您访问 Web 应用程序时,需要使用您的访问令牌。因此,在您的脚本中,会发生错误。而是curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true)被使用。这样,不显示任何值。删除时curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true),在您的脚本中,重定向页面将作为 HTML 数据检索。为了避免这个问题,下面的修改如何?模式一:在您的脚本中,似乎没有使用访问令牌。在这种情况下,请将Execute the app as:和分别设置Who has access to the app:为Me和。Anyone, even anonymous在这种情况下,不需要使用访问令牌。为此,请将以下脚本添加到您的脚本中以访问 Web 应用程序。curl_setopt($curl_handle,&nbsp;CURLOPT_FOLLOWLOCATION,&nbsp;true);通过上述流程,您可以使用 php 脚本访问 Web 应用程序并从 Web 应用程序中检索值。模式二:如果要访问分别以Execute the app as:和Who has access to the app:asUser accessing the web app和部署的 Web 应用程序Anyone,则需要使用访问令牌。为此,请将以下脚本添加到您的脚本中以访问 Web 应用程序。curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);$header = ['Authorization: Bearer ###your access token###'];curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header);笔记:在上述情况下,如果您作为 Web Apps 的所有者,在这种情况下让其他用户访问您的 Web Apps,请与用户共享部署 Web Apps 的 GAS 项目。这样,用户可以使用用户的访问令牌访问 Web 应用程序。笔记:当您在 Google Apps Script 端修改 Web Apps 的脚本时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。

缥缈止盈

我尝试在隐身窗口中访问 Apps 脚本 URL,它会重定向到 Google 登录提示。当您使用该选项的User accessing the web app设置Execute the app as时,毫无疑问,该脚本将以访问它的用户身份运行。因此,使用您当前的设置,用户需要登录到 Google 帐户才能成功执行应用程序。如果您将Execute the app as选项切换到,则会为该选项提供Me (<you address>)一个附加选项 ,。这是启用对应用程序的真正匿名访问的唯一方法。Anyone, even anonymousWho has access to this app有关更多详细信息,请参阅权限文档。
打开App,查看更多内容
随时随地看视频慕课网APP