2010-10-26 43 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