<h2 id="简介">简介
cURL的官方定义为:curl is a command line tool for transferring data with URL Syntax
,即使用URL语法规则来传输数据的命令行工具。
PHP 支持 Daniel Stenberg 创建的 libcurl 库,能够连接通讯各种服务器、使用各种协议。libcurl 目前支持的协议有 http、https、ftp、gopher、telnet、dict、file、ldap。 libcurl 同时支持 HTTPS 证书、HTTP POST、HTTP PUT、 FTP 上传(也能通过 PHP 的 FTP 扩展完成)、HTTP 基于表单的上传、代理、cookies、用户名+密码的认证。
PHP中使用curl">在PHP中使用cURL
图示:
PHP">/**
* get方式发送curl请求
* @param string $url 请求服务器地址
* @param array $header 请求头数据
* @param int $timeout 超时时间
* @return mixed
* @author itbsl
*/
function curl_get($url,$header=[],$timeout=30) {
//初始化curl
$curl = curl_init();
//设置curl(请求的服务器地址)
//参数1: curl资源
//参数2: 配置项<a href="https://www.jb51.cc/tag/mingcheng/" target="_blank" class="keywords">名称</a>
//参数3: 配置项的值
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_URL,$url);
//跳过安全证书验证
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_SSL_VERIFYHOST,false); // 从证书中检查SSL加密算法是否存在
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_SSL_VERIFYPEER,false); // 跳过证书检查
//设置<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>的信息以<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>流的形式返回,而不是直接<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_RETURNTRANSFER,true);
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_HTTPHEADER,$header);
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_TIMEOUT,$timeout);
//发出请求
$result = curl_exec($curl);
//<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" class="keywords">关闭</a>curl资源
curl_close($curl);
return $result;
}
<h3 id="curl模拟post请求">cURL模拟post请求
/**
* get方式发送curl请求
* @param string $url 请求服务器地址
* @param array $header 请求头数据
* @param int $timeout 超时时间
* @return mixed
* @author itbsl
*/
function curl_get($url,$header=[],$timeout=30) {
//初始化curl
$curl = curl_init();
//设置curl(请求的服务器地址)
//参数1: curl资源
//参数2: 配置项<a href="https://www.jb51.cc/tag/mingcheng/" target="_blank" class="keywords">名称</a>
//参数3: 配置项的值
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_URL,$url);
//跳过安全证书验证
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_SSL_VERIFYHOST,false); // 从证书中检查SSL加密算法是否存在
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_SSL_VERIFYPEER,false); // 跳过证书检查
//设置<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>的信息以<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>流的形式返回,而不是直接<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_RETURNTRANSFER,true);
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_HTTPHEADER,$header);
curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($curl,CURLOPT_TIMEOUT,$timeout);
//发出请求
$result = curl_exec($curl);
//<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" class="keywords">关闭</a>curl资源
curl_close($curl);
return $result;
}
<pre class="PHP">
/**
-
post方式发送curl请求
-
@param string $url 请求的服务器地址
-
@param array $data 要发送的数据
-
@param array $header 请求头数据
-
@param int $timeout 超时时间
-
@return mixed
-
@author itbslitbsl@foxmail.com
*/
function curl_post($url,$data=[],$header);
//设置请求方式为post请求
curl_setopt($curl,CURLOPT_POST,true);
//设置post方式提交时携带的数据
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
curl_setopt($curl,$timeout);
//发出请求
$result = curl_exec($curl);
//关闭curl资源
curl_close($curl);
return $result;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。