CURL 获取不到重定向URL

使用浏览器可以看到

    location:http://www.amazon.com/dp/B018UQ5AMS?_encoding=UTF8&SubscriptionId=AKIAJWXT2MCY6ZQDW7VQ&camp=2025&creative
=386001&creativeASIN=B018UQ5AMS&linkCode=sp1&showDetailTechData=1&tag=ASSOCIATETAG#technical-data

而curl获取到的链接 却任然还是原来的链接
代码:

    $url_temp = "http://www.amazon.com/PurSteam-Steamer-Commercial-Fast-Heat-Aluminum/dp/tech-data/B00MG2OOH%3FSubscriptionId%3DAKIAJWXT2MCY6ZQDW7VQ%26tag%3DASSOCIATETAG%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00MG2OOHK";
    
    $url = get_redirect_url($url_temp);
    
    function get_redirect_url($url) {
        $redirect_url = false;
        $ch = curl_init($url);
        $referer = "http://www.amazon.com/";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $referer); //设置referer
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 1); //不返回请求体内容
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //允许请求的链接跳转
    
        $cookie_file = dirname(__FILE__) . '/includes/cookie.txt';
    
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);   // 设置从$cookie所指文件中读取cookie信息以发送
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);   // 设置将返回的cookie保存到$cookie所指文件
    
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Accept:*/*',
            'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0',
            'Connection: Keep-Alive'));
        $content = curl_exec($ch);
    
        if (!curl_errno($ch)) {
            $redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); //获取最终请求的url地址
        }
    
        return $redirect_url;
    }

图片描述

阅读 5k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进