PHP获取小程序太阳码
function getAccessToken() { $appId = ""; $secretId = ""; $accessUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; $url = sprintf($accessUrl, $appId, $secretId); $result = $this->https_request($url); $Result = json_decode($result, true); return $Result['access_token']; } function getProductQrcode($product_id, $page, $scene) { $field = 'qrcode_image'; $file_path = 'product_qrcode'; $dir = ROOT_PATH . 'public/uploads/qr/mall_qrcode'; $user_info = \app\admin\model\shihu\Product::get($product_id); if (!empty($user_info[$field])) { return $user_info[$field]; } $res = $this->getAccessToken(); $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$res"; $processData = [ 'page' => $page, 'scene' => $scene//二维码携带的参数 ]; $qrData = json_encode($processData); $data = $this->https_request($url, $qrData); $tmp = json_decode($data, true); if ($tmp['errcode'] == 45009 || $tmp['errcode'] == 41030) { return (['code' => $tmp['errcode'], 'msg' => $tmp['errmsg']]); } $filename = $product_id . date('YmdHis') . rand(10000, 999999) . '.jpg'; if (!is_dir($dir)) { @mkdir($dir, 0777, true); } $file = $dir . '/' . $filename; file_put_contents($file, $data); $retFile = '/uploads/qr/' . $file_path . '/' . $filename; $user_info->qrcode_image = $retFile; $user_info->save(); return $retFile; } function https_request($url, $data = null) { if (function_exists('curl_init')) { $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } else { return false; } }