hyperf + 京东联盟sdk实现简单商品列表转链跳转购买商品功能示例
最近有研究一下京东联盟api,我看官网上有php的sdk,随便有找了一下swoole适配的sdk
还真有一个找到了就测试一下吧
https://github.com/yumufeng/jd-union-sdk
环境:
centos7
hyperf 2.2
环境都是现成的,这里就不多介绍,之前有发过教程;
京东联盟注册,这里也不多介绍了,自行网上搜索教程;
进入步骤:
compose安装jd-union-sdk
composer require yumufeng/jd-union-sdk
[[email protected] ~]# netstat -anp | grep 9501
tcp 0 0 0.0.0.0:9501 0.0.0.0:* LISTEN 18462/skeleton.Mast
tcp 0 0 192.168.1.98:9501 192.168.1.38:51146 TIME_WAIT -
tcp 0 0 192.168.1.98:9501 192.168.1.38:51138 TIME_WAIT -
[[email protected] ~]# kill -9 18462
[[email protected] ~]# cd /home/www/hyperf-skeleton/
[root@hyperf hyperf-skeleton]# composer require yumufeng/jd-union-sdk
Using version ^2.1 for yumufeng/jd-union-sdk
./composer.json has been updated
Running composer update yumufeng/jd-union-sdk
Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
- Locking yumufeng/curl-swoole (v1.0.4)
- Locking yumufeng/jd-union-sdk (v2.1.9)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
As there is no 'unzip' nor '7z' command installed zip files are being unpacked using the PHP zip extension.
This may cause invalid reports of corrupted archives. Besides, any UNIX permissions (e.g. executable) defined in the archives will be lost.
Installing 'unzip' or '7z' (21.01+) may remediate them.
- Downloading yumufeng/curl-swoole (v1.0.4)
- Downloading yumufeng/jd-union-sdk (v2.1.9)
- Installing yumufeng/curl-swoole (v1.0.4): Extracting archive
- Installing yumufeng/jd-union-sdk (v2.1.9): Extracting archive
Package swiftmailer/swiftmailer is abandoned, you should avoid using it. Use symfony/mailer instead.
Generating optimized autoload files
> rm -rf runtime/container
79 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
[[email protected] hyperf-skeleton]#
控制器调用
\hyperf\app\Controller\JdlmController.php
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\View\RenderInterface;
use Hyperf\Utils\ApplicationContext;
use Hyperf\DbConnection\Db;
use yumufeng;
use Hyperf\Guzzle\ClientFactory;
use Hyperf\HttpServer\Annotation\AutoController;
/**
* @AutoController()
*/
class JdlmController extends BaseController
{
/**
* @var \Hyperf\Guzzle\ClientFactory
*/
private $clientFactory;
public function __construct(ClientFactory $clientFactory)
{
$this->clientFactory = $clientFactory;
}
public function index(RenderInterface $render,ResponseInterface $response,RequestInterface $request)
{
//return 123;
$page = $this->request->input('page')??1;
$config = [
'appkey' => 'xxx', // AppId (京东联盟的appkey)
'appSecret' => 'xxx', // 密钥 (京东联盟的appSecret)
'unionId' => 'xxx', // 联盟ID (如果使用京东联盟的,填京东联盟的,使用京佣的填京佣的)
'positionId' => '', // 推广位ID (如果使用京东联盟的,填京东联盟的,使用京佣的填京佣的)
'siteId' => 'xxx', // 网站ID, (如果使用京东联盟的,填京东联盟的,使用京佣的填京佣的)
'apithId' => '', // 第三方网站Apith的appid (可选,不使用apith的,可以不用填写)
'apithKey' => '', // 第三方网站Apith的appSecret (可选,不使用apith的,可以不用填写)
'jyCode' => '', // 京东京佣的API授权调用code (可选,不使用京佣的,可以不用填写)
'isCurl' => true // 设置为true的话,强制使用php的curl,为false的话,在swoole cli环境下自动启用 http协程客户端
];
//https://github.com/yumufeng/jd-union-sdk
$client = new \JdMediaSdk\JdFatory($config);
//商品类目查询
//$result = $client->good->category();
//京粉精选商品查询接口 //jingfen($eliteId = 1, $pageIndex = 1, $pageSize = 50, $sortName = 'price', $sort = 'desc')
$result = $client->good->jingfen(1,$page,12);
if ($result == false ) {
var_dump($client->getError());
}
//获取推广商品信息接口
//$tgxx = $client->good->info(array_column($result['data'],'skuId'));
//获取通用推广链接 劵商品合一
//$tglj = $client->link->get($result['data'][1]['materialUrl'],['couponUrl'=>$result['data'][1]['couponInfo']['couponList'][0]['link']]);
foreach ($result['data'] as &$v) {
$v['tglj'] = $client->link->get($v['materialUrl'],['couponUrl'=>$v['couponInfo']['couponList'][0]['link']]);
}
//var_dump($result);
$tdk = [];
$tdk['title'] = '京东商品优惠券_京东联盟-技术博客集';
$tdk['keywords'] = '京东商品优惠券,京东联盟';
$tdk['description'] = '京东商品优惠券,京东联盟';
return $render->render('home/jdlm/index',
[
'tdk' => $tdk,
'cats' => $this->cats,
'session' => $this->session->get('user'),
'jdlmres' => $result,
'page' => $page
]
);
}
}
ps:
一些搜索啥的属于高级功能,要自己申请,这里就不搞了;
还有官方建议转链自己保存一段时间,我这是测试就不管那么多了
视图view
\hyperf\storage\view\home\jdlm\index.blade.php
@extends('layouts.homebase')
@section('tdk')
<title>{{$tdk['title']}}</title>
<meta name="keywords" content="{{$tdk['keywords']}}" />
<meta name="description" content="{{$tdk['description']}}" />
@endsection
@section('css')
@parent
@endsection
@section('content')
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="row">
@foreach($jdlmres['data'] as $v)
<div class="col-md-4">
<div class="thumbnail">
<a href="{{$v['tglj']['clickURL']}}">
<img alt="300x200" style="width: 228px" src="{{$v['imageInfo']['imageList'][0]['url']}}" />
<div class="caption">
<h3>{{$v['skuName']}}</h3>
<p>品牌:{{$v['brandName']}}</p>
<p>商店名称:{{$v['shopInfo']['shopName']}}</p>
<p style="text-decoration: line-through;">促销价:{{$v['priceInfo']['lowestPrice']}}</p>
<p style=" display: inline;float: right;font-size: 20px;color: red;border: 1px solid #e1d9d9;">优惠券:{{$v['couponInfo']['couponList'][0]['discount']}}</p>
<p>劵后价:{{$v['priceInfo']['lowestCouponPrice']}}</p>
<p><a class="btn btn-primary" href="#">购买</a></p>
</div>
</a>
</div>
</div>
@endforeach
</div>
<ul class="pagination">
<li><a href="" class="{{$page==1?'btn btn-large disabled':''}}"> < </a></li>
<li><a href="#">{{$page}}</a></li>
<li><a href="https://blog.zongscan.com/jdlm/index?page={{$page+1}}"> > </a></li>
</ul>
</div>
</div>
</div>
@endsection
@section('js')
@parent
@endsection
ps:
随便改改了,以方便出效果截图一下
效果截图,随便发一下测试地址
https://blog.zongscan.com/jdlm/index
相关文章