用PHP对获得淘宝客的真实地址和内容

利用PHP CURL获取淘宝客链接的真实地址和内容,其实淘宝那边只是做了几次跳转,外加判断了一次refer,这样的话,普通的curl或者一些其他函数就不能得到最终的结果了。



/**
 * 获取淘宝客的真实地址和内容
 *
 * @author cnxzcxy
 * @version $$Id$$
 **/
function cnxzcxy_curl($url, $method = '', $post = '', $refer = '', $returnHeaderInfo = false)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt ($curl, CURLOPT_TIMEOUT, 60);//设置超时时间,单位秒
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    if (!empty($refer)) {
        curl_setopt($curl, CURLOPT_REFERER, $refer);
    }
    if ($method == 'post') {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    }
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $str = curl_exec($curl);
    $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    $location = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
    curl_close($curl);
    unset($curl);
    if(!$str) 
    { 
        return false; 
    }
    //返回头信息
    if ($returnHeaderInfo) {
        return array($httpCode, $location, $str);
    }
    return $str;
}
 
//原始淘宝客链接
$originUrl = 'http://s.click.taobao.com/t_js?tu=http%3A%2F%2Fs.click.taobao.com%2Ft%3Fe%3DzGU34CA7K%252BPkqB07S4%252FK0CFcRfH0GoT805sipKj0%252BcUz4Afr3YQbK2yBIu%252Bt3BkAH5cLTBOByB2M4rUcLIrxtemFUSwzUAVjoEKxT8H8bD6Uww%253D%253D%26ref%3D%26et%3DjFBC6ejEHSDLAg%253D%253D';
$url = explode('tu=', $originUrl);
$url = urldecode($url[1]);
 
//利用curl模拟请求
$content = cnxzcxy_curl($url, 'get', '', $originUrl, true);
print_r($content);
 
/*
返回结果
array(
  [0]=>200,//http code
  [1]=>http://detail.tmall.com/item.htm?id=19610539887&ali_trackid=2:mm_14363892_0_0:1366182113_4k3_1370801342&tbpm=3,//最终url
  [2]=>... //html内容在此)
*/


你可能感兴趣的:(php)