Php 如何在重定向时查找结束url

Php 如何在重定向时查找结束url,php,redirect,file-get-contents,Php,Redirect,File Get Contents,可能重复: 我正在使用文件获取内容('http://someurl.com/1893849“)获取url的内容,但url会被重定向。例如,/1893849以/某个url路径结束。除了获取文件内容外,还有什么方法可以知道结束路径吗? 如果你需要知道redirec的地址 curl_setopt($init, CURLOPT_HEADER, 1); curl_setopt($init, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($init, CURLOPT_HT

可能重复:

我正在使用
文件获取内容('http://someurl.com/1893849“)
获取url的内容,但url会被重定向。例如,
/1893849
/某个url路径
结束。除了获取文件内容外,还有什么方法可以知道结束路径吗?

如果你需要知道redirec的地址

curl_setopt($init, CURLOPT_HEADER, 1);
curl_setopt($init, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($init, CURLOPT_HTTPHEADER, array('Expect:') );


可能的重新发布:不通过
文件获取内容
。查看使用
cURL
库,特别是使用
cURL\u setopt()
-
CURLOPT\u FOLLOWLOCATION
。查找临时移动的
或永久移动的
或找到的
302的英文字符串可能不是一个好主意。检查输出的第一行是否有状态代码(并且只有状态代码)将更加健壮。
curl_setopt($init, CURLOPT_HEADER, 1);
curl_setopt($init, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($init, CURLOPT_HTTPHEADER, array('Expect:') );
if ( strstr($content, 'Moved Temporarily') or  
strstr($content, 'Moved Permanently') or strstr($content, '302 Found')   ) :

if ( preg_match('@Location: (.*?)\n@', $content, $red) ) :

print_r($red);

endif;