GET 请求
//CURL获取url返回值
function httpGet($url,$accessToken){
$header[] = 'Content-Type:application/x-www-form-urlencoded;charset=utf-8';
$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_HTTPHEADER, $header);
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );//是否返回值,1时给字符串,0输出到屏幕
$sContent = curl_exec($oCurl);//获得页面数据
$aStatus = curl_getinfo($oCurl);//获取CURL连接数据的信息
curl_close($oCurl);//关闭资源
//获取成功
$output_array = json_decode($sContent,true);//转换json格式
if(intval($aStatus["http_code"])==200){
return $output_array;
}else{
return false;
}
}
POST 请求
public function httpPost($url,$data){
$header[] = 'Key: fd9a6d2e576c4e299481a2eee';
$header[] = 'Content-Type:application/x-www-form-urlencoded;charset=utf-8';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
if($header) curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
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_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$tmpInfo = curl_exec($curl);
curl_close($curl);
$output_array = json_decode($tmpInfo,true);
return $output_array;
}
菜鸟教程:https://www.runoob.com/php/php-ref-curl.html
安装:
要使用PHP的cURL支持你必须在编译PHP时加上--with-curl[=DIR] 选项,DIR为包含lib和include的目录路径。在include目录中必须有一个名为curl,包含了easy.h和curl.h的文件夹。lib文件夹里应该有一个名为libcurl.a的文件。对于PHP 4.3.0你可以配置--with-curlwrappers 使cURL使用URL流。
PHP 常用的 curl 函数:
curl_init() 初始化一个cURL会话。
curl_setopt() 为给定的cURL会话句柄设置一个选项。
curl_exec() 执行一个cURL会话
curl_close() 关闭一个cURL会话。
curl_setopt 设置项:(菜鸟教程:https://www.runoob.com/php/func-curl_setopt.html)
bool curl_setopt ( resource $ch , int $option , mixed $value ) $ch: 由curl_init() 返回的curl 句柄。$option:需要设置的CURLOPT_XXX 选项。$value:将设置在 $option 选项上的值。
PUT请求:
function puturl($url,$data){
$data = json_encode($data);
$ch = curl_init(); //初始化CURL句柄
curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); //设置请求方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output,true);
}
DEL请求:
function delurl($url,$data){
$data = json_encode($data);
$ch = curl_init();
curl_setopt ($ch,CURLOPT_URL,$put_url);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output,true);
}
PATCH请求:
function patchurl($url,$data){
$data = json_encode($data);
$ch = curl_init();
curl_setopt ($ch,CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data); //20170611修改接口,用/id的方式传递,直接写在url中了
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output);
return $output;
}