开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 2911|回复: 22
收起左侧

[PHP相关教程] PHP源码 京东联盟API接口,转链+cha询订单,cha询推广位

[复制链接]
结帖率:100% (21/21)
发表于 2021-6-29 10:20:58 | 显示全部楼层 |阅读模式   湖北省武汉市
写在前面,本内容回复可见,分享不易,请给出免费的点赞和评分

1、class源码,注意替换AppKey和appsecret

JdClient.php
  1. <?php
  2. class JdClient{
  3.         public $AppKey = '你的AppKey';
  4.         public $AppSecret = '你的appsecret';
  5.         public $url = 'https://api.m.jd.com/api?functionId=unionOpenMngApiDemo&appid=unionpc&body=';
  6.         /* 成员变量 */
  7.           var $date;
  8.           var $Name;         
  9.           var $RequestUrl;         
  10.           var $HeadArray;         
  11.          
  12.         /* 成员函数 */
  13.         function SetDate($Array){
  14.                 $this->data = json_encode($Array);
  15.         }
  16.         function SetApiName($Name){
  17.                 $this->ApiName = $Name;
  18.         }
  19.         function SetRequestUrl($url){
  20.                 $this->RequestUrl = $url;
  21.         }
  22.         function SetHeaderArray($HeadArray){
  23.                 $this->HeadArray = $HeadArray;
  24.         }
  25.         function curl_get(){
  26.                 // 创建一个新cURL资源
  27.                 $ch = curl_init();
  28.                 // echo 'crul中调用的url:'.$this->RequestUrl;echo PHP_EOL;
  29.                 // 设置URL和相应的选项
  30.                 curl_setopt($ch, CURLOPT_URL, $this->RequestUrl);
  31.                 //设置头文件的信息作为数据流输出
  32.                 curl_setopt($ch, CURLOPT_HEADER, 0);
  33.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置成 1 的时候,页面是没有输出内容的
  34.                 curl_setopt($ch,CURLOPT_HTTPHEADER,$this->HeadArray);
  35.                 /**禁止证书验证防止curl输出空白**/
  36.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//禁止 cURL 验证对等证书
  37.                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//是否检测服务器的域名与证书上的是否一致
  38.                 // 抓取URL并把它传递给浏览器
  39.                 $res = curl_exec($ch);
  40.                
  41.                 // 关闭cURL资源,并且释放系统资源
  42.                 curl_close($ch);
  43.                 return $res;
  44.         }        
  45. }

  46. //工具商获取推广链接
  47. class jd_union_open_promotion_byunionid_get extends JdClient{
  48.         var $materialId;//推广物料url
  49.         var $unionId;
  50.         var $positionId;
  51.                
  52.         private $private = 'Private';
  53.         
  54.         function SetMaterialId($string){
  55.                 $this->materialId = urlencode($string);
  56.         }
  57.         function SetUnionId($string){
  58.                 $this->unionId = $string;
  59.         }
  60.         function SetPositionId($string){
  61.                 $this->positionId = $string;
  62.         }
  63.         function GetResult(){
  64.                 // echo 'GetResult中的materialId:'.$this->materialId;echo PHP_EOL;
  65.                 $Array['businessParam'] = array(
  66.                         "materialId"=>$this->materialId,
  67.                         "unionId"=>$this->unionId,
  68.                         "positionId"=>$this->positionId
  69.                         );
  70.                 $Array['apiName'] = 'jd.union.open.promotion.byunionid.get';
  71.                 $Array['appKey'] = $this->AppKey;
  72.                 $Array['appSecret'] = $this->AppSecret;
  73.                 $data = json_encode($Array,JSON_UNESCAPED_SLASHES);
  74.                 $request_url = $this->url.urlencode($data);
  75.                 $header_array = array("referer:https://union.jd.com/");               
  76.                 // $GoodsList = curl_get($request_url, $header_array);
  77.                 $a = new JdClient();
  78.                 $a->SetRequestUrl($request_url);
  79.                 $a->SetHeaderArray($header_array);

  80.                 $GoodsList = $a->curl_get();
  81.                 $GoodsList = json_decode($GoodsList);//json_decode() 将json格式的数据转换为对象,数组,转换为数组要加true
  82.                 $tgurl=json_decode(json_decode($GoodsList->data->result)->jd_union_open_promotion_byunionid_get_responce->getResult)->data->shortURL;

  83.                 return $tgurl;
  84.         }
  85. }

  86. //订单行查询接口
  87. class jd_union_open_order_row_query extends JdClient{
  88.         var $pageIndex;//推广物料url
  89.         var $pageSize;
  90.         var $type;
  91.         var $startTime;
  92.         var $endTime;
  93.         var $key;
  94.                
  95.         private $private = 'Private';
  96.         
  97.         function SetPageIndex($string){
  98.                 $this->pageIndex = urlencode($string);
  99.         }
  100.         function SetPageSize($string){
  101.                 $this->pageSize = $string;
  102.         }
  103.         function SetType($string){
  104.                 $this->type = $string;
  105.         }
  106.         function SetStartTime($string){
  107.                 $this->startTime = rawurlencode($string);
  108.         }
  109.         function SetEndTime($string){
  110.                 $this->endTime = rawurlencode($string);
  111.         }
  112.         function SetKey($string){
  113.                 $this->key = $string;
  114.         }
  115.         function GetResult(){
  116.                 // echo 'GetResult中的materialId:'.$this->materialId;echo PHP_EOL;
  117.                 $Array['businessParam'] = array(
  118.                         "pageIndex"=>$this->pageIndex,
  119.                         "pageSize"=>$this->pageSize,
  120.                         "type"=>$this->type,
  121.                         "startTime"=>$this->startTime,
  122.                         "endTime"=>$this->endTime,
  123.                         "key"=>$this->key
  124.                         );
  125.                 $Array['apiName'] = 'jd.union.open.order.row.query';
  126.                 $Array['appKey'] = $this->AppKey;
  127.                 $Array['appSecret'] = $this->AppSecret;
  128.                 $data = json_encode($Array,JSON_UNESCAPED_SLASHES);
  129.                 $request_url = $this->url.urlencode($data);
  130.                 $header_array = array("referer:https://union.jd.com/");               
  131.                 // $GoodsList = curl_get($request_url, $header_array);
  132.                 $a = new JdClient();
  133.                 $a->SetRequestUrl($request_url);
  134.                 $a->SetHeaderArray($header_array);

  135.                 $GoodsList = $a->curl_get();
  136.                 $GoodsList = json_decode($GoodsList);//json_decode() 将json格式的数据转换为对象,数组,转换为数组要加true
  137.                 $tgurl=json_decode(json_decode($GoodsList->data->result)->jd_union_open_order_row_query_responce->queryResult)->data;

  138.                 return $tgurl;//array的json
  139.         }
  140. }

  141. //查询推广位
  142. class jd_union_open_position_query extends JdClient{
  143.         var $unionId;//推广物料url
  144.         var $key;
  145.         var $unionType;//3:私域推广位,上限5000个,不在联盟后台展示,无对应 PID;4:联盟后台推广位,上限500个,会在推客联盟后台展示,可用于内容平台推广
  146.         var $pageIndex;
  147.         var $pageSize;
  148.                
  149.         private $private = 'Private';
  150.         
  151.         function SetUnionId($string){
  152.                 $this->unionId = $string;
  153.         }
  154.         function SetKey($string){
  155.                 $this->key = $string;
  156.         }
  157.         function SetUnionType($string){
  158.                 $this->unionType = $string;
  159.         }
  160.         function SetPageIndex($string){
  161.                 $this->pageIndex = $string;
  162.         }
  163.         function SetPageSize($string){
  164.                 $this->pageSize = $string;
  165.         }

  166.         function GetResult(){
  167.                 // echo 'GetResult中的materialId:'.$this->materialId;echo PHP_EOL;
  168.                 $Array['businessParam'] = array(
  169.                         "unionId"=>$this->unionId,
  170.                         "key"=>$this->key,
  171.                         "unionType"=>$this->unionType,
  172.                         "pageIndex"=>$this->pageIndex,
  173.                         "pageSize"=>$this->pageSize
  174.                         );
  175.                 $Array['apiName'] = 'jd.union.open.position.query';
  176.                 $Array['appKey'] = $this->AppKey;
  177.                 $Array['appSecret'] = $this->AppSecret;
  178.                 $data = json_encode($Array,JSON_UNESCAPED_SLASHES);
  179.                 $request_url = $this->url.urlencode($data);
  180.                 $header_array = array("referer:https://union.jd.com/");               
  181.                 // $GoodsList = curl_get($request_url, $header_array);
  182.                 $a = new JdClient();
  183.                 $a->SetRequestUrl($request_url);
  184.                 $a->SetHeaderArray($header_array);

  185.                 $GoodsList = $a->curl_get();
  186.                 $GoodsList = json_decode($GoodsList);//json_decode() 将json格式的数据转换为对象,数组,转换为数组要加true
  187.                 $tgurl=json_decode(json_decode($GoodsList->data->result)->jd_union_open_position_query_responce->queryResult)->data->result;

  188.                 return $tgurl;//array的json
  189.         }
  190. }
复制代码

2、转链demo
  1. <?php
  2. include('./class/JdClient.php');
  3. $c = new jd_union_open_promotion_byunionid_get();
  4. $c->SetMaterialId('https://u.jd.com/G85ARu9');
  5. $c->SetUnionId('你的联盟ID');
  6. $c->SetPositionId('推广位ID');
  7. $resp = $c->GetResult();
  8. echo PHP_EOL;
复制代码


3、订单查询demo
  1. <?php
  2. include('./class/JdClient.php');
  3. $c = new jd_union_open_order_row_query();
  4. $c->SetPageIndex('1');//页数
  5. $c->SetPageSize('500');//一页多少数量
  6. $c->SetType('1');//1下单时间
  7. $c->SetStartTime('2021-06-28 22:00:00');
  8. $c->SetEndTime('2021-06-28 23:00:00');
  9. $c->SetKey('<span style="color: rgb(96, 98, 102); font-family: -apple-system-font, " helvetica="" neue",="" sans-serif;="" background-color:="" rgb(245,="" 247,="" 250);"="">推客的授权key</span>');
  10. $resp = $c->GetResult();
  11. echo "<pre>";print_r($resp);echo "<pre>";
复制代码


4、查询推广位demo

  1. <?php
  2. include('./class/JdClient.php');
  3. $c = new jd_union_open_position_query();
  4. $c->SetUnionId('联盟ID');
  5. $c->SetKey('<span style="color: rgb(96, 98, 102); font-family: -apple-system-font, " helvetica="" neue",="" sans-serif;="" background-color:="" rgb(245,="" 247,="" 250);"="">推客的授权key</span>');
  6. $c->SetUnionType('4');
  7. $c->SetPageIndex('1');
  8. $c->SetPageSize('100');
  9. $resp = $c->GetResult();
  10. echo "<pre>";print_r($resp);echo "<pre>"
复制代码
交流QQ群:589231594

签到天数: 1 天

发表于 2023-11-16 11:18:05 | 显示全部楼层   广东省广州市
谢谢楼主分享
回复 支持 反对

使用道具 举报

发表于 2021-8-8 17:49:58 | 显示全部楼层   北京市北京市
666  感谢开源,想知道为什么我用了以后返回是空呢?
回复 支持 反对

使用道具 举报

结帖率:63% (5/8)

签到天数: 1 天

发表于 2021-7-5 02:22:23 | 显示全部楼层   河北省唐山市
谢谢楼主分享
回复 支持 反对

使用道具 举报

发表于 2021-7-4 23:27:28 | 显示全部楼层   广东省清远市
谢谢楼主分享
回复 支持 反对

使用道具 举报

结帖率:100% (3/3)

签到天数: 28 天

发表于 2021-7-4 19:20:44 | 显示全部楼层   广东省湛江市
回复 支持 反对

使用道具 举报

结帖率:33% (5/15)
发表于 2021-7-4 19:16:52 | 显示全部楼层   吉林省通化市
6666谢谢看看
回复 支持 反对

使用道具 举报

结帖率:92% (159/172)
发表于 2021-7-3 13:17:04 | 显示全部楼层   河北省张家口市
11111111111111111111111
回复 支持 反对

使用道具 举报

结帖率:100% (1/1)
发表于 2021-7-2 02:34:39 | 显示全部楼层   重庆市重庆市
感谢分享
回复 支持 反对

使用道具 举报

结帖率:50% (3/6)

签到天数: 15 天

发表于 2021-6-30 01:06:22 | 显示全部楼层   天津市天津市
感谢作者,支持支持
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表