淘宝联盟开放平台使用PHP调用淘宝客API生成二合一淘口令教程

淘宝联盟开放平台使用PHP调用淘宝客API生成二合一淘口令教程

 开发筹备

  1.  创建淘宝开放平台账号,点我登录
  2.  创建应用,并获取相关API权限,如下图,申请填写狗一百个字即可获取。

     

     

  3.  获取SDK,选择版本,生成新的SDK,注:获取新的权限后需要更新SDK!
  4.  淘宝联盟账号,点我登录

  5. 淘宝联盟推广pid,格式为:mm_1xxx_2xxx_3xxx,(含义 1xxx:淘宝联盟账号ID ,2xxx:媒体ID,3xxx:推广位ID,后续代码会用到推广位ID)

     

 开发文档

  熟读开发文档,可以在线调试自己想要的API以及查询具体错误信息,文档链接

调用方式

  •  SDK调用
  •  API调用
  •  本文使用SDK调用,也会写出API调用的示例 ## SDK方式代码调用
  •  SDK安装 本文采用的框架为ThinkPHP6,SDK解压包复制到extend目录下即可。

SDK代码

<?php
/**
 * Created by PhpStorm.
 * User: 雪后西溏 <958808264@qq.com>
 * Date: 2021-03-01
 * Time: 17:19
 */

namespace app\facade;


use app\lib\exception\Error;

require_once '../extend/tbk/TopSdk.php';

class TbkTools
{
    protected static $appkey = '淘宝开放平台可以';
    protected static $secretKey = '淘宝开放平台密匙';
    protected static $adzone_id = '推广位id';

    /**
     * funcionName    查询商品优惠券以及生成二合一淘口令
     *
     * options.
     *
     * @param string $id 淘宝商品id
     *
     * @return mixed
     *
     * @throws Error
     * @author  雪后西溏 <958808264@qq.com>
     */
    public static function getCouponById($id = '')
    {
        $c = new \TopClient(self::$appkey, self::$secretKey);
        $rep = new \TbkItemInfoGetRequest();
        $rep->setNumIids($id);
        $good = $c->execute($rep);
        // 查询商品是否存在
        if (key_exists('results', $good)) {
            // 根据商品名称查询阿里妈妈推广物料
            $rep = new \TbkDgMaterialOptionalRequest();
            $rep->setQ($good['results']['n_tbk_item'][0]['title']);
            unset($good);
            $rep->setAdzoneId(self::$adzone_id);
            // 物料列表
            $list = $c->execute($rep);
            if (key_exists('result_list', $list)) {
                $info = [];
                // 物料查询出来的比较多,根据商品id,判断是否是用户想要的查询的商品
                foreach ($list['result_list']['map_data'] as $v) {
                    // 判断商品是否存在优惠券,coupon_share_url为我们的推广二合一链接
                    if ($v['item_id'] == $id) {
                        $info = $v;
                    }
                }
                if (empty($info)) {
                    throw new Error(['msg' => '没有查询到商品!']);
                }
                if (key_exists('coupon_share_url', $info)) {
                    // 生成淘口令
                    $rep = new \TbkTpwdCreateRequest();
                    $rep->setText('快来领取优惠券吧');
                    // 补全淘口令跳转链接
                    $rep->setUrl('https:' . $info['coupon_share_url']);
                    // 淘口令打开显示图
                    $rep->setLogo($info['pict_url']);
                    $res = $c->execute($rep);
                    // 对淘口令改造,IOS14以后¥LgK9cCt1wPS¥此类淘口令无法打开,需已数字开头,/结尾,或者完整的淘口令 {淘口令}{短链接}{商品名} , 如:"5.0¥KXo1cCuoTCt¥ https://m.tb.cn/h.4l40Cnq  猫咪绝育服春夏季薄款母猫绝育衣服手术服防舔断奶服猫咪衣服夏装"
                    $res['data']['password_simple'] = mt_rand(10, 99) / 10 . $res['data']['password_simple'] . '/';
                    $info['tkl'] = $res['data'];
                    $info['id'] = $id;

                    return $info;

                } else {
                    throw new Error(['msg' => '没有查询到优惠券']);
                }
            } else {
                throw new Error(['msg' => $list['sub_msg'], 'errcode' => $list['sub_code']]);
            }
        } else {
            throw new Error(['msg' => $good['sub_msg'], 'errcode' => $good['sub_code']]);
        }
    }
}

 

 

API代码

  • 签名函数
    function createSign($paramArr, $appSecret)
    {
        $sign = $appSecret;
        ksort($paramArr);
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $sign .= $key . $val;
            }
        }
        $sign .= $appSecret;
        $sign = strtoupper(md5($sign));
        
        return $sign;
    }

     

  • 组参函数
    function createStrParam($paramArr)
    {
        $strParam = '';
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $strParam .= $key . '=' . urlencode($val) . '&';
            }
        }
        
        return $strParam;
    }

     

  • 代码调用,简单示例
    <?php
    
    namespace app\api\controller\v1;
    
    use app\api\controller\Base;
    use app\lib\exception\Error;
    
    require_once '../extend/tbk/TopSdk.php';
    
    class Tbk extends Base
    {
        
        public function read($id)
        {
                    // 获取商品信息
            $good = $this->getData('taobao.tbk.item.info.get', ['num_iids' => $id]);
            
            return json_data($good);
        }
        
        
        protected function getData($method = '', $params = [])
        {
            $appKey = '淘客联盟key';
            $appSecret = '淘宝联盟密匙';
            $sessionkey = 'session';
            
            $paramArr = [
                'app_key'     => $appKey,
                'session_key' => $sessionkey,
                'method'      => $method,
                'format'      => 'json',
                'v'           => '2.0',
                'sign_method' => 'md5',
                'timestamp'   => time(),
            ];
            
            $paramArr = array_merge($paramArr, $params);
            
            //生成签名
            $sign = createSign($paramArr, $appSecret);
            //组织参数
            $strParam = createStrParam($paramArr);
            $strParam .= 'sign=' . $sign;
            
            
            //访问服务
            $url = 'http://gw.api.taobao.com/router/rest?' . $strParam;
            
            return json_decode(curl_get($url), true);
            
        }
        
        
    }        

     

 

posted @ 2021-02-25 14:30  雪后西溏  阅读(5)  评论(0)    收藏  举报