I would like to ask for your opinion or ideas if you encountered something like this scenario.
1.) User will send a curl request to http://mycurl_url.com/get_request
2.) get_request function is access through REST API.
3.) get_request function validates any request then redirects them to external site like www.external_site.com/receive_redirect_response_value
for coding here are some sample
Client requesting curl:
$curl = curl_init('http://mycurl_url.com/get_request');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
... // the REST API KEY is submitted here
curl_exec($curl);
Then on the mycurl_url.com/get_request here's the code
$has_access = has_accessed($_GET['key']);
if ($has_access) {
header('Location: www.external_site.com/receive_redirect_response_value?value=this_is_the_value');
}
return false;
Now the idea here is that the user will do a curl request and if he's allowed then he will be redirected to an external site "PROPERLY". Now I quoted the word "PROPERLY" since based on what I experienced it's redirecting as expected but the problem is upon redirection the "External Site" has no capability of storing any session. It seems like its been in a stateless form just like the first request when we do a CURL RESTFUL API Request.
Can anyone give me an idea on how to achieve a redirection that does not inherit the "Stateless" form upon redirection to the external site?
Hope I was able to make my question clearly. Thanks again
Related
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?
The 1st code block gets data from the dummy API. The second code block returns nothing from the Google Apps Script web app URL. The only difference in the code is the $url variable value.
The GAS App is set to Web App so anyone, even anonymous can access it (screenshot below). If I go directly to the GAS url GAS Output, it returns the JSON.
The Google Apps Script code is below in third code block.
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
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.
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. In this case, when you access to Web Apps, it is required to use your access token. So in your script, an error occurs. But curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true) is used. 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.
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. 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.
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.
Pattern 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.
For this, please add the following script to your script for accessing to Web Apps.
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. By this, the users can access to Web Apps with the user's access token.
Note:
When you modified the script of Web Apps at Google Apps Script side, please redeploy Web Apps as new version. By this, the latest script is reflected to Web Apps. Please be careful this.
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script
cURL Functions
How to do simple google script api with php
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.
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. Therefore, with your current settings, a user needs to be logged in to a Google account to successfully execute the application.
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. That's the only way to enable truly anonymous access to the application.
See the permissions documentation for more details.
I am using the following code to send a POST request using cURL. This is working perfectly. The only issue I have is that the location in the address bar does not update itself.
The webpage that sends this request is www.somedomain.com/merge.php, but after the post has been executed the address bar still shows www.somedomain.com/merge.php instead of www.somedomain.com/preview.php
I have tried using curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); with no luck.
$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'www.somedomain.com/preview.php');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, 'landscape=true');
curl_exec ($c);
curl_close ($c);
Thank you
You are misunderstanding what's going on. There are three parties involved:
client (browser) <----> your server <----> somedomain.com
The client sends an HTTP request to your server, your server sends an HTTP request to somedomain.com. Your server will receive the response, the client has nothing to do with it. If you want to redirect the client, you need to issue an appropriate HTTP response to the client from your server telling it to redirect elsewhere. Because the client is talking to your server, not somedomain.com. Whatever is going on between your server and somedomain.com is none of its business.
If you want the client to directly send a POST request to somedomain.com, you need to create a form that POSTs to somedomain.com or trigger something equivalent using Javascript.
You cannot use cURL to do this as the REQUEST did not initiate within the browser, rather it came from PHP.
If you want the browser to update the URL then you should look at posting a FORM from within the current page.
When you make a request using curl, you are programmatically accessing the url and this happens only in the server side.
This will not change the url in the address bar.
If you want to redirect to the new url then use
header('redirect', 'http://newurl.com');
This is impossible.
You use cURL and make request from server. But you can change URL on client.
You can use
Header("Location:http://www.somedomain.com/preview.php")
after curl_close($c);
But I think this is not correct solution.
It is not possible.
This is the same question at PHP Post & Redirect with cURL Same As HTML Form
Although that question was closed, you can read the valid answer to help you clarify the problem.
How can i redirect to a different page along with passing some POST parameters using PHP ?
You cannot "redirect" with the POST method per se, what you're after is to execute a POST request to the site you were planning to redirect to. Have a look at the cURL POST example from http://php.net/manual/de/book.curl.php:
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '#/home/user/test.png');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
Substitute the CURLOPT_URL with your target and set the required fields in the $data array. For this to work your PHP needs to have the cURL module enabled.
Alternatively you could store all the data you plan to send in the POST in your session and just have the target read from there.
you need to store the POST parameters in SESSION variable
How can i redirect to a different page along with passing some POST parameters using PHP?
That is difficult to do, because redirecting needs both, the server and the browser.
Your script can tell the browser that it should redirect.
But the browser, according to the specs, must get confirmation to allow to send the POST request to the redirected URL.
But even so, not all browsers will re-send the post data with the redirect:
Note: When automatically redirecting a POST request after
receiving a 301 status code, some existing HTTP/1.0 user agents
will erroneously change it into a GET request. (Ref)
So as this does not consistently work and I guess you don't want to have the user to press a button to perform the redirect, you can't create easily what you're looking for with just a redirect.
However as Kashif Khan wrote, you can store the submitted post data into some session and then redirect the user to a new location in which you could read again from that session.
To have this working in the browser nicely, use the 303 See Other status code for the redirect.
I need to make a POST in JSON format to an HTTPS web page in a remote server and receive an answer in JSON format.
The data to be send it to the remote server is take it from the URL (bar)<---Done in PHP
My problem is to send this data and receive an answer.
I tried making it in PHP, and HTML using cURL(php) and submit(html).
The results: In PHP I can't send anything.
In HTML I can submit the data, get an answer but I can't catch in my code.
I see the answer using Wireshark, and as I see the POST is make it after a negotiation protocol, and as I said I receive an answer(encoded due to HTTPS, I think).
Now I need receive that answer in my code to generate an URL link so I'm considering to use Java Script.
I never do something similar before.
Any suggestion will be appreciated, thanks.
I'm using the following code with not result but a 20 seconds of delay until a blank page.
<?php
$url = 'https://www.google.com/loc/json';
$body = '{"version":"1.1.0","cell_towers":[{"cell_id":"48","location_area_code":1158,"mobile_country_code":752,"mobile_network_code.":7,"age":0,"signal_strength":-71,"timing_advance":2255}]}';
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $body);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($c, CURLOPT_HTTPHEADERS,'Content-Type: application/json');
$page = curl_exec($c);
echo($page);
//print_r($page);
curl_close($c);
?>
New info
I Just get new very important info
"The Gears Terms of Service prohibits direct use of the Google location server (http://www.google.com/loc/json) via HTTP requests. This service may only be accessed through the Geolocation API."
So, I was going trough the wrong way, and from now I will start to learn about Gears in order to apply the Gears API.
Cheers!
There's no real reason PHP couldn't do the PHP for you, if you set things up properly.
For instance, it may require a cookie that it had set on the client browser at some point, which your PHP/curl request doesn't have.
To do proper debugging, use HTTPFox or Firebug in Firefox, which monitor the requests from within the browser itself, and can show the actual data, not the encrypted garbage that wireshark would capture.
Of course, you could use the client browser as a sort of proxy for your server. Browser posts to the HTTPS server, gets a response, then sends that response to your server. But if that data is "important" and shouldn't be exposed, then the client-side solution is a bad one.
I'm trying to perform a redirect using cURL. I can load the page fine, that's not a problem, but if I load say google.com non of the images load and the site does not work (obviously because its just printing the HTML and not actually doing a redirect).
Is there any way to perform a redirect using cURL? Sort of similar to how ...
header("Location: http://google.com");
... works?
Any help would be much appreciated.
Well, from my understading, it seems like OP want's to redirect the user to the search results URL.
Using the GoogleAPI would be a first choice and to achieve something like that, I would do this:
<?php
$query = "firefox";
$apiUrl = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".urlencode($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $apiUrl);
$content = curl_exec($ch);
$content = json_decode($content);
$luckyUrl = $content->responseData->results[0]->unescapedUrl;
header("Location: ".$luckyUrl);
?>
The code above works like 'I feel lucky'....
Use curl with -L
-L/--location
(HTTP/HTTPS) If the server reports that the requested page has
moved to a different location (indicated with a Location: header
and a 3XX response code), this option will make curl redo the
request on the new place. If used together with -i/--include or
-I/--head, headers from all requested pages will be shown. When
authentication is used, curl only sends its credentials to the
initial host. If a redirect takes curl to a different host, it
won't be able to intercept the user+password. See also --loca‐
tion-trusted on how to change this. You can limit the amount of
redirects to follow by using the --max-redirs option.
When curl follows a redirect and the request is not a plain GET
(for example POST or PUT), it will do the following request with
a GET if the HTTP response was 301, 302, or 303. If the response
code was any other 3xx code, curl will re-send the following
request using the same unmodified method.
So when using cURL
add
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
I'm afraid it is impossible to force the client's browser to send certain POST values and refers, you can only force it to go somewhere, hence header().
Does this answer your question?
It's should to work.pls try this: header( 'Location: http://www.google.com' ).Use the (')single cote instead of "(double)