tags:

views:

46

answers:

4

Hi,

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.

A: 

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

riad
I didn't say that header() was not working. I said I wanted a cURL solution similar to header().
Jamie Redmond
Could you elaborate why using single quotes instead of double quotes should work instead of OP's own try?
Repox
+1  A: 

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.
Nope. I'm not using header(). And cURL can alter referrer, user-agent and other header values before a request is made.
Jamie Redmond
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
@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
+1  A: 

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
+2  A: 

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&amp;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
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
Well, it's not possible to 'opt out' the referrer if you move the client - you can't modify how the client works.
Repox