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)
Related
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
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 know that using cURL i can see the destination URL, pointing cURL to URL having CURLOPT_FOLLOWLOCATION = true.
Example :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "www.example1.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch); //Some information on the fetch
curl_close($ch);
$info will have the url of the final destination which can be www.example2.com.
I hope my above understanding is correct. Please let me know if not!.
My main question is, what all type of redirection cURL will be able to know?
Apache redirect, javascript redirects, form submition redirects, meta-refresh redirects!?
update
Thanks for your answeres #ceejayoz and #Josso. So is there a way by which I can follow all the redirect programatically through php?
cURL will not follow JS or meta tag redirects.
I know this answer is a little late, but I ran into a similar issue and needed more than just following the HTTP 301/302 status redirects. So I wrote a small library that will also follow rel=canonical and og:url meta tags.
https://github.com/mattwright/URLResolver.php
I found meta refresh tags to not provide much benefit, but they are used if no head or body html tag is returned.
As far as I know, it only follows HTTP Header redirects. (301 and 302).
curl is a multi-protocol library, which provides just a little HTTP support but not much more that will help in your case. You could manually scan for the meta refresh tag as workaround.
But a better idea was to check out PEAR HTTP_Request or the Zend_Http class, which more likely already provide something like this. Also phpQuery might be relevant, as it comes with its own http functions, but could easily ->find("meta[refresh]") if there's a need. Or look for a Mechanize-like browser class: Is there a PHP equivalent of Perl's WWW::Mechanize?
I just found this on the php site. It parses the response to find redirects and follows them. I don't think it gets every type of redirect, but it's pretty close
http://www.php.net/manual/en/ref.curl.php#93163
I'd copy it here but I don't want to plagiarize