php cURL ,获取重定向 url 到一个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4062819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:50:48  来源:igfitidea点击:

cURL , get redirect url to a variable

phpcurlredirect

提问by Vamsi Krishna B

I'm using curl to fill a form. After completion of the post the other script which handles the form is redirecting to another URL. I want to get this redirect URL into a variable.

我正在使用 curl 填写表格。帖子完成后,处理表单的另一个脚本将重定向到另一个 URL。我想将此重定向 URL 放入一个变量中。

回答by EGL 2-101

Easy way to find the redirected url (if you don't want to know in advance)

查找重定向网址的简便方法(如果您不想提前知道)

$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

回答by RobertPitt

You would use

你会用

curl_setopt($CURL, CURLOPT_HEADER, TRUE);

And parse the headers for the locationheader

并解析标题的location标题

回答by nico limpika

Here I get the resource http headers then I parse the headers out into an array $retVal. I got the code for parsing the headers from here (http://www.bhootnath.in/blog/2010/10/parse-http-headers-in-php/) You could also use http://php.net/manual/en/function.http-parse-headers.phpif you have (PECL pecl_http >= 0.10.0)

在这里,我获取资源 http 标头,然后将标头解析为数组 $retVal。我从这里得到了解析标题的代码(http://www.bhootnath.in/blog/2010/10/parse-http-headers-in-php/)你也可以使用http://php.net/手册/en/function.http-parse-headers.php如果你有 (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("##代码##")', 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);

回答by Delta

You may want to set the CURLOPT_FOLLOWLOCATION to true.

您可能希望将 CURLOPT_FOLLOWLOCATION 设置为 true。

Or set the CURLOPT_HEADER to true and then use regexp to get the Location header.

或者将 CURLOPT_HEADER 设置为 true,然后使用 regexp 获取 Location 标头。