京东联盟开发(6)——推广链接解析SKUID
1、从推广方案中分析出价格及推广码
$keyword = " 【京东】长虹(CHANGHONG) L3 老人手机 移动/联通2G 老年机 双卡双待 咖啡 原价:168.00元 券后价:163.00元 购买链接:https://union-click.jd.com/jdc?d=s7inaP --------------------------- 【领券下单,更便宜】关注 东购 公众号,获取更多优惠! "; $express_before = "/原价:(\d+.\d+)\s*元/i"; $express_after = "/券后价:(\d+.\d+) 元/i"; $express_url = "/jdc\?d=([a-zA-Z0-9]+)/i"; preg_match($express_before, $keyword, $matches_before); preg_match($express_after, $keyword, $matches_after); preg_match($express_url, $keyword, $matches_url); var_dump($matches_before); var_dump($matches_after); var_dump($matches_url);
2、302跳转取新的地址,并分析出skuid
$url= "https://union-click.jd.com/jdc?d=".$matches_url[1]; $skuid = url2skuid($url); var_dump($skuid); // 推广短链接转skuid,并检查是否有券的产品 https://union-click.jd.com/jdc?d=A3Cp11 function url2skuid($url) { $result1 = http_request($url, false); $express1 = "/hrl='(.+?)'/i"; preg_match($express1, $result1, $matches1); if (!isset($matches1[1])){ return array('errcode'=> 1, 'msg'=>"获取跳转1失败"); } $jumpurl1 = $matches1[1]; // var_dump($jumpurl1); $result2 = http_request($jumpurl1, true); // var_dump($result2); if (!isset($result2["redirect_url"])){ return array('errcode'=> 2, 'msg'=>"获取跳转2失败"); } $jumpurl2 = $result2["redirect_url"]; // var_dump($jumpurl2); $express2 = "/\.jd\.com\/[a-z\.\/\?\=]{0,}(\d+)(\.html|\&)/i"; // $express2 = "/jingfen\.jd\.com\/item\.html?sku=(\d+)&/i"; preg_match($express2, $jumpurl2, $matches2); // var_dump($matches2); if (!isset($matches2[1])){ return array('errcode'=> 3, 'msg'=>"获取skuid失败"); }else{ if (!stristr($jumpurl2,"jingfen")){ return array('errcode'=> 4, 'msg'=>$matches2[1]." 无券"); }else{ return array('errcode'=> 0, 'msg'=>$matches2[1]); } } } function http_request($url, $info = False) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $output = curl_exec($ch); if ($info){ $info = curl_getinfo($ch); // $retURL = $info['url']; curl_close($ch); return $info; }else{ curl_close($ch); return $output; } }
本文来自博客园,作者:方倍工作室,转载请注明原文链接:https://www.cnblogs.com/txw1958/p/jd_url2skuid.html