PHP's "micro letter template message push" the relevant code

Tips:

Micro-channel push message template, the micro-channels have to go to the end, provided configuration template, generates a corresponding message template id, the string of characters, along with required parameter data passed to the micro channel side, remember! ~

The body portion of code required parameters, responsible for service module, ready micro-channel side: a first portion

/**
 * After completion of customer orders in the micro channel line-side push [Finish infos
 * @Param int $ orderid order id
 * @return mixed
 */
public function order_complete_wx_notice($orderid)
{
    //$this->write_log(['order_cmp'=>1,'$orderid'=>$orderid]);
    if($orderid){
        // Get the information displayed by the template orderid
        $order_info = $this->get_template_info($orderid, 3);
        //$this->write_log(['order_cmp'=>2,'order_info'=>$order_info]);
        if($order_info){
            // template data
            $request = [];
            $request['touser'] = $order_info['openid'];
            $ Request [ 'template_id'] = '84Muc5Er_gyddmU1sdfdXSssssdAvW93kI'; // order complete message template id
            $ Request [ 'page'] = 'pages / near / index'; // click on the jump page template card
            $ Request [ 'form_id'] = $ order_info [ 'prepay_id']; // id this payment
            $request['data'] = [
                'Keyword1' => [ 'value' => $ order_info [ 'course_name']], // course name
                'Keyword2' => [ 'value' => $ order_info [ 'order_complete_time']], // completion time
                'Keyword3' => [ 'value' => $ order_info [ 'gym_name']], // the name of the store room
                'Keyword4' => [ 'value' => $ order_info [ 'address']], // store address
                'Keyword5' => [ 'value' => 'Completed'], // order status
                'Keyword6' => [ 'value' => '400-010-88888'], // customer service telephone
                'Keyword7' => [ 'value' => 'Your order has been completed this time, please call customer service, valuable advice Oh!'], // Tips
            ];
            // $ request [ 'emphasis_keyword'] = 'keyword5.DATA'; // templates zoom Image
            //$this->write_log(['order_cmp'=>3,'request'=>$request]);
            // send data
            $this->wx_show_template($request);
        }
    }
}

/**
 * Id access to information through the order presented template
 * @Param int $ id order id ($ type = 2) || Order id ($ type = 3 or $ type = 1)
 * @Param int $ type 2, payment success; 3, the order is completed.
 * @return array
 */
public function get_template_info($id, $type)
{
    if($id && $type){
        $this->load->dao('order_dao');
        return $this->order_dao->get_template_info($id, $type);
    }
}

Part II: is responsible for data transmission to the prepared end of the micro-channel, micro-channel micro-channel push to give the user a message template

/**
 * Sending data to the end of the micro channel, display interface template
 * @param array $request
 */
public function wx_show_template($request)
{
    // Get redis_access_token value of redis
    $access_token = $this->get_redis_access_token();
    if(!$access_token){
        // acquired from the micro-channel end
        $access_token = $this->get_access_token();
        // Get into redis
        $this->load->library('dbredis');
        $this->dbredis->set('redis_access_token', $access_token);
    }
    if(!$access_token){
        exit ( 'access_token bad!');
    }
    // sending address
    $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;
    // request micro-channel, micro-channel side display template
    $this->curl_post_weixin($url, $request);
}

/**
 * CURL POST data to the micro-channel mode
 * @Param string $ url address request
 * @Param array $ data to send data
 */
public function curl_post_weixin($url, $data)
{
    if($url && count($data)){
        $headers = ['Content-Type:application/json'];
        $ch = curl_init();
        curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers); // key points
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_exec($ch);
        curl_close($ch);
    }
}

/**
 * Get access_token value
 * @return string $access_token
 */
public function get_access_token()
{
    $appConfig = [
        'app_id' => 'wxsdd9asdfghe5efc',
        'secret' => 'ec1879wiujhyytbdt786ddb7d29106'
    ];
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appConfig['app_id'].'&secret='.$appConfig['secret'];
    $ Ch = curl_init (); // create the handle
    curl_setopt ($ ch, CURLOPT_URL, $ url); // get the data through url
    curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // get information returned as a stream file
    curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); // skip verification certificate
    curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false); // check the SSL encryption certificate is
    $output = json_decode(curl_exec($ch));
    $access_token = $output->access_token;
    curl_close($ch);
    return $access_token;
}

Concluded:

WeChat template message push, the key point is to go to the micro channel side configuration to form the corresponding template, generated template id, then, is to prepare the parameters, after the data transfer, the micro-channel client receives messages curl function, based openID, pushes the message corresponding to a user's (mobile) communication terminal of the micro!
Published 59 original articles · won praise 2 · Views 5581

Guess you like

Origin blog.csdn.net/LDR1109/article/details/101272425