PHP CURL支持HTTP、HTTPS 请求亲测可用

timg

废话就不多说 直接上代码,亲测好用原生:

/**
* curl发送htpp请求
* 可以发送https,http,get方式,post方式,post数据发送
*/
public function dataRequest($url,$https=true,$method=’get’,$data=null)
{
//初始化curl
$ch = curl_init($url);
//字符串不直接输出,进行一个变量的存储
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//https请求
if ($https === true) {
//确保https请求能够请求成功
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
}
//post请求
if ($method == ‘post’) {
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
}
//发送请求
$str = curl_exec($ch);
$aStatus = curl_getinfo($ch);
//关闭连接
curl_close($ch);
if(intval($aStatus[“http_code”])==200){
return json_decode($str);
}else{
return false;
}
}

使用方法

$result = $this ->dataRequest(‘https://api.weixin.qq.com/cgi-bin/,true,’get’,””);
//数值转换
$object = json_decode( json_encode( $result),true);
$data[‘access_token’] = $object[‘access_token’];

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部