curl模拟http请求

<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请求


<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;
    }

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...