57°

微信小程序生成二维码

//获取二维码
public function getWxcode($account = 100091){
    $ACCESS_TOKEN=$this->getWxAccessToken();
    $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$ACCESS_TOKEN['access_token'];
    $post_data=
        array(
            'page'=>'pages/product_details/product_details',
            'scene'=>$account
        );
    $post_data=json_encode($post_data);
    $result = $this->httpRequest( $url, $post_data,"POST");
    header('Content-Type: image/jpeg');
    //file_put_contents($this->qrcode_path.date("Ymd/")."qrcode.png",$result);
    //$path = $this->qrCodes($result);
    $file = $_SERVER['DOCUMENT_ROOT'].'/public/uploads/qrcode/'.uniqid().'.png';
    
    // 写入文件
    file_put_contents($file, $result);

    // 图片地址返回到小程序端并进行展示 return str_replace('/var/www/html/zhihuiyinwang', 'https://new.zhyin.net', $file);

}

//请求 protected function httpRequest($url, $data='', $method='GET'){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_AUTOREFERER, 1); if($method=='POST') { curl_setopt($curl, CURLOPT_POST, 1); if ($data != '') { curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } }

curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;

}

//获取access_token public function getWxAccessToken(){ $appid=''; $appsecret=''; if(Session::get('access_token_'.$appid) && Session::get('expire_time_'.$appid)>time()){ return Session::get('access_token_'.$appid); }else{ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; $access_token = $this->makeRequest($url); $access_token = json_decode($access_token['result'],true); Session::set('access_token_'.$appid,$access_token); Session::set('expire_time_'.$appid,time()+7000); return $access_token; } }

public function makeRequest($url, $params = array(), $expire = 0, $extend = array(), $hostIp = '') { 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 = parse_url($url);
    if (empty($urlInfo['host'])) {
        $urlInfo['host'] = substr(DOMAIN, 7, -1);
        $url = "http://{$hostIp}{$url}";
    } else {
        $url = str_replace($urlInfo['host'], $hostIp, $url);
    }
    $_header[] = "Host: {$urlInfo['host']}";
}

// 只要第二个参数传了值之后,就是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;

}

本文由【小小小壮】发布于开源中国,原文链接:https://my.oschina.net/u/3853452/blog/3072794

全部评论: 0

    我有话说: