phpcurl类

1.需求

了解curl的基本get和post用法

2.例子

<?php
class Curl{
    private $timeout=30;
    
    public function set_timeout($timeout)
    {
        $this->timeout = $timeout;
        return $this;
    }
    public function get($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);

        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    public function post($url,$data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_POST,true);
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }
}

$obj=new Curl();
$arr=array('a'=>1,'b'=>2);
$post =$obj->post('http://www.baidu.com',$arr);
$get =$obj->get('http://www.baidu.com');

3.总结

这里是最基础的部分,最重要的就是参数的配置问题

原文地址:https://www.cnblogs.com/norm/p/6246409.html