Micro-Credit Public number template message mass PHP instance code

Source: Internet
Author: User
Tags curl current time echo date json

Micro-letter template message can only be sent to a person, if you want to mass, need to pass through the PHP loop, and then send.

Note that if the template message is sometimes free and unstable, it is possible that your Access_token token update cache is out of date. Can be viewed from the log file. Suggest 300 seconds to update. Otherwise it would be annoying.

Template ID need to go to the public number to set up the industry after getting.

The code is as follows Copy Code
<?php
Use the method. Add Fahuo_wechat () directly to the logic of the page; the function is to be referenced beforehand.
Functionfahuo_wechat ($shopid) {
if (_cfg ("Sendmobile")) {
$thisdb = System::load_sys_class ("model");
$access = Getaccesstoken ()//template ID API, etc.
$access _token= $access [' Access_token '];
$template _id= $access [' template_id '];
$sql = "SELECT * from ' @ #_member_go_record ' where ' shopid ' = ' $shopid ' and status = ' paid, not shipped, unfinished '";
$row = $thisdb->getlist ($sql);
$POSTURL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token= $access _token";
foreach ($rowas $k=> $v) {
$uid = $v [' uid '];
$member _band= $thisdb->getone ("Select B_code,b_data from" @ #_member_band ' WHERE ' b_uid ' = ' $uid ' and b_data!= ' ");
if (Empty ($member _band)) {return false;}
return $row;
if (!empty ($member _band[' B_data ')) {
$url =web_path. " /mobile/user/buydetail/". $shopid;
$mobile = $v [' Company_money '];
$data =array (
"Touser" => $member _band[' b_code '],//one person at a time
"template_id" => $template _id,
"url" => $url,
Callback
"Data" =>array (
' =>array ' (
"Value" => "Hello,". $v [' username ']., your hand has been successfully assembled. ",
"Color" => "#888",
),
"Keyword1" =>array (
"Value" => $v [' Shopname '],
"Color" => "#888",
),
echo Date (' y-m-d h:i:s ', $itemlist [0][' Q_end_time '])
"Keyword2" =>array (
"Value" =>date (' y-m-d h:i:s ', $v [' time ']),
"Color" => "#888",
),
"Keyword3" =>array (
"Value" => "20 minutes",
"Color" => "#888",
),
"Keyword4" =>array (
"Value" =>round ($v [' Moneycount ']). " Golden Beans ",
"Color" => "#888",
),
"Keyword5" =>array (
"Value" => "Pending",
"Color" => "#888",
),
"Remark" =>array (
"Value" => "Click to view Details",
"Color" => "#054ff7",
),
),
);
Https_request ($POSTURL, Json_encode ($data), $mobile)//The contents of the shipping notice. To a micro-trust server
}
The string combination of sent shipping prompts was successful. Ready to send.
}
}
}
Micro-letter Delivery Preparation
Functiongetaccesstoken () {
Access_token should be stored and updated globally, with the following code written to the file as an example
$path = g_caches. ' Access_token.json ';
$data = Json_decode (file_get_contents ($path), true);
Print_r ($data); exit;
if ($data [' Expire_time '] < time ()) {//Expires less than current time, description expires, need to be updated
$appid = $data [' AppID '];
$appsecret = $data [' Appsecret '];
$url = "Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= $appid &secret=$ Appsecret ";
$json =getcurl ($url);
$data = Json_decode ($json, true);
Print_r ($res); exit;
$access _token= $data [' Access_token '];
if ($access _token) {
$thisdb = System::load_sys_class ("model");
$wechat = $thisdb->getone ("SELECT * from" @ #_wechat_config ' WHERE id = 1 ");//Get AppID
$data [' expire_time '] = time () + 300;//5 minute update. Ask for 500 times a day, no more than 2000 limits on the line.
The 300 here is best not to set too large. Otherwise, you will often be prompted to expire the token, unable to send a message. The official said 2 hours expired. It's a lie.
$data [' access_token '] = $access _token;
$data [' template_id ']= $wechat [' template_id '];
$data [' AppID ']= $wechat [' AppID '];
$data [' Appsecret ']= $wechat [' Appsecret '];
$FP =fopen ($path, "w");
Fwrite ($fp, Json_encode ($data));
Fclose ($FP);
}
}
Return$data;
}
Functionhttps_request ($url, $data = null, $mobile = ') {
$curl = Curl_init ()//initialization of a curl session
curl_setopt ($curl, Curlopt_url, $url);//This is the URL address you want to retrieve with PHP. You can also set this option when initializing with the Curl_init () function
curl_setopt ($curl, Curlopt_ssl_verifypeer, FALSE);
curl_setopt ($curl, Curlopt_ssl_verifyhost, FALSE);
if (!empty ($data)) {
curl_setopt ($curl, Curlopt_post, 1);
curl_setopt ($curl, Curlopt_postfields, $data);
A string that passes all data as an HTTP "POST" operation.
}
curl_setopt ($curl, Curlopt_returntransfer, 1);
The information that is curl_exec () is returned in the form of a file stream, rather than direct output.
$output = curl_exec ($curl);
if (!strstr ($output, ": 0"))
If the message after the letter is not included in the receipt: 0, it must have not been sent successfully. So put the unsuccessful in the log. Easy to find the reason.
{
$path 1= g_caches. ' Access_token2.json ';//access_token2.json Save the directory for the log, please correspond to the modification.
File_put_contents ($path 1, $output, file_append);//file_append means saving the log in an append way
}
if (Strstr ($output, "40003") && $mobile)//If is 40003 description, the customer does not pay attention to us, sends the information to remind him.
{
_sendmobile ($mobile, ' please pay attention to our public number. Otherwise we will not receive the micro-letter notice, thank you. ');//
}
Curl_close ($curl);
Returnjson_decode ($output);
}
Determine if the user name is a normal administrator
functionis_gly ($name) {
if (Strstr ($name, "gly"))
{
Returntrue;
}
Else
{
Returnfalse;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.