【问题标题】:Trouble using cURL with Google Apps Script Web App in a PHP file在 PHP 文件中将 cURL 与 Google Apps Script Web App 一起使用时遇到问题
【发布时间】:2020-01-17 03:57:40
【问题描述】:

我试图弄清楚为什么此代码不适用于 Google Apps Script 网络应用 URL,但它可以用于随机 Web API URL?

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

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

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

有什么想法吗?

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

第一个代码块

<?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";



// Set the curl URL option
curl_setopt($curl_handle, CURLOPT_URL, $url);

// was an update from my stack overflow question.
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); //https://stackoverflow.com/questions/59780986/trouble-using-curl-with-google-apps-script-web-app-in-a-php-file/59781600#59781600

// 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 rest api curl google-apps-script


    【解决方案1】:
    • 您想使用 php 访问 Web 应用程序。
    • 您想知道脚本运行时没有显示值的原因。

    如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

    问题原因:

    在您的 Web 应用程序中,Execute the app as:Who has access to the app: 分别设置为 User accessing the web appAnyone。在这种情况下,当您访问 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: 分别设置为MeAnyone, even anonymous。在这种情况下,不需要使用访问令牌。

    为此,请将以下脚本添加到您的脚本中以访问 Web 应用程序。

    curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
    

    通过上述流程,您可以使用 php 脚本访问 Web Apps 并从 Web Apps 中检索值。

    模式 2:

    如果你想访问以Execute the app as:Who has access to the app:分别为User accessing the web appAnyone部署的Web Apps,则需要使用访问令牌。

    为此,请将以下脚本添加到您的脚本中以访问 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 应用程序中。请注意这一点。

    参考资料:

    如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

    【讨论】:

    • 田池,谢谢!!!你的解释很有帮助,解决了我的问题。对于落后的任何人,我需要添加curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); 代码行才能使其工作。 Tanaike,您能解释一下该行对脚本的作用吗?
    • @Mr. B 谢谢你的回复。我很高兴你的问题得到了解决。它访问 Web 应用程序,URL 被重定向。为此,使用CURLOPT_FOLLOWLOCATION。当不使用CURLOPT_FOLLOWLOCATION 时,重定向页面被检索为 HTML 数据。
    【解决方案2】:

    我尝试在隐身窗口中访问 Apps 脚本 URL,它会重定向到 Google 登录提示。

    当您将User accessing the web app 设置用于Execute the app as 选项时,毫无疑问,该脚本将以访问它的用户身份运行。因此,使用您当前的设置,用户需要登录到 Google 帐户才能成功执行应用程序

    如果您将Execute the app as 选项切换为Me (&lt;you address&gt;),则会为Who has access to this app 选项提供一个附加选项Anyone, even anonymous。这是启用对应用程序的真正匿名访问的唯一方法。

    更多详情请见the permissions documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 2012-07-21
      • 2013-03-20
      相关资源
      最近更新 更多