简体   繁体   English

在 PHP 文件中将 cURL 与 Google Apps Script Web App 一起使用时遇到问题

[英]Trouble using cURL with Google Apps Script Web App in a PHP file

I'm trying to figure out why this code won't work with a Google Apps Script web app URL, but it will work with a random web API URL?我想弄清楚为什么此代码不适用于 Google Apps Script Web 应用程序 URL,但它适用于随机 Web API URL?

The 1st code block gets data from the dummy API.第一个代码块从虚拟 API 获取数据。 The second code block returns nothing from the Google Apps Script web app URL.第二个代码块不会从 Google Apps 脚本网络应用程序 URL 返回任何内容。 The only difference in the code is the $url variable value.代码中唯一的区别是 $url 变量值。

The GAS App is set to Web App so anyone, even anonymous can access it (screenshot below). GAS 应用程序设置为 Web 应用程序,因此任何人,甚至匿名者都可以访问它(下面的屏幕截图)。 If I go directly to the GAS url GAS Output , it returns the JSON.如果我直接转到 GAS url GAS Output ,它会返回 JSON。

The Google Apps Script code is below in third code block. Google Apps 脚本代码在下面的第三个代码块中。

Any ideas?有任何想法吗?

Problem solved thanks to Tanaike!!多亏了田池,问题解决了!! Working code pasted at the bottom of the question.工作代码粘贴在问题的底部。

在此处输入图片说明

First Code Block第一个代码块

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

?>

Second Code Block第二个代码块

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

?>

First Code Block: GAS Code第一个代码块:GAS 代码

function doGet(e) {
  var data = {
    status: 'success',
    dataSet: 'Some Info'

  };

  var output = JSON.stringify(data);
  return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON);
}

Working Code, thank you 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);

?>
  • You want to access to Web Apps using php.您想使用 php 访问 Web 应用程序。
  • You want to know the reason that no values are shown when the script is run.您想知道脚本运行时未显示任何值的原因。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

Reason of your issue:您的问题原因:

In your Web Apps, Execute the app as: and Who has access to the app: are set as User accessing the web app and Anyone , respectively.在您的 Web 应用程序中, Execute the app as:Who has access to the app:分别设置为User accessing the web appAnyone In this case, when you access to Web Apps, it is required to use your access token.在这种情况下,当您访问 Web Apps 时,需要使用您的访问令牌。 So in your script, an error occurs.所以在你的脚本中,发生了错误。 But curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true) is used.但是使用curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true) By this, no values are shown.由此,不显示任何值。 When curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true) is removed, in your script, the redirect page is retrieved as HTML data.curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true)被删除时,在您的脚本中,重定向页面将作为 HTML 数据进行检索。

In order to avoid this issue, how about the following modifications?为了避免这个问题,下面的修改怎么样?

Pattern 1:模式一:

In your script, it seems that the access token is not used.在您的脚本中,似乎未使用访问令牌。 In this case, please set Execute the app as: and Who has access to the app: as Me and Anyone, even anonymous , respectively.在这种情况下,请分别设置Execute the app as:Who has access to the app: as MeAnyone, even anonymous In this case, it is not required to use the access token.在这种情况下,不需要使用访问令牌。

For this, please add the following script to your script for accessing to Web Apps.为此,请将以下脚本添加到您的用于访问 Web 应用程序的脚本中。

curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);

By above flow, you can access to Web Apps using php script and retrieve the values from Web Apps.通过上述流程,您可以使用 php 脚本访问 Web Apps 并从 Web Apps 检索值。

Pattern 2:模式2:

If you want to access to Web Apps which deployed with Execute the app as: and Who has access to the app: as User accessing the web app and Anyone , respectively, it is required to use the access token.如果您想访问使用Execute the app as:Who has access to the app: as User accessing the web appAnyone分别部署User accessing the web app ,则需要使用访问令牌。

For this, please add the following script to your script for accessing to Web Apps.为此,请将以下脚本添加到您的用于访问 Web 应用程序的脚本中。

curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
$header = ['Authorization: Bearer ###your access token###'];
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header);

Note:笔记:

  • Under above condition, if you, who is the owner of Web Apps, makes other users access to your Web Apps with this condition, please share the GAS project that Web Apps was deployed with the users.在上述情况下,如果您作为 Web Apps 的所有者,在这种情况下让其他用户访问您的 Web Apps,请与用户共享 Web Apps 部署的 GAS 项目。 By this, the users can access to Web Apps with the user's access token.通过这种方式,用户可以使用用户的访问令牌访问 Web 应用程序。

Note:笔记:

  • When you modified the script of Web Apps at Google Apps Script side, please redeploy Web Apps as new version.当您在 Google Apps Script 端修改 Web Apps 的脚本时,请将 Web Apps 重新部署为新版本。 By this, the latest script is reflected to Web Apps.这样,最新的脚本就会反映到 Web Apps 中。 Please be careful this.请注意这一点。

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

I tried accessing the Apps Script URL in an incognito window and it redirects to a Google login prompt.我尝试在隐身窗口中访问 Apps 脚本 URL,它重定向到 Google 登录提示。

When you use the User accessing the web app setting for the Execute the app as option, the script will, not surprisingly, run as the identity of the user accessing it.当您将User accessing the web app设置用于将Execute the app as选项Execute the app as ,该脚本将以访问它的用户的身份运行,这并不奇怪。 Therefore, with your current settings, a user needs to be logged in to a Google account to successfully execute the application .因此,根据您当前的设置,用户需要登录 Google 帐户才能成功执行应用程序

If you switch the Execute the app as option to Me (<you address>) , that makes an additional option, Anyone, even anonymous , available for the Who has access to this app option.如果您将Execute the app as选项切换为Me (<you address>) ,则会提供一个附加选项, Anyone, even anonymous ,可用于Who has access to this app选项。 That's the only way to enable truly anonymous access to the application.这是启用对应用程序的真正匿名访问的唯一方法。

See the permissions documentation for more details.有关更多详细信息,请参阅权限文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM