PHP部分代码
//生成推广 小程序二维码
public function huoqu_code()
{
$uid = input('uid');
if(empty($uid)){
return json(array('code'=>'0','Status'=>'invalid parameter','Msg'=>'参数错误'));
}
$this_user = Db::name('user')->where(array( 'id' => $uid))->find();
$tuiguang_code = '';
if($this_user['tuiguang_code']){
$tuiguang_code = $this_user['tuiguang_code'];
}else{
$access_token= $this->huoqu_accesstoken();
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token['access_token'];
$post_data =
array(
'page' => 'pages/index/index', //扫码跳转页面
'scene' => $this_user['invite_code'], //用户邀请码
);
$post_data = json_encode($post_data);
$data = $this->send_post($url, $post_data);
$result = $this->data_uri($data, 'image/png');
db('user')->where(array('id'=>$uid))->update(array('tuiguang_code' => $result));
$tuiguang_code = $result;
}
//数据封装到数组
$data = array(
'tuiguang_code' => $tuiguang_code,
);
return json(array('code'=>'1','Status'=>'success','Msg'=>'数据获取成功','data'=>$data));
}
//获取accesstoken
private function huoqu_accesstoken()
{
$website = Db::name('website')->find();
$appid = $website['xcx_appid'];//小程序appid
$appsecret = $website['xcx_appsecret'];//小程序appsecret
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=" . $appsecret;
$access_token = $this->curl_post($url);
$access_token = json_decode($access_token['result'], true);
return $access_token;
}
/**
* 发起curl请求
* $url 访问路径
* $params 参数,该数组多于1个,表示为POST
* $expire 请求超时时间
* $extend 请求伪造包头参数
* @return array 返回的为一个请求状态,一个内容
*/
private function curl_post($url, $params = array(), $expire = 0, $extend = array())
{
if (empty($url)) {
return array('code' => '0');
}
$_curl = curl_init();
$_header = array('Accept-Language: zh-CN','Connection: Keep-Alive','Cache-Control: no-cache');
// 只要第二个参数传了值之后,就是POST的
if (!empty($params)) {
curl_setopt($_curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($_curl, CURLOPT_POST, true);
}
if (substr($url, 0, 8) == 'https://') {
curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
curl_setopt($_curl, CURLOPT_URL, $url);
curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($_curl, CURLOPT_USERAGENT, 'API PHP CURL');
curl_setopt($_curl, CURLOPT_HTTPHEADER, $_header);
if ($expire > 0) {
curl_setopt($_curl, CURLOPT_TIMEOUT, $expire); // 处理超时时间
curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $expire); // 建立连接超时时间
}
// 额外的配置
if (!empty($extend)) {
curl_setopt_array($_curl, $extend);
}
$result['result'] = curl_exec($_curl);
$result['code'] = curl_getinfo($_curl, CURLINFO_HTTP_CODE);
$result['info'] = curl_getinfo($_curl);
if ($result['result'] === false) {
$result['result'] = curl_error($_curl);
$result['code'] = -curl_errno($_curl);
}
curl_close($_curl);
return $result;
}
/**
* 消息推送http
* @param $url
* @param $post_data
* @return bool|string
*/
private function send_post($url, $post_data)
{
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/json',
//header 需要设置为 JSON
'content' => $post_data,
'timeout' => 60
//超时时间
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
//二进制转图片image/png
private function data_uri($contents, $mime)
{
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}