Products
GG网络技术分享 2025-03-18 16:17 0
请求代码:
/**
* 发送curl get
* @param string $url
* @return mixed
*/
function curl_get($url)
{
$oCurl = curl_init();
if (stripos($url, \"https://\") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (defined(\'CURLOPT_IPRESOLVE\') && defined(\'CURL_IPRESOLVE_V4\')) {
curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus[\"http_code\"]) == 200) {
return $sContent;
} else {
return false;
}
}
if (!function_exists(\'http_post_json\')){ //这一行是判断公共方法有无这个方法,避免重名~
/**
* PHP发送Json对象数据
* @param $url string
* @param $jsonStr string
* @param string[] $headers
* @return array
*/
function http_post_json(string $url, string $jsonStr, array $headers = array(
\'Content-Type: application/json; charset=utf-8\',
)): array
{
$headers[] = \'Content-Length: \' . strlen($jsonStr);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array($httpCode, $response);
}
}
接口调用:
/**必须先进入登录状态,然后拿到phone的code去请求然后拿到access_code,请求phone的接口 */
$appid = getConfig(\'appid_y\'); //填写自己的appid,小程序中看
$secret = getConfig(\'secret_y\'); //填自己的secret,公众平台看
$url = \"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret\";
$access_token = json_decode(curl_get($url),true);
if(isset($access_token[\'errcode\']))
return [\'errcode\'=>$access_token[\'errcode\'],\'msg\'=>\'请求失败\',\'data\'=>$access_token];
$access_token = $access_token[\'access_token\']; //获取到了access_token
//请求电话号使用方法只能在公网能访问的目录下进行,本地进行没有返回值
$url = \'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=\'.$access_token;
$json_code = json_encode([\'code\'=>$param[\'code\']]);
$headers = [
\'Accept: application/json\',
\'User-Agent: */*\',
\'Content-Type: application/json; charset=utf-8\',
];
$phone = http_post_json($url,$json_code,$headers);
$phone[1] = json_decode($phone[1],true);
if(emptyempty($phone[1])||$phone[1][\'errcode\']!=0)
throw new Exception(\'系统获取手机号失败\');
$phoneNumber = $phone[1][\'phone_info\'][\'phoneNumber\'];
/**拿到电话号码end */Demand feedback