1

I am trying to call my Google App from a PHP Script, this is the code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, "https://script.google.com/macros/s/.../exec");
$res = curl_exec($ch); // $res = true

This calls the URL and the "Loading" screen from Google App Script is shown and thats it, nothing else happens. Calling the URL from the browser works, so it must be related to curl. Maybe I have to set another curl option?

Thanks!

2 Answers 2

1

Here is an example for calling google apps script with a POST request

// set some post fields
$post = [
    'username' => 'user1',
    'password' => 'passuser1',
    'gender'   => 1,
];

$url = curl_init('https://script.google.com/macros/s/... your apps script url basically .../exec');
curl_setopt($url, CURLOPT_RETURNTRANSFER, true);
curl_setopt($url, CURLOPT_POSTFIELDS, $post);
curl_setopt($url, CURLOPT_FOLLOWLOCATION, true); //we must follow location -> google apps script makes a small redirect "for security reasons"

// execute!
$response = curl_exec($url);

// close the connection, release resources used
curl_close($url);

// do anything you want with your response
var_dump($response);
Sign up to request clarification or add additional context in comments.

Comments

0

This snippet below has worked wonders for me in the past:

$APPS_SCRIPT_URL = 'https://script.google.com/macros/s/.../exec';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $APPS_SCRIPT_URL); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$content = trim(curl_exec($ch));
curl_close($ch);          

Also, remember to properly authorize and publish your script for it to be executed by an anonymous user (your PHP script)

1 Comment

Is it possible to get the google script response back to php without getting 302 error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.