Skip to content

Instantly share code, notes, and snippets.

@pzzrudlf
Last active October 9, 2018 12:42
Show Gist options
  • Save pzzrudlf/46e27e632419ed61cd17c120f913e38d to your computer and use it in GitHub Desktop.
Save pzzrudlf/46e27e632419ed61cd17c120f913e38d to your computer and use it in GitHub Desktop.
<?php
//淘客长(短)网址提取商品id
//unescape 转码
function unescape($str) {
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i ++)
{
if ($str[$i] == '%' && $str[$i + 1] == 'u')
{
$val = hexdec(substr($str, $i + 2, 4));
if ($val < 0x7f)
$ret .= chr($val);
else
if ($val < 0x800)
$ret .= chr(0xc0 | ($val >> 6)) .
chr(0x80 | ($val & 0x3f));
else
$ret .= chr(0xe0 | ($val >> 12)) .
chr(0x80 | (($val >> 6) & 0x3f)) .
chr(0x80 | ($val & 0x3f));
$i += 5;
} else
if ($str[$i] == '%')
{
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
} else
$ret .= $str[$i];
}
return $ret;
}
//使用curl模拟请求返回得到真实url
function getUrl($interger, $clickurl){
if ($interger == 0){//淘宝客短连接
$headers = get_headers($clickurl, TRUE);
// var_dump($headers);echo "<hr>";
$tu = $headers['Location'];
$eturl = unescape($tu[1]); //获取完整连接
$u = parse_url($eturl); //得到et的连接
$param = $u['query'];
$ref = str_replace('tu=', '', $param);
$curl_ref = $tu[1];
}
if ($interger == 1) {//淘宝客长连接
$headers = get_headers($clickurl, TRUE);
// var_dump($headers);echo "<hr>";
$tu = $headers['Location'];
$eturl = unescape($tu); //获取完整连接
$u = parse_url($eturl); //得到et的连接
$param = $u['query'];
$ref = str_replace('tu=', '', $param);
$curl_ref = $tu;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ref);
curl_setopt($ch, CURLOPT_REFERER, $curl_ref);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_MAXREDIRS,2);
$out = curl_exec($ch);
$dd = curl_getinfo($ch);
curl_close($ch);
$item_url = $dd['url'];
return $item_url;
}
function getGoodId($url)
{
$arrary = null;
if (strpos($url, '?')) {
$string = getUrl(1, $url);
} else {
$string = getUrl(0, $url);
}
$array = explode("&", $string);
$array = explode("=", $array[0]);
return $array[1];
}
$shortUrl = "http://s.click.taobao.com/0s9wtSx";
$longUrl = "http://s.click.taobao.com/t?e=m%3D2%26s%3DiXAoCv04KwocQipKwQzePOeEDrYVVa64K7Vc7tFgwiG3bLqV5UHdqfQLyIrTqO%2FOjGYPrSmetxHJBeWGuImap%2BHrJgCSSM0oEDl2nELui%2F4%2BoqMzwamC58XPpYxuh%2B0%2FTGHAbzT0LqZfbSSjgeLIle%2BPFjHRYhaAasI%2By6E4HjaV8iXdx%2FoeKkhg2urGScCXsEYHIm3z3%2Fc55yxjpJ9KBMYOae24fhW0&pvid=19_219.72.200.154_32080_1474362268684";
$goodId1 = getGoodId($shortUrl);
$goodId2 = getGoodId($longUrl);
echo "<br><br>";
print_r($goodId1);
echo "<br><br>";
print_r($goodId2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment