大家好,我是网创有方,今天教大家如何使用大淘客的api实现拼多多商品详情信息查询。这里用到的多多进宝,如果没有多多进宝的,先去多多进宝注册个账号吧!
第一步:进入大淘客官方创建应用,并且下载大淘客的sdk

第二步:将sdk导入到项目中
第三步:编写验签和查询代码,验签的目的是为了防止恶意注入,来保证请求的合法性。
代码中进行了2轮查询,为什么要这样操作呢?官方说goosId已经被废弃了,现在采用加密后的goodSign代替,但是goodSign获取方式官方又不说,后面经过网络查询得知可以在第一轮的json返回数据里获取。所以这里就进行了二次请求。把第一轮查询到的goodSign赋值给第二轮查询的参数。

- <?php
- include 'vendor/autoload.php';
-
-
- //拼多多商品详情
- $host = "https://openapi.dataoke.com/api/dels/pdd/goods/detail";
- //默认必传参数
-
- $data = [
-
- 'appKey' =>"你的appKey",
-
- 'version' => 'v2.0.0',
-
- 'goodsSign' =>'',
-
- 'searchId' =>'',
-
- 'goodsImgType'=>'',
-
- 'goodsId'=>'你要查询的商品名称'
-
- ];
- //加密的参数
-
- $data['sign'] = makeSign($data,"你的secretKey");
-
- //拼接请求地址
- $url = $host . '?' . http_build_query($data);
- //执行请求获取数据
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT,3000);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- $output = curl_exec($ch);
- $a = curl_error($ch);
- if(!empty($a)){
- return json_encode(array('code'=>10003, 'msg'=>$a));
- }
- // curl_close($ch);
- // return $output;
-
-
- echo($output);
- // 使用json_decode函数将JSON字符串解码为PHP对象
- $jsonObject = json_decode($output); //将第一次请求返回的json字符串转化为php对象
- // 提取name字段的值
- $goodsSign = $jsonObject->goodsSign; //提起goodsSign
-
- //开始第二次请求
-
- $data = [
-
- 'appKey' =>"你的appKey",
-
- 'version' => 'v2.0.0',
-
- 'goodsSign' =>$goodsSign,
-
- 'searchId' =>'',
-
- 'goodsImgType'=>'',
-
- 'goodsId'=>'你要查询的商品名称'
-
- ];
- //加密的参数
-
- $data['sign'] = makeSign($data,"你的secretKey");
-
- //拼接请求地址
- $url = $host . '?' . http_build_query($data);
- //执行请求获取数据
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT,3000);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- $output = curl_exec($ch);
- $a = curl_error($ch);
- if(!empty($a)){
- return json_encode(array('code'=>10003, 'msg'=>$a));
- }
-
-
- function makeSign($data, $appSecret)
- {
- ksort($data);
- $str = '';
- foreach ($data as $k => $v) {
-
- $str .= '&' . $k . '=' . $v;
- }
- $str = trim($str, '&');
- $sign = strtoupper(md5($str . '&key=' . $appSecret));
- return $sign;
- }
第四步:访问相关网页进行查询

第五步:对返回的json数据进行数据处理,这里就不说了,大家想怎么处理自己看着办吧