Ở đây tôi nhận được tiêu đề tài nguyên http sau đó tôi phân tích cú pháp tiêu đề thành một mảng $ retVal. Tôi đã nhận mã cho phân tích các tiêu đề từ đây (http://www.bhootnath.in/blog/2010/10/parse-http-headers-in-php/) Bạn cũng có thể sử dụng http://php.net/manual/en/function.http-parse-headers.php nếu bạn có (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);
Nguồn
2010-11-22 08:42:15
Bản sao có thể có của [Làm cách nào để nhận URL đích bằng cách sử dụng cURL?] (http://stackoverflow.com/questions/1439040/how-can-i-get-the- url-url-using-curl) –