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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组