63128

Posting data to google apps script using php/curl

Question:

I want to submit some simple data from a php based database system to google apps engine.

The following code produces no error, but no result, too. I can't find out why google apps script does not react.

The google apps script is configured for use by anybody who knows the url (anonymous user is allowed). The script will be executed by my account, even if it is triggered bei anonymous user. The apps script has all the rights it needs to work.

The goal is that google apps script shows the posted data.

Any help will be very welcome. Maybe there is another method to transfer the data to google apps script. I could'nt find a solution after several hours of searching and trying.

On the PHP-System-side I've got the following code to SEND data:

// Get cURL resource $curl = curl_init(); // Set some options curl_setopt_array($curl, array( CURLOPT_URL => 'https://script.google.com/macros/s/AKfycbykWm97cwsxoP6NGhub8AqGuQammVwCwVgJrxjFLxY8TAFGRw/exec', CURLOPT_POST => 1, CURLOPT_POSTFIELDS => array( 'fname' => 'John', 'lname' => 'Miller' ) )); // Send the request, save response to $resp $resp = curl_exec($curl); // Show response echo $resp; // Close request to clear up some resources curl_close($curl); echo 'curl closed';

On the Google apps script-side I've got the following code to RECEIVE data:

// Show name in google apps function doPost(e) { var parameter var value for (var i in e.parameters) { parameter = i; wert = e.parameters[i]; if (parameter == "lname") {var lname = value;} if (parameter == "fname") {var fname = value;} } //Return return HtmlService.createHtmlOutput("The name is: " + fname + " " + lname) }

Answer1:

Do not use htmlService, use contentService and return a json or plain text. This is explained in more detail in the official docs if still doesnt aork make sure your php script can handle a redirect to googleusercontent.com. try it on the browser url using doGet first. You can also use get instead of post in your php.

Answer2:

There is no point having a for loop in your code. Even in the for loop, there still needs to be a check for a value in the parameter. Your for loop isn't working anyway because there is an error. You have an s on the end of parameter.

for (var i in e.parameters) {

It must be:

e.parameter

I tested some code, and this code works:

function doGet(e) { Logger.log('doGet ran. e: ' + e); Logger.log('e.parameter: ' + e.parameter); var lname = e.parameter.lname; var fname = e.parameter.fname; Logger.log('e.parameter name: ' + fname); Logger.log('e.parameter last: ' + lname); //Return return ContentService.createTextOutput("The name is: " + fname + " " + lname); }

You can test this in your browser without running the PHP. But it's kind of difficult to test. You have to do it just right. If you change the doPost() to a doGet() then you can test it by just putting a url into your browser.

You need to add the search string parameters to the end of the URL.

https://script.google.com/macros/s/YourAppsScriptID/exec?fname=Joe&lname=Wonky

Run the exec version of the code, not the dev version. But if you make any changes, you need to save a new version, and deploy the new version, or you test will be running the old deployed version.

There is no point in using a doPost. Just run a GET request and use doGet()

If there is no search value for a parameter, the value assigned will be undefined. Depending on what you are trying to do, you can check for that in the Apps Script, or in the PHP after getting a return. If you don't want or need a return, there's no point in running the content service.

If you are trying to display HTML content to the browser, you should have a link to a Stand Alone Apps Script, and not even use Curl.

Your Deployment setting for <strong>Who has access to the app:</strong> must be <strong>Anyone, Even Anonymous</strong> unless you want the code to only run if the user is signed into a Google account. Setting it to just <em>Anonymous</em> does not mean that it doesn't require the user to be logged in.

Answer3:

<strong>Next issue: Authorization / Access problem to apps script: Execute the app as "User accessing the web app" doesn't work.</strong>

Concerning the issue "doPost" (see above): After adding cacert.pem to php.ini it works fine with - cURL on the senders side (php) - doPost on GAS side

BUT it only works if the script in GAS is deployed with the following options: "Execute the app as:" is set to "Myself". "Who has access to the app" is set to "Anyone, Even Anonymous".

If I switch to "Execute the app as:" is set to "User accessing the web app". "Who has access to the app" is set to "Anyone".

then the following happens:

After sending the https-request via cURL to google apps script, a login-screen appears for user login to google. The tab in chrome is titled "Meet Google". It says "Sign in to continue to Google Drive". The whole login screen is in english language (I'm german). After that I'm entering login-information of my private google account and press "sign in"-button. After that a second login screen appears. The tab in chrome is titled "Google Drive". It says "Zur Nutzung von Google Drive anmelden" (translation: "please sign in to access google drive."). Then I'm typing in the same login information a second time and press the "anmelden"-button (translation: "sign in"). The next thing that happens is that the URL of the apps script appears in chrome. The tab is titled "Fehler" (translation: "Error"). In the window I can read "Skript-Funktion nicht gefunden: doGet" (translation: "Script function not found: doGet").

I cannot understand the behavour of google apps script. Especially I can't understand why google asks for "doGet", although there definitely is a "doPost" in the apps script.

Some help would be very welcome.

Answer4:

@Jörg,

I had same issue but it's fixed now.

The solution is to publish your script again with version # 2, make sure to select 'Anyone, even anonymous' and use the '/exec' URL instead of '/dev'

Recommend

  • How to update powerpivot pivot table filter via cell reference?
  • Argument 5: cannot convert from 'System.Drawing.Image' to 'string' - calling cla
  • VBScript to check for open process by user
  • Matching intervals between pandas dataframe
  • Print: Entry, “:CFBundleIdentifier”, Does Not Exist have tried most solutions
  • How do I configure Maven Cargo to use an embedded Tomcat server?
  • jquery full calendar
  • Does for loop open and close a database connection on each iteration?
  • UrlEncode non-string properties for HTTP Post through HttpClient
  • Unable to run SDL program in Eclipse but able to do so in Windows Explorer
  • QueryString id parameter not being used
  • Running jasmine tests for a component with NgZone dependency
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)
  • What does “t” refer to in this SQL?
  • Private IP address in reserved subnet range
  • C# fibonacci function returning errors
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Assign variable to the value in HTML
  • How to use carriage return with multiple line?
  • Yii2: Config params vs. const/define
  • Seeking advice on Jetty HttpClient Hang
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Spring security and special characters
  • Optimizing database types to compact database (SQLite)
  • TFS: Get latest causes slow project reloading
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Running a C# exe file
  • Perl system calls when running as another user using sudo
  • JSON with duplicate key names losing information when parsed
  • Symfony2: How to get request parameter
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • ORA-29908: missing primary invocation for ancillary operator
  • Google cloud sdk not working when python points python3
  • C# - Getting references of reference
  • using HTMLImports.whenReady not working in chrome
  • Reading document lines to the user (python)
  • Android Heatmap on canvas or ImageView