Wechat Mini Program Alliance is the official wechat mini program promotion background,

Mini program Alliance is a CPS promotion tool provided by wechat official for mini program merchants and tweeters (promoters). It has the characteristics of “deal first and pay later”, merchants in the management background to release product promotion demand and commission, commission will be settled after the successful completion of the promotion.

/** * wechat Applet alliance *@author blog.alipay168.cn
 * Class UnionXcx
 * @package wbs\xcx\union
 *
 * @noticeNote The Runtime requires write permission to store logs and temporary data, and can also be customized */
class UnionXcx
{
    / * * *@varString Extension bit */
    private $pid = ' ';

    /** * Interface base *@var string
     */
    private $uri_base = 'https://api.weixin.qq.com';

    /** * Log in to the alliance's Twitter user management background, click [Account Setting] in the left navigation bar, * to obtain the Twitter account appID and AppSecret (AppSecret requires the super administrator of the account to scan the wechat code); * appid *@var string
     */
    private $appid = ' ';

    /** * Log in to the alliance's Twitter user management background, click [Account Setting] in the left navigation bar, * to obtain the Twitter account appID and AppSecret (AppSecret requires the super administrator of the account to scan the wechat code); Small appSecret * *@var string
     */
    private $secret = ' ';

    /** * Temporary directory for storing data and logs *@var string
     */
    public $dir_runtime = ' ';

    public function __construct($config)
    {
        $this->appid = $config['appid'];
        $this->secret = $config['secret'];
        $this->pid = $config['pid'];
        $this->dir_runtime = !empty($config['runtime'])?$config['runtime'] : __DIR__ . '/.. /runtime/';
        if(! is_dir($this->dir_runtime)) {
            @mkdir($this->dir_runtime, 0644.true); }}/** * Initiate a POST request *@param $url
     * @param $data array|string
     * @param array $headers
     * @return array|bool|float|int|mixed|\stdClass|string|null
     */
    protected function curl_post($url.$data.$headers = [])
    {
        if (!empty($headers)) {
            $headers = array_merge($headers['content-type: application/json']);
        } else {
            $headers = ['content-type: application/json'];
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);// Disable for testing
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);// Disable for testing
        //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        //curl_setopt($curl, CURLOPT_REFERER, $url);
        if (!empty($headers)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        $response = curl_exec($curl);
        $error = curl_error($curl);
        return $error ? $error : $response;
    }

    /** * Initiate a POST request *@param $url
     * @param array $data array
     * @param array $headers
     * @return array|bool|float|int|mixed|\stdClass|string|null
     */
    protected function curl_get($url.$data= [].$headers = [])
    {
        if (!empty($data)) {
            if (stripos($url.'? ') = = =false) {
                $url = $url . '? ' . http_build_query($data);
            } else {
                $url = $url . '&' . http_build_query($data); }}if (!empty($headers)) {
            $headers = array_merge($headers['content-type: application/json']);
        } else {
            $headers = ['content-type: application/json'];
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 0);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($headers)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        $response = curl_exec($curl);
        $error = curl_error($curl);
        return $error ? $error : $response;
    }

