一.
<?php
namespace Org\Weixin;
class OrderPush
{
protected $appid;
protected $secrect;
protected $accessToken;
function __construct($appid, $secrect)
{
$this->appid = $appid;
$this->secrect = $secrect;
$this->accessToken = $this->getToken($appid, $secrect);
}
/**
* 发送post请求
* @param string $url
* @param string $param
* @return bool|mixed
*/
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch); //运行curl
curl_close($ch);
return $data;
}
/**
* 发送get请求
* @param string $url
* @return bool|mixed
*/
function request_get($url = '')
{
if (empty($url)) {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* @param $appid
* @param $appsecret
* @return mixed
* 获取token
*/
protected function getToken($appid, $appsecret)
{
if (S($appid)) {
$access_token = S($appid);
} else {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&app . $this->accessToken;
$dataRes = $this->request_post($url, urldecode($json_template));
if ($dataRes['errcode'] == 0) {
return true;
} else {
return false;
}
}
}
原文链接:https://www.jianshu.com/p/f3caa4fd54ad(感谢分享)
二、
public function http_request($url,$data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public function moban($name, $zu, $remain, $openid)
{
$appid = ""; //填写微信后台的appid
$appsecret = ""; //填写微信后台的appsecret
//从数据库查看access_token
$sql = "SELECT * FROM `tokentime` WHERE id='$appid'";
$query = mysql_query($sql);
$rk = mysql_fetch_array($query);
$time = date('Y-m-d H:i:s', time());
if ($rk == "") //数据库查询无结果 获取access_token并存入
{
$TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&app . $ACCESS_TOKEN;
$res = http_request($url, urldecode($json_template));
if ($res[errcode] == 0) echo '消息发送成功!';
}
原文链接:https://yq.aliyun.com/articles/200767?spm=a2c4e.11155472.0.0.2061590cl0G6F2(感谢分享)