后台curl网络请求
<?php
//前端进行网络请求 ajax
//后台进行网络请求用到两种方式 curl socket
//进行网络请求的步骤
//1.初始化一个curl
//2.对curl进行配置
//3.执行curl
//4.关闭curl
function httpGet($url){
//初始化
$curl = curl_init();
//curl_setopt设置一个cURL传输选项 三个参数
//由curl_init()返回的cURL句柄
//需要设置的CURLOPT_XXX选项
//将设置在option选项上的值
//配置
curl_setopt($curl,CURLOPT_URL,$url);
//作用:1.不让curl_exec自动执行 2.将结果返回一个字符串
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
//执行cURL
$res = curl_exec($curl);
//关闭
curl_close($curl);
return $res;
}
// $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxb2671dd2665ac3a9&secret=afeeba74b070dd6daf6784fe40b2451e";
// $res = httpGet($url);
// echo $res;
//封装post
function httpPost($url,$data){
//初始化一个curl回话
$curl = curl_init();
//声明post请求
curl_setopt($curl,CURLOPT_POST,true);
//设置curl回话
curl_setopt($curl,CURLOPT_URL,$url);
//设置请求体
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
//设置CURLOPT_RETURNTRANSFER 防止curl_exec直接输出
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
//执行
$res = curl_exec($curl);
//关闭
curl_close($curl);
//返回值
return $res;
}
$url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=kSptEAufwzE9YEc5TzOaliQaIB34qXkp8IvEX7h2YlRG6c8rwTktxOQxrh8lRN99w49Ksnrm2UHqpPJXpMggk-99r0I0bC6tRUASYIy3OhB5d1wgknGqUdkzZKvoSOcTKOWgAJAAEM";
$data = '{
"openid":"测试号",id
"remark":"改名"
}';
httpPost($url,$data);
$url1 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=kSptEAufwzE9YEc5TzOaliQaIB34qXkp8IvEX7h2YlRG6c8rwTktxOQxrh8lRN99w49Ksnrm2UHqpPJXpMggk-99r0I0bC6tRUASYIy3OhB5d1wgknGqUdkzZKvoSOcTKOWgAJAAEM&openid=oA0JhwOoEVNp8TGm2AqIkTuR4gC4&lang=zh_CN ";
echo httpGet($url1);
?>