在laravel中以curl的方式请求京东联盟api获取数据流程步骤
在laravel框架中使用curl请求京东联盟api获取商品数据流程步骤示例,我这就以jd.union.open.goods.bigfield.query接口:商品详情查询接口,大字段信息为例
京东联盟官方文档
https://union.jd.com/searchResultDetail?articleId=108188
请求api:
https://api.jd.com/routerjson
api调试工具:
https://union.jd.com/openplatform/debug/v2?apiName=jd.union.open.goods.bigfield.query
必须项:
注册联盟账号,拿到appkey、secretkey
进程开发步骤:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Torann\GeoIP\Cache;
use Wanglelecc\Laracms\Models\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
class SelectOption extends Model
{
//京东联盟 api s
//京东联盟api签名
public static function sign_method($method,$data=array(),$secretkey){
date_default_timezone_set("PRC");
//sign_method2生成url的post数据
//360buy_param_json,access_token,app_key,method,timestamp,v 这几个参数从来生成sing签名
$sign_method = array(
'access_token'=> '',
'app_key'=> 'xx',
'method' => $method,
'timestamp'=> date('Y-m-d H:i:s'),
'v' => '1.0',
);
$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://api.jd.com/routerjson
?access_token=
&app_key=你的网站key
&method=jd.union.open.goods.bigfield.query
&v=1.0
&sign=8FBDBD255E66B9B5D62DB799FDB47BB1
&360buy_param_json=%7B%22goodsReq%22%3A%7B%22skuIds%22%3A%5B100010572322%5D%7D%7D
×tamp=2021-11-24+22%3A30%3A10.888%2B0800
360buy_param_json: {"goodsReq":{"skuIds":[100010572322]}}
Array
(
[access_token] =>
[app_key] => 你的网站key
[timestamp] => 2021-11-24 22:36:26
[format] => json
[method] => jd.union.open.order.query
[v] => 1.0
[sign_method] => md5
[360buy_param_json] => {"goodsReq":{"skuIds":[100010572322]}}
[sign] => 5052D6DCAFA2C4FD0E0B07455FD36FBE
)
*************************/
public static function Getbigfield($skuids){
$url='https://api.jd.com/routerjson';
$method='jd.union.open.goods.bigfield.query';
$secretkey='xx';
$param = array();
$data['goodsReq'] = array('skuIds'=>[$skuids],'fields' => ['imageInfo','detailImages']); //不能加单引号,或者双引号
$param['360buy_param_json'] = json_encode($data);
$res = self::sign_method($method,$param,$secretkey);
$res = self::curl_post($url, $res);
$res = json_decode($res,true);
$res = json_decode($res['jd_union_open_goods_bigfield_query_responce']['queryResult'],true);
return $res;
}
public static function curl_post($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
//京东联盟 api end
控制器中调用
use App\Models\SelectOption;
$skuid = 10075402996145;
$row['goodsinfo'] = SelectOption::Getbigfield($skuid)['data'][0];
效果:
相关文章