    /** * Get access_token *@return string|array
     */
    public function get_access_token()
    {
        $tokenFile = $this->dir_runtime . 'data/access_token';
        if (file_exists($tokenFile)) {
            $info = @json_decode(file_get_contents($tokenFile), true);
            // Return directly
            if (!empty($info['expire'&&])$info['expire'] > time()) {
                return $info['token']; }}$url = $this->uri_base . '/cgi-bin/token? grant_type=client_credential&appid=' . $this->appid . '&secret=' . $this->secret;


        $res = $this->curl_get($url.' ');
        $ret = json_decode($res.true);

        if((! is_dir($this->dir_runtime) && @mkdir($this->dir_runtime, 0644.true)) || !is_writable($this->dir_runtime)) {
            exit($this->dir_runtime . 'Directory requires write permission');
        }
        if (!empty($ret['errcode']) {// Log
            if(! is_dir($this->dir_runtime . 'log/')) {
                @mkdir($this->dir_runtime . 'log/'.0644.true);
            }
            @file_put_contents($this->dir_runtime . 'log/' . date('Ymd').'_err.log'.$res . PHP_EOL, FILE_APPEND);
            exit('Failed to get accessToken :' . $ret['errmsg']);
            //return ['code' => -1, 'msg' => $ret['errmsg']];
        }
        if(! is_dir($this->dir_runtime . 'data/')) {
            @mkdir($this->dir_runtime . 'data/'.0644.true);
        }
        @file_put_contents($this->dir_runtime . 'data/access_token', json_encode([
            'expire' => time() + 7000.// 200 seconds ahead
            'token'= >$ret['access_token']]));return $ret['access_token'];

    }

    /** * add promotion bit *@param$name String Promotion name, such as home promotion *@return array
     */
    public function promotion_add($name)
    {
        $data = json_encode([
            'promotionSourceName'= >$name
        ], JSON_UNESCAPED_UNICODE);
        $access_token = $this->get_access_token();
        $res = $this->curl_post($this->uri_base . '/union/promoter/promotion/add? access_token=' . $access_token.$data);
        $ret = json_decode($res.true);

        if (!empty($ret['errcode']) {return ['code'= > -1.'msg'= >$ret['errmsg']];
        }

        return ['code'= >0.'msg'= >'success'.'pid'= >$ret['pid']];
    }


    /** * delete a promotion bit *@param$PID String Promotional bit string *@param$name String Extension bit name *@return array
     */
    public function promotion_del($pid.$name)
    {
        $data = json_encode([
            'promotionSourcePid'= >$pid.'promotionSourceName'= >$name,
        ], JSON_UNESCAPED_UNICODE);
        $access_token = $this->get_access_token();
        $res = $this->curl_post($this->uri_base . '/union/promoter/promotion/del? access_token=' . $access_token.$data);
        $ret = json_decode($res.true);

        if (!empty($ret['errcode']) {return ['code'= > -1.'msg'= >$ret['errmsg']];
        }

        return ['code'= >0.'msg'= >'success'];
    }


    /** * update promotion *@param$PID String Promotional bit string *@param$oldName String Extension bit name (before modification) *@param$newName String New extension name *@return array
     */
    public function promotion_udp($pid.$oldName.$newName)
    {
        $data = json_encode([
            'previousPromotionInfo'= > ['promotionSourcePid'= >$pid.'promotionSourceName'= >$oldName,].'promotionInfo'= > ['promotionSourceName'= >$newName
            ]
        ], JSON_UNESCAPED_UNICODE);
        $access_token = $this->get_access_token();
        $res = $this->curl_post($this->uri_base . '/union/promoter/promotion/upd? access_token=' . $access_token.$data);
        $ret = json_decode($res.true);

        if (!empty($ret['errcode']) {return ['code'= > -1.'msg'= >$ret['errmsg']];
        }

        return ['code'= >0.'msg'= >'success'];
    }


    /** * get the list of promotion bits *@param int $start
     * @param int $limit
     * @return array
     */
    public function promotion_list($start = 0.$limit = 20)
    {
        $data = [
            'start'= >$start.'limit'= >$limit
        ];
        $access_token = $this->get_access_token();

        $res = $this->curl_get($this->uri_base . '/union/promoter/promotion/list? access_token=' . $access_token.$data);
        $ret = json_decode($res.true);

        if (!empty($ret['errcode']) {return ['code'= > -1.'msg'= >$ret['errmsg']];
        }

        return [
            'code'= >0.'msg'= >'success'.'list'= >$ret['promotionSourceList'].// Promotion bit data
            'total'= >$ret['total'].// Total number of promotion bits
            'limitMaxCount'= >$ret['promotionMaxCnt'].// Maximum number of promotion bits allowed to be created
        ];
    }

    /** * Get the category list and category ID *@return array
     */
    public function product_category()
    {
        $access_token = $this->get_access_token();

        $res = $this->curl_get($this->uri_base . '/union/promoter/product/category? access_token=' . $access_token.' ');
        $ret = json_decode($res.true);

        if (!empty($ret['errcode']) {return ['code'= > -1.'msg'= >$ret['errmsg']];
        }

        return [
            'code'= >0.'msg'= >'success'.'list'= >$ret['productCats'].// Category data
        ];
    }

       // Due to the space limitation here, you can view all of them at the link below
      / / other see https://blog.alipay168.cn/index/detail/item/628.html
    }
Copy the code

Curl curl curl curl curl curl curl curl curl

Example operations:


      
require (".. /union/UnionXcx.php");

$config = [
   'appid'= >' '.'secret'= >' '.'pid'= >' ',];$union = new wbs\xcx\union\UnionXcx($config);
// Get the category
$category = $union->product_category();
Copy the code