嗨,我知道它在StackOverFlow上的一個非常常見的主題。 我已經花了整整一週的時間來搜索它。獲取curl中最後一個重定向的url php
我有一個網址:abc.com/default.asp?strSearch=19875379
這進一步重定向到該網址:abc.com/default.asp?catid={170D4F36-39F9-4C48-88EB- CFC8DDF1F531} & details_type = 1 &的itemid = {49F6A281-8735-4B74-A170-B6110AF6CC2D}
我已經作出了努力,得到使用捲曲在我的PHP代碼最終URL,但不能做到這一點。
這裏是我的代碼:
<?php
$name="19875379";
$url = "http://www.ikea.co.il/default.asp?strSearch=".$name;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
curl_close($ch);
// the returned headers
$headers = explode("\n",$a);
// if there is no redirection this will be the final url
$redir = $url;
// loop through the headers and check for a Location: str
$j = count($headers);
for($i = 0; $i < $j; $i++){
// if we find the Location header strip it and fill the redir var
//print_r($headers);
if(strpos($headers[$i],"Location:") !== false){
$redir = trim(str_replace("Location:","",$headers[$i]));
break;
}
}
// do whatever you want with the result
echo $redir;
?>
它給我的網址 「abc.com/default.asp?strSearch=19875379」,而不是這個網址「abc.com/default.asp?catid={170D4F36 -39F9-4C48-88EB-CFC8DDF1F531} & details_type = 1 &的itemid = {49F6A281-8735-4B74-A170-B6110AF6CC2D}」
預先感謝你的幫助:)
非常感謝閱讀。我已經按照您的建議進行了更改,並獲得了以下輸出: – Avtansh