php curl 取最终地址的方式,可以做一个参考(前提是结果中有 header 头信息):
$content = curl_exec($ch);
$retVal = array();
$fields = explode("\r\n", @
preg_replace_callback('/\x0D\x0A[\x09\x20]+/', 'a', $content));
foreach( $fields as $field ) {
if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
$match[1] = @
preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', 'b', 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]);
}
}
}
if(!is_array($retVal)) {
return false;
}
return $retVal['location'];