Redirecting with cURL?

I am trying to redirect using cURL. I can load the page in order, this is not a problem, but if I load, say google.com does not load images and the site does not work (obviously because it just prints HTML and does not actually redirect).

Is there any way to redirect using cURL? It seems like ...

header("Location: http://google.com");

... works?

Any help would be greatly appreciated.

+3
source share
4 answers

Well, because of my understanding, it seems like the OP wants to redirect the user to the URL of the search results. Using the GoogleAPI would be the first choice and achieve something similar, 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'm in luck" ....

+4

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

cURL

  curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);  
+3

I am afraid that it is impossible to force the client browser to send certain POST values ​​and is referenced, you can force it to go somewhere, therefore, header ().

Does this answer your question?

+1
source

This should work.pls: header ('Location: http://www.google.com '). Use (') instead of one cat of "(double)

0
source

Source: https://habr.com/ru/post/1771498/


All Articles