curl 请求类

curl 简介

phpGrace 封装了基于 curl 请求的工具类 class curl,实现了 get、post 请求,同事提供 header、ip 模拟功能;

函数列表

get()

功能 : 完成 get 请求
参数 : $url    请求地址
返回 : 请求结果

post()

功能 : 完成 post 请求
参数 : $url 请求地址, $data 数组形式的请求数据
返回 : 请求结果

header 与 ip

1 通过请求对象的 header 属性可以设置请求头;  
2 通过请求对象的 analogIP 属性可以开启 ip 模拟功能;

演示代码

get 请求

<?php
class indexController extends grace{
    public function index(){
        $curl = new \phpGrace\tools\curl();
        $res  = $curl->get('https://ip.cn/api/index?ip=&type=0');
        // 查看请求状态信息
        // p($curl->httpStatus); 
        echo $res;
    }
}

post 请求

<?php
class indexController extends grace{
    public function index(){
        $curl = new \phpGrace\tools\curl();
        $data = array('name' => 'grace', 'age' => 10);
        $res  = $curl->post('http://localhost/test/index/postdemo', $data);
        echo $res;
    }
    public function postdemo(){
        p($_POST);
    }
}

设置 header 及 ip 模拟

<?php
class indexController extends grace{
    public function index(){
        $curl = new \phpGrace\tools\curl();
        // 设置 header
        $curl->header   = array(
            "Connection: Keep-Alive",
            "Accept: text/html, application/xhtml+xml, */*",
            "Pragma: no-cache",
            "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3",
            "User-Agent: Mozilla/8.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"
        );
        // 模拟 ip
        $curl->analogIP = true;
        $res  = $curl->get('https://ip.cn/api/index?ip=&type=0');
        echo $res;
    }
}

下载地址

系统环境 可以在编辑器的tools目录下通过 Terminal 执行命令行直接下载到目录里 

curl -O http://liukuaizhuan.gitee.io/phpgracemanual/工具/curl通信类/curl.php