开发者

I have a url in client side javascript and I need to resolve it to the destination url. How can I do this?

Is there a way to do this? I have no idea how I would do it and it is critical to my application that I be able to do this.

As an example I need to resolve: http://feedproxy.google.com/~r/Techcrunch/~3/eX9UmYpqsw0/ to http://techcrunch.com/2011/02/15/apple-ecosystem/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+Techcrunch+(TechCrunch)开发者_开发百科&utm_content=FeedBurner


There are various ways...I used this in my handy dandy url LENGTHENING app {shameless plug}: http://stretch.thecreativeshift.net/

function _longer_get_url($url, $javascript_loop = 0, $timeout = 5 ) {
  $url = str_replace( "&", "&", urldecode(trim($url)) );

  $cookie = tempnam ("/tmp", "CURLCOOKIE");
  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
  curl_setopt( $ch, CURLOPT_URL, $url );
  curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
  curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  curl_setopt( $ch, CURLOPT_ENCODING, "" );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
  curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
  curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
  curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
  $content = curl_exec( $ch );
  $response = curl_getinfo( $ch );
  curl_close ( $ch );

  if ($response['http_code'] == 301 || $response['http_code'] == 302) {
    ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");

    if ( $headers = get_headers($response['url']) ) {
      foreach( $headers as $value ) {
        if ( substr( strtolower($value), 0, 9 ) == "location:" ) {
          return _longer_get_url( trim( substr( $value, 9, strlen($value) ) ) );        
        }
      }
    }
  }

  if ((preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value)) && $javascript_loop < 5) {
    return _longer_get_url( $value[1], $javascript_loop+1 );
  }
  else {
    return $response['url'];
  }
}

:) I think I found it in the comments section of the PHP cURL man pages but can't find it now

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