侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

hyperf + 京东联盟sdk实现简单商品列表转链跳转购买商品功能示例

swoole  /  管理员 发布于 2年前   460

最近有研究一下京东联盟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
[root@hyperf ~]# 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   -                   
[root@hyperf ~]# kill -9 18462
[root@hyperf ~]# 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!
[root@hyperf 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':''}}"> &lt; </a></li>
                    <li><a href="#">{{$page}}</a></li>
                    <li><a href="https://blog.zongscan.com/jdlm/index?page={{$page+1}}"> &gt; </a></li>
                </ul>
            </div>
        </div>
    </div>
@endsection
@section('js')
@parent
@endsection

ps:

随便改改了,以方便出效果截图一下


效果截图,随便发一下测试地址

https://blog.zongscan.com/jdlm/index

京东联盟api.png


  • 上一条:
    Laravel 10.1版本发布
    下一条:
    在go语言中实现字数限制器功能示例代码
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • swoole协程通信之数据处理协程对数据进行读写,保存到协程上下文示例(0个评论)
    • swoole redis连接池应用优化之多协程频繁访问redis(0个评论)
    • php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例(2个评论)
    • php中使用hyperf框架调用文心千帆大模型实现国内版chatgpt功能示例(0个评论)
    • 在hyperf框架中使用基于protobuf的RPC生成器实现rpc服务(1个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-09
    • 2020-03
    • 2020-06
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-11
    • 2022-03
    • 2022-05
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-07
    • 2023-08
    • 2023-11
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客