Wechat applet customer service message function php

Posted by geethalakshmi on Sat, 10 Aug 2019 10:10:59 +0200

Project description:

This project is a simple Wechat small program customer service message class, to achieve customer service message related functions. The official example of php is wrong, so I won't Tucao anymore.

This example uses a developer server instead of a cloud call.

Official documents:

Customer Service Message Guide

Customer Service Message Service

Applicable scenarios

Client message flow chart

Use steps

1. Open Customer Service News

https://mp.weixin.qq.com/wxam...

Login-Development-Development Settings-Message Push

[](https://raw.githubusercontent...

Click "Start"

[](https://raw.githubusercontent...

url (server address): Fill in the url corresponding to the developer server, such as https://xxxxxx/demo.php

Token (token): This is a random fill-in. It requires 3-32 bits.

Encoding AESKey (Message Encryption Key): Just click "Random Generation".

Message Encryption: You can choose compatible mode according to your needs.

Data format: JSON has more advantages than xml in terms of compression efficiency and transmission efficiency. Here we choose json.

Note: Do not submit after the above operation, wait until the developer server is configured before submitting.

2. Configuring the Developer Server

Verify the signature's PHP sample code:

    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];
    $echostr=$_GET["echostr"];

    $token = TOKEN;//Here's the token you filled out in the first step.
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode( $tmpArr );
    $tmpStr = sha1( $tmpStr );

    if ($tmpStr == $signature ) {
        return $echostr;
    } else {
        return false;
    }

The official example does not return $echostr, which is the key to verifying the success of the developer's server and must be returned.

3. Submit message push configuration

If no error is reported, the configuration is proved to be successful.

4. Developer Server demo

<?php



//Verify signature
//$signature = $_GET["signature"];
//$timestamp = $_GET["timestamp"];
//$nonce = $_GET["nonce"];
//$echostr=$_GET["echostr"];
//
//$token = TOKEN; // Change this to the token you filled out in the first step
//$tmpArr = array($token, $timestamp, $nonce);
//sort($tmpArr, SORT_STRING);
//$tmpStr = implode( $tmpArr );
//$tmpStr = sha1( $tmpStr );
//
//if ($tmpStr == $signature ) {
//    return $echostr;
//} else {
//    return false;
//}


include_once './Xcxmsg.php';
$xcxmsg = new Xcxmsg();

$postStr = file_get_contents('php://input');
if (!$postStr)
    return false;
$postArr = json_decode($postStr, true);
if (!isset($postArr['MsgType']) || !isset($postArr['FromUserName']))
    return false;
$data = ["touser" => $postArr['FromUserName']];

$accessToken = $xcxmsg->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accessToken;

switch ($postArr['MsgType']) {
    case "text":
        //If the user sends a text message, it is processed here.
        //Reply to graphic links or other types, as required
        $data['msgtype'] = "link";
        $data['link'] = [
            "title" => "hello",
            "description" => "Is Really A Happy Day",
            "url" => "LINK_URL",//Connection url
            "thumb_url" =>"THUMB_URL" //Picture url
        ];
        $json = json_encode($data, JSON_UNESCAPED_UNICODE);
        $xcxmsg->curl($json, $url);
        break;
    case "image": //If the user sends a picture message, enter here

        //The server can reply to pictures or other types, as required.
        $data['msgtype'] = "image";
        $data['image'] = ['media_id' => 'media_id value']; // media_id returned by executing $xcxmsg - > upload ($accessToken)
        $json = json_encode($data, JSON_UNESCAPED_UNICODE);
        $xcxmsg->curl($json, $url);
    case "miniprogrampage":
        //If the user sends a widget card, enter here
        //Here the server replies to small cards, or other types, as required.
        $data['msgtype'] = "miniprogrampage";
        $data['miniprogrampage'] = [
            "title" => "title",
            "pagepath" => "pages/index/index",
            "thumb_media_id" => "media_id value"];// media_id returned by executing $xcxmsg - > upload ($accessToken)
        $json = json_encode($data, JSON_UNESCAPED_UNICODE);
        $xcxmsg->curl($json, $url);
        break;
    case "event":
        //If the user enters a session event
        //Here you can reply to the text
        $data['msgtype'] = "text";
        $data['text'] = [
            "content" => "Hello World",
            ];
        $json = json_encode($data, JSON_UNESCAPED_UNICODE);
        $xcxmsg->curl($json, $url);
        break;
    default:
}

5. Small Program Front End

Add the following code where necessary:

<button open-type="contact" >Customer Service News</button>

With the preview of Wechat development tool, generate two-dimensional code, scan and test whether it is successful or not.

Project address: https://github.com/guyan0319/...

If you have any questions in use, please reply and correct them. Thank you.

Topics: PHP JSON curl SHA1