### 前言
最近自己做了团队内部的http调试工具,github开源地址 [https://github.com/diandianxiyu/ApiTesting](https://github.com/diandianxiyu/ApiTesting) ,通过这个项目又重新操作了PHP的curl函数,通过本篇博客进行记录,和大家一起学习~
### Code
本代码通过PHP7环境运行。
~~~
function curl($url, $method='GET',$fields = [], $headers=[],$auth = false){
//如果是get的获取方式,拼接参数到url上
if($method == "GET"){
$fields_string = http_build_query($fields);
$url=$url."?".$fields_string;
}
$curl = curl_init($url); //初始化
curl_setopt ($curl, CURLOPT_CUSTOMREQUEST, $method ); //设定HTTP请求方式
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.
curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge($header,$headers)); //和参数中的header一起传递过去
if($auth){
curl_setopt($curl, CURLOPT_USERPWD, "$auth");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
if($fields){
//POST
if($method == "POST"){//单独对POST方法设置参数传递
$fields_string = http_build_query($fields);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
}else{
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true) ;
}
}
$response = curl_exec($curl); //执行curl
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header_string = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$header_rows = explode(PHP_EOL, $header_string);
foreach($header_rows as $key => $value){
$header_rows[$key]=trim($header_rows[$key]);
}
$i=0;
foreach((array)$header_rows as $hr){
$colonpos = strpos($hr, ':');
$key = $colonpos !== false ? substr($hr, 0, $colonpos) : (int)$i++;
$headers[$key] = $colonpos !== false ? trim(substr($hr, $colonpos+1)) : $hr;
}
$j=0;
foreach((array)$headers as $key => $val){
$vals = explode(';', $val);
if(count($vals) >= 2){
unset($headers[$key]);
foreach($vals as $vk => $vv){
$equalpos = strpos($vv, '=');
$vkey = $equalpos !== false ? trim(substr($vv, 0, $equalpos)) : (int)$j++;
$headers[$key][$vkey] = $equalpos !== false ? trim(substr($vv, $equalpos+1)) : $vv;
}
}
}
curl_close($curl);
return array($body, $headers); //最终返回 result[0]为body,result[1]为header
}
~~~
### 参考资料
[http://www.php.net/curl](http://www.php.net/curl)
- 前言
- php编写RSS源
- PHP编写rss源(续)
- ubuntu 上给PHP安装扩展 Msgpack 和 Yar
- PHPCMS广告模块详细分析——广告的生成
- Yii配合Yar在php5.3.3环境下的错误以及解决方案
- 【Yaf】Yaf的环境安装遇到的问题以及解决方案
- 【PHP扩展】centos给PHP安装扩展
- 【MYSQL】PHPMYADMIN出现的问题以及解决方案
- 【PHP】阿里云升级PHP到5.5详解
- 【phpMyAdmin】修改配置文件连接到其他服务器
- 【PHP】PHP5.4.0版本ChangeLog详解(上)
- 【PHP】编译安装 PHP5.6.13遇到问题以及解决方案
- 【翻译】PHP7——新特性
- 【PHP】数组foreach引发的小问题
- 【CURL】PHP的CURL开发项目最佳实践
- 【PHP】PHP转换图片为ico格式源码
- 【PHP】PHP图像裁剪缩略裁切类源代码及使用方法