3

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.

Jamie Redmond
  • 81
  • 2
  • 5
  • What's wrong with using header(Location: ...)? – Ash Burlaczenko Oct 26 '10 at 13:09
  • I'm sending post content and modifying the referrer based on certain conditions. Header() can't do either of those. – Jamie Redmond Oct 26 '10 at 13:18
  • Your question doesn't make sense - hence it is difficult to answer. cURL doesn't make redirects - it follows them (if told to). Do you need cURL to open another page when the first one is done? Maybe you need to explain better. – Repox Oct 26 '10 at 13:22
  • @Repox When telling cURL to go to http://google.com and to search for "firefox" I want the browser to actually go to the URL given by the cURL response. Currently cURL is returning the HTML. If I output the HTML it renders Google on MY domain. I want the browser to actually go to Google. As I said, similar to how header("Location: ...") works. Or a meta redirect. – Jamie Redmond Oct 26 '10 at 13:25
  • If I'm not mistaken, a Google search returns a lot of search results and what you wanna do is parse the HTML and go to the first result returned? – Repox Oct 26 '10 at 13:29

4 Answers4

4

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'....

Repox
  • 14,127
  • 6
  • 52
  • 74
  • On the right tracks yes :D Only issue is the referrer to the luckyUrl would need to be modified/blanked if the user chooses to. – Jamie Redmond Oct 26 '10 at 13:41
  • Well, it's not possible to 'opt out' the referrer if you move the client - you can't modify how the client works. – Repox Oct 26 '10 at 13:45
3

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);  
nerkn
  • 1,828
  • 1
  • 20
  • 33
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?

Thomas F.
  • 134
  • 3
  • Nope. I'm not using header(). And cURL can alter referrer, user-agent and other header values before a request is made. – Jamie Redmond Oct 26 '10 at 13:36
  • Indeed; I don't think what you want to do is possible, by the sounds of things. The closest you're going to get is to use GET variables instead of POST, e.g. `header('Location: http://www.google.com/search?q=firefox')`, – Matt Gibson Oct 26 '10 at 13:37
  • @Jamie Yes, cURL can do that, but cURL runs on your server, not the client's browser. It won't be able to redirect the client's browser to Google with different header values. I'm thinking that perhaps you'll want to run something client-side to do this -- perhaps some Javascript, as described in http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit that will POST from the client, based on results from the server? – Matt Gibson Oct 26 '10 at 13:41
0

It's should to work.pls try this: header( 'Location: http://www.google.com' ).Use the (')single cote instead of "(double)

riad
  • 6,726
  • 22
  • 54
  • 67