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

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

swoole  /  管理员 发布于 3星期前   39

最近有研究一下京东联盟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)
    • 相关文章
    • 在hyperf框架中使用基于protobuf的RPC生成器实现rpc服务(0个评论)
    • hyperf + 京东联盟sdk实现简单商品列表转链跳转购买商品功能示例(0个评论)
    • Hyperf 3.0版本发布(0个评论)
    • hyperf2.2框架中添加Cache代理类的流程步骤及使用案例(0个评论)
    • hyperf2.1框架使用Dockerfile部署流程步骤(0个评论)
    • 近期文章
    • mysql5.7中实现分区表及分区where in查询示例及分区分表对比浅析(0个评论)
    • nginx + vue配置实现同域名下不同路径访问不同项目(0个评论)
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 在go语言中使用FFmpeg库实现PCM音频文件编码为mp3格式文件流程步骤(0个评论)
    • gopacket免安装Pcap实现驱动层流量抓包流程步骤(0个评论)
    • 在laravel项目中实现密码强度验证功能推荐扩展包:password-strength(0个评论)
    • 在go语言中用filepath.Match()函数以通配符模式匹配字符串示例(0个评论)
    • Laravel Response Classes 响应类使用优化浅析(0个评论)
    • mysql中sql_mode的各模式浅析(0个评论)
    • 百度文心一言今天发布,个人第一批内测体验记录,不好别打我(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 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
    Top

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

    侯体宗的博客