最近在更新淘宝客插件,了解了下京东的相关接口,本文教大家使用WordPress短代码实现京东推广自动转链的功能,有了它只需要填入推广链接即可自动转换为你的推广链接,方便快捷。有个缺点必须提前说明,此方法会请求京东联盟API,所以网页响应时间会延长一些,一般来讲速度都还可以接受,对网页打开速度有要求的就自己测试吧。
前面已经给大家分享过使用京东联盟API转链是如何实现的了,并且在很久之前就已经分享过wordpress短代码的使用,所以今天直接进入正题。
复制
//京东推广短代码 //https://www.daimadog.org/6955.html function dmd_jd_tg($array,$content) { $url=gettgurl($content); if(!$url){ $url=$content; } return $url; } add_shortcode('dmdtg', 'dmd_jd_tg'); //https://www.daimadog.org/6915.html //京东联盟api签名 function sign_method($method,$data=array(),$secretkey){ date_default_timezone_set("PRC"); $sign_method = array( 'app_key'=> '你的appkey', 'timestamp'=> date('Y-m-d H:i:s'), 'format' => 'json', 'method' => $method, 'v' => '1.0', 'sign_method'=>'md5', ); $sign_method = array_merge($sign_method,$data); ksort($sign_method); $data = $secretkey; foreach ($sign_method as $key => $value) { $data .= trim($key) .trim($value); } $data .= $secretkey; $sign_method['sign'] = strtoupper(md5($data)); return $sign_method; } //https://www.daimadog.org/6915.html //京东联盟api推广转链 function gettgurl($tgurl){ $url='https://router.jd.com/api'; $method='jd.union.open.promotion.common.get'; $secretkey='你的secretkey'; $param = array(); $data['promotionCodeReq'] = array('materialId'=>$tgurl,'siteId'=>'你的站点ID'); $param['param_json'] = json_encode($data); $GoodsList = sign_method($method,$param,$secretkey); $GoodsList = curl_post('https://router.jd.com/api', $GoodsList); $GoodsList = json_decode($GoodsList); $code=json_decode($GoodsList->jd_union_open_promotion_common_get_response->result)->code; if($code==200){ $tgurl=json_decode($GoodsList->jd_union_open_promotion_common_get_response->result)->data->clickURL; } return $tgurl; } // post请求 function curl_post($url, $curlPost) { $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $result = curl_exec($ch); curl_close($ch); return $result; }
在文章或页面中使用只需要添加如下短代码即可:
复制
[dmdtg]https://item.jd.com/100010572322.html[/dmdtg]
如果转链失败,链接将不会发生变化,所以在发布前请一定要自己预览一下,免得造成损失!
评论 (0)