php cURL從重定向獲取url

2022-09-14 17:15:11 字數 3507 閱讀 6799

我目前正在使用curl嘗試從**刮刀的重定向獲取url.我只需要**上的**.我在過去幾天研究過stackoverflow和其他**,但都沒有成功.我目前使用的**來自這個**:

$url = "";

$ch = curl_init($url);

curl_setopt($ch, curlopt_useragent,'mozilla/5.0 (windows; u; windows nt 5.1; en-us; rv:1.8.1.1) gecko/20061204 firefox/2.0.0.1');

curl_setopt($ch, curlopt_returntransfer, true);

curl_setopt($ch, curlopt_header, true);

curl_setopt($ch, curlopt_followlocation, false);

curl_setopt($ch, curlopt_nobody, true);

$response = curl_exec($ch);

preg_match_all('/^location:(.*)$/mi', $response, $matches);

curl_close($ch);

echo !empty($matches[1]) ? trim($matches[1][0]) : 'no redirect found';

任何幫助將不勝感激!

最佳答案

在您的特定情況下,伺服器正在檢查某些使用者**字串.

當伺服器檢查使用者**字串時,只有當伺服器看到「有效」(根據伺服器)使用者**時,它才會響應302重定向狀態**.任何「無效」使用者**都不會收到302重定向狀態**響應或location:標頭.

在您的特定情況下,當伺服器收到來自「無效」使用者**的請求時,它會響應200 ok狀態**,而響應正文中沒有文字.

(注意:在下面的**中,提供的實際url已被示例替換.)

假設http:的伺服器檢查使用者**字串,並且http:/product/123/重定向到

在php中,您的解決方案是:

並且,此指令碼的輸出將是:http:

我目前正在使用curl嘗試從**刮刀的重定向獲取url.我只需要**上的**.我在過去幾天研究過stackoverflow和其他**,但都沒有成功.我目前使用的**來自這個**:

$url = "";

$ch = curl_init($url);

curl_setopt($ch, curlopt_useragent,'mozilla/5.0 (windows; u; windows nt 5.1; en-us; rv:1.8.1.1) gecko/20061204 firefox/2.0.0.1');

curl_setopt($ch, curlopt_returntransfer, true);

curl_setopt($ch, curlopt_header, true);

curl_setopt($ch, curlopt_followlocation, false);

curl_setopt($ch, curlopt_nobody, true);

$response = curl_exec($ch);

preg_match_all('/^location:(.*)$/mi', $response, $matches);

curl_close($ch);

echo !empty($matches[1]) ? trim($matches[1][0]) : 'no redirect found';

任何幫助將不勝感激!

最佳答案

在您的特定情況下,伺服器正在檢查某些使用者**字串.

當伺服器檢查使用者**字串時,只有當伺服器看到「有效」(根據伺服器)使用者**時,它才會響應302重定向狀態**.任何「無效」使用者**都不會收到302重定向狀態**響應或location:標頭.

在您的特定情況下,當伺服器收到來自「無效」使用者**的請求時,它會響應200 ok狀態**,而響應正文中沒有文字.

(注意:在下面的**中,提供的實際url已被示例替換.)

假設http:的伺服器檢查使用者**字串,並且http:/product/123/重定向到

在php中,您的解決方案是:

並且,此指令碼的輸出將是:http:

Nginx 重定向時獲取網域名稱

如果你在處理 nginx 重定向時要獲取原請求的網域名稱 比如 http 到 https 請用 host而不是 server name。從 http 的 sub2 子網域名稱跳轉到 https 的 sub1 子網域名稱 我用的 nginx 當初為了讓 http 請求跳轉到同名的 https 請求,配...

重定向介面獲取請求頭引數

1.重定向api測試需要先關閉postman的自動重定向功能 2.獲取請求頭返回資料 需要獲取的資料 獲取code值 檢視location是否存在 pm.test location is present function 獲取請求頭引數 var a postman.getresponseheader...

python 重定向獲取真實url的方法

樓主在做公司專案jdmkyqyqgf的時候遇到url重定向的問題,因此上網簡單查jdmkyqyqgf找,作出如下結果 由於使用的是語言是python所以以下是python的簡單解決方案 rs req程式設計客棧uests.get url,headers http headers,timeout 10...