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)
}
Next issue: Authorization / Access problem to apps script: Execute the app as "User accessing the web app" doesn't work.
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.
@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'
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.
There is no point having a
for
loop in your code. Even in thefor
loop, there still needs to be a check for a value in the parameter. Yourfor
loop isn't working anyway because there is an error. You have ans
on the end of parameter.It must be:
I tested some code, and this code works:
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 adoGet()
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.
Run the
exec
version of the code, not thedev
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 usedoGet()
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 Who has access to the app: must be Anyone, Even Anonymous unless you want the code to only run if the user is signed into a Google account. Setting it to just Anonymous does not mean that it doesn't require the user to be logged in.