回答
你會使用
curl_setopt($CURL, CURLOPT_HEADER, TRUE);
,並解析其標頭的location
頭
+1錯過了那一點:) – Sarfraz 2010-10-31 11:06:16
沒問題,使用post字段會發送數據,然後服務器會重定向,並且當數據輸出爲響應時,curl將選擇那個,那麼位置標題應該在那裏。 – RobertPitt 2010-10-31 11:07:57
您可能希望將CURLOPT_FOLLOWLOCATION設置爲true。
或者將CURLOPT_HEADER設置爲true,然後使用regexp獲取位置標題。
也注意到這個PHP安全模式,http://www.php.net/manual/en/function.curl-setopt.php#95027 – RobertPitt 2010-10-31 11:08:34
在這裏我得到資源http標題,然後我將標題解析到數組$ retVal中。我得到了代碼從這裏(http://www.bhootnath.in/blog/2010/10/parse-http-headers-in-php/)解析頭你也可以使用http://php.net/manual/en/function.http-parse-headers.php如果你有(PECL pecl_http> = 0.10.0)
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
// Getting binary data
$header = curl_exec($ch);
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach($fields as $field) {
if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
if(isset($retVal[$match[1]])) {
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
//here is the header info parsed out
echo '<pre>';
print_r($retVal);
echo '</pre>';
//here is the redirect
if (isset($retVal['Location'])){
echo $retVal['Location'];
} else {
//keep in mind that if it is a direct link to the image the location header will be missing
echo $_GET[$urlKey];
}
curl_close($ch);
簡單的方法來找到重定向的URL(如果你不這樣做想提前知道)
$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
它是'CURLINFO_REDIRECT_URL' – Sparky 2012-08-14 10:14:53
我在這裏找不到CURLINFO_REDIRECT_URL http:// www.php.net/manual/en/function.curl-getinfo.php – 2012-08-15 04:41:15
但我在這裏找到http://php.net/ChangeLog-5.php !!! CURLINFO_REDIRECT_URL被添加到5.3.7 - 但沒有記錄。但是從源頭上我假設這是(第一個)重定向url值,以防curl調用不使用自動重定向。所以我們知道下一個網址是什麼,以防萬一我們啓用了重定向。感謝@Sparky強迫我挖掘。 – 2012-08-15 04:59:53
- 1. curl被重定向後獲取url
- 2. 使用cURL獲取重定向URL
- 3. 獲取curl中最後一個重定向的url php
- 4. 獲取URL,一個給定的URL重定向到
- 5. 將所有變量的URL重定向到另一個URL
- 6. 獲取URL的另一個URL重定向到
- 7. 如何獲得Curl的重定向URL
- 8. 重定向URL GET變量
- 9. PHP重定向到URL變量
- 10. Codeigniter重定向到變量url
- 11. 將URL重定向到GET變量
- 12. 將STDERR重定向到一個變量
- 13. 重寫php獲取變量到另一個獲取變量
- 14. PHP獲取重定向URL
- 15. 獲取重定向URL
- 16. Android - 獲取url重定向
- 17. 獲取URL的一部分重定向
- 18. 從POST獲取變量並重定向
- 19. 如何從url獲取變量並使用php重定向301?
- 20. 獲取cURL跟蹤重定向
- 21. 發送帖子後要獲取重定向URL Curl請求
- 22. 重定向URL(重定向一個域到另一個域上採取相同)
- 23. 重定向到一個重寫的URL
- 24. htaccess重定向存在一個特定的URL變量
- 25. PHP重定向自定義變量URL
- 26. 重定向特定的URL變量
- 27. 重定向url獲取直接url
- 28. .htaccess的重定向到一個URL到另一個URL
- 29. php curl獲取一個url的鏈接
- 30. php curl重定向到另一個具有特定url格式的網站
可能重複[如何使用cURL獲取目標URL?](http://stackoverflow.com/questions/1439040/how-can-i-get-the- destination-url-using-curl) – 2016-05-26 17:00:22