public function getWxAccessToken(){
$appid="";
KaTeX parse error: Expected group after '_' at position 54: …t('access_token_̲'.appid) && Session::get(‘expire_time_’.KaTeX parse error: Expected group after '_' at position 64: …t('access_token_̲'.appid);
// }else{
KaTeX parse error: Expected 'EOF', got '&' at position 76: …ient_credential&̲appid=".appid."&secret=".$appsecret;
$access_token = this−>makeRequest(this->makeRequest(this−>makeRequest(url);
// print_r($access_token);die;
accesstoken=jsondecode(access_token = json_decode(accesstoken=jsondecode(access_token[‘result’],true);
// Session::set(‘access_token_’.appid,appid,appid,access_token);
// Session::set(‘expire_time_’.$appid,time()+7000);
return $access_token;
// }
}
/**
* 发起http请求
* @param string $url 访问路径
* @param array $params 参数,该数组多于1个,表示为POST
* @param int $expire 请求超时时间
* @param array $extend 请求伪造包头参数
* @param string hostIpHOST的地址∗@returnarray返回的为一个请求状态,一个内容∗/publicfunctionmakeRequest(hostIp HOST的地址
* @return array 返回的为一个请求状态,一个内容
*/
public function makeRequest(hostIpHOST的地址∗@returnarray返回的为一个请求状态,一个内容∗/publicfunctionmakeRequest(url, $params = array(), $expire = 0, $extend = array(), KaTeX parse error: Expected '}', got 'EOF' at end of input: … if (empty(url)) {
return array(‘code’ => ‘100’);
}
$_curl = curl_init();
$_header = array(
'Accept-Language: zh-CN',
'Connection: Keep-Alive',
'Cache-Control: no-cache'
);
// 方便直接访问要设置host的地址
if (!empty($hostIp)) {
urlInfo=parseurl(urlInfo = parse_url(urlInfo=parseurl(url);
if (empty($urlInfo[‘host’])) {
$urlInfo[‘host’] = substr(DOMAIN, 7, -1);
KaTeX parse error: Expected '}', got 'EOF' at end of input: url = "http://{hostIp}{$url}";
} else {
url=strreplace(url = str_replace(url=strreplace(urlInfo[‘host’], $hostIp, $url);
}
KaTeX parse error: Expected '}', got 'EOF' at end of input: …er[] = "Host: {urlInfo[‘host’]}";
}
// 只要第二个参数传了值之后,就是POST的
if (!empty(KaTeX parse error: Expected '}', got 'EOF' at end of input: … curl_setopt(_curl, CURLOPT_POSTFIELDS, http_build_query(params));curlsetopt(params));
curl_setopt(params));curlsetopt(_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(KaTeX parse error: Expected '}', got 'EOF' at end of input: …l_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;
}
public function getWxcode(){
$ACCESS_TOKEN=$this->getWxAccessToken();
$url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$ACCESS_TOKEN['access_token'];
$post_data=
array(
'page'=>"",
'scene'=>'14'//34%2CS853EE4QRP
);
$post_data=json_encode($post_data);
$data=$this->send_post($url,$post_data);
$result=$this->data_uri($data,'image/png');
return '<image src='.$result.'></image>';
}
/**
* 消息推送http
* @param $url
* @param $post_data
* @return bool|string
*/
protected 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 KaTeX parse error: Expected 'EOF', got '}' at position 13: result;
}̲
//二进制转图片image/…contents, $mime)
{
base64=base64encode(base64 = base64_encode(base64=base64encode(contents);
return (‘data:’ . $mime . ‘;base64,’ . $base64);
}
调用getwxcode就可以生成了,其中page需要符合规则。appid和appsecret需要填自己的
本文介绍了一种通过PHP获取微信小程序码的方法,包括获取微信Access Token和使用此Token生成无限小程序码的过程。代码示例展示了如何设置HTTP请求,解析响应,并将生成的小程序码转换为可直接显示的图片格式。
1万+

被折叠的 条评论
为什么被折叠?



