zoukankan      html  css  js  c++  java
  • curl模拟http请求

    简介

    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

    图示:

    cURL模拟get请求

    /**
     * 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: 配置项名称
        //参数3: 配置项的值
        curl_setopt($curl, CURLOPT_URL, $url);
    
        //跳过安全证书验证
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  // 跳过证书检查
    
        //设置获取的信息以文件流的形式返回,而不是直接输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    
        //发出请求
        $result = curl_exec($curl);
    
        //关闭curl资源
        curl_close($curl);
    
        return $result;
    }
    

    cURL模拟post请求

    /**
     * post方式发送curl请求
     * @param string $url   请求的服务器地址
     * @param array $data   要发送的数据
     * @param array $header 请求头数据
     * @param int $timeout  超时时间
     * @return mixed
     * @author itbsl<itbsl@foxmail.com>
     */
    function curl_post($url, $data=[], $header=[], $timeout=30) {
    
        //初始化curl
        $curl = curl_init();
    
        //设置curl(请求的服务器地址)
        //参数1: curl资源
        //参数2: 配置项名称
        //参数3: 配置项的值
        curl_setopt($curl, CURLOPT_URL, $url);
    
        //跳过安全证书验证
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  // 跳过证书检查
    
        //设置获取的信息以文件流的形式返回,而不是直接输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    
        //设置请求方式为post请求
        curl_setopt($curl, CURLOPT_POST, true);
    
        //设置post方式提交时携带的数据
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    
        //发出请求
        $result = curl_exec($curl);
    
        //关闭curl资源
        curl_close($curl);
    
        return $result;
    }
    
  • 相关阅读:
    网络测量中基于Sketch方法的简单介绍
    Reading SBAR SDN flow-Based monitoring and Application Recognition
    Reading Meticulous Measurement of Control Packets in SDN
    Reading SketchVisor Robust Network Measurement for Sofeware Packet Processing
    ovs加dpdk在日志中查看更多运行细节的方法
    后缀数组
    (转载)LCA问题的Tarjan算法
    Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)
    Vijos 1816统计数字(计数排序)
    卡特兰数
  • 原文地址:https://www.cnblogs.com/itbsl/p/9995882.html
Copyright © 2011-2022 走看看