The PHP code that calls the WeChat enterprise account interface to send notification information

When I was researching to use PHP to call the WeChat enterprise account interface to send notification information, I encountered a problem, that is, the problem of Chinese character encoding. If the data submitted by POST is of array type, first use json_encode to convert the array type data into josn string, but there will be problems if there are Chinese characters in the data: json_encode cannot serialize GB2312 encoded Chinese characters, if it is UTF- 8-encoded Chinese characters also become unrecognizable garbled characters after being converted with json_encode, and the interface of the WeChat enterprise account cannot receive these garbled characters. what can we do about it? After repeated research and repeated debugging, I finally found two methods:

The first method (array data):

1. Dump the page code into UTF-8 encoding;

2. Use urlencode to encode Chinese characters;

 

3. Use json_encode to convert array data into josn string

4. Use urldecode to decode the josn string data;

5. Then send the decoded josn string data to the WeChat enterprise account interface.

 

The second method (string data):

1. Dump the page code into UTF-8 encoding;

2. Connect the POST data to be transmitted with the type of string splicing;

3. Then send the spliced ​​string data to the WeChat enterprise account interface.

 

For the simplicity of the code, I used the second method, the code is as follows:

 

<?php
function curlPost($url,$data=""){   
    $ch = curl_init();
    $opt = array(
			CURLOPT_URL     => $url,            
            CURLOPT_HEADER  => 0,
			CURLOPT_POST    => 1,
            CURLOPT_POSTFIELDS      => $data,
            CURLOPT_RETURNTRANSFER  => 1,
            CURLOPT_TIMEOUT         => 20
    );
    $ssl = substr($url,0,8) == "https://" ? TRUE : FALSE;
    if ($ssl){
        $opt[CURLOPT_SSL_VERIFYHOST] = 1;
        $opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
    }
    curl_setopt_array($ch,$opt);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
$corpid="Please change it to the corpid of your enterprise number";
$corpsecret="Please change it to the corpsecret of your enterprise number";
$Url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$corpsecret";
$res = curlPost($Url);
$ACCESS_TOKEN=json_decode($res)->access_token;
$Url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$ACCESS_TOKEN";
$msg='Text message to send';
$data="{\"touser\":\"User ID to send to\",\"msgtype\":\"text\",\"agentid\":Your application ID,\"text\" :{\"content\":\"$msg\"},\"safe\":0}";
$res = curlPost($Url,$data);
$errmsg=json_decode($res)->errmsg;
if($errmsg==="ok"){
	echo "Send successfully!";
}else{
	echo "Send failed,".$errmsg;
}
?>

 

 

More introduction: http://www.cnblogs.com/jisheng/archive/2012/02/13/2350040.html

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327018830&siteId=291194637