2010-10-26 144 views
3

我正在尝试使用cURL执行重定向。我可以加载页面的罚款,这不是一个问题,但如果我加载说google.com非图像加载和网站不工作(显然是因为它只是打印HTML,而不是实际上做重定向)。使用cURL重定向?

有没有办法使用cURL执行重定向?类似于...

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

...作品?

任何帮助将不胜感激。

+0

这有什么错用头(位置: ...)? – 2010-10-26 13:09:24

+0

我正在发送帖子内容并根据特定条件修改引用者。标题()不能做任何一个。 – 2010-10-26 13:18:43

+0

你的问题没有意义 - 因此很难回答。 cURL不会重定向 - 它遵循它们(如果被告知)。 当第一个完成时,您是否需要cURL打开另一个页面?也许你需要更好地解释。 – Repox 2010-10-26 13:22:19

回答

0

这应work.pls试试这个:标题(“位置:http://www.google.com”)。使用的(')单棚代替“(双)

+0

我没有说头()不工作。我说我想要一个类似于header()的cURL解决方案。 – 2010-10-26 13:19:07

+0

你能否详细说明为什么用单引号代替双引号而不用OP自己的尝试? – Repox 2010-10-26 13:20:04

+0

我会给你+1作为你的答案实际上解决了我的问题 – Seeker 2013-08-05 07:38:01

1

恐怕是不可能的强制客户端的浏览器发送某些POST值,且是指你只能强迫它去某处,因此头()。

这是否回答你的问题?

+0

没有。我没有使用header()。在提出请求之前,cURL可以更改引用链接,用户代理和其他标头值。 – 2010-10-26 13:36:30

+0

确实;我不认为你想做什么是可能的,通过事物的声音。最接近的是使用GET变量而不是POST,例如'header('Location:http://www.google.com/search?q=firefox')', – 2010-10-26 13:37:33

+0

@Jamie是的,cURL可以做到这一点,但cURL在您的服务器上运行,而不是客户端的浏览器。它将无法使用不同的标题值将客户端的浏览器重定向到Google。我想,也许你会想运行一些客户端来做到这一点 - 也许一些JavaScript,如http:// stackoverflow中所述。com/questions/133925/javascript-post-request-like-a-form-submit那将根据服务器的结果从客户端发布POST? – 2010-10-26 13:41:07

3

使用卷曲带-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); 
4

好了,从我的understading,好像OP希望的将用户重定向到搜索结果的URL。 使用GoogleAPI将是第一选择,并实现类似的东西,我会做到这一点:

<?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); 
?> 

上面的代码工作像“我觉得很幸运” ......

+0

在正确的轨道上是:D只有问题是指向luckyUrl的引用者,如果用户选择,则需要修改/消隐。 – 2010-10-26 13:41:44

+0

那么,如果您移动客户端,则无法“选择退出”引荐来源 - 您无法修改客户端的工作方式。 – Repox 2010-10-26 13:45:00