dsegw3424 2015-07-31 09:24
浏览 59
已采纳

PHP cURL从t.co获取最后一个重定向URL

I got this function to detect a page's redirect final URL

So, if I call the function with google.com it will detect the redirect and bring back http://www.google.co.ve or http://www.google.co.ar, http://www.google.co.il, etc...

This tells me that this is detecting the final URL properly... now when I try with a shortener service like t.co and I use this link as an example... t.co/0BWUquGyj3 it will return t.co/0BWUquGyj3 again, instead of detecting the return URL

function parse_url($url, $user_agent = 'desktop') {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_FILETIME, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_MAXREDIRS, 45);    
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 45);    
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($curl, CURLOPT_TIMEOUT, 120);
    curl_setopt($curl, CURLOPT_ENCODING, 'UTF-8');
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");
    $curl_exec = curl_exec($curl);
    $header = curl_getinfo($curl);
    $final_url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
    curl_close($curl);

    return $final_url;
}
#http://t.co/0BWUquGyj3
$cosas = parse_html("t.co/0BWUquGyj3");
echo $cosas; //returns t.co/0BWUquGyj3 instead of the final redirect URL
  • 写回答

1条回答 默认 最新

  • dongpu5600 2015-07-31 09:43
    关注
    function unshorten_url($url) {
      $ch = curl_init($url);
      curl_setopt_array($ch, array(
        CURLOPT_FOLLOWLOCATION => TRUE,  // the magic sauce
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_SSL_VERIFYHOST => FALSE, // suppress certain SSL errors
        CURLOPT_SSL_VERIFYPEER => FALSE, 
      ));
      curl_exec($ch); 
      return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    }
    

    Output:

    echo unshorten_url("t.co/0BWUquGyj3"); // https://twitter.com/zaynmalik/status/626878040472223744/photo/1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度