zoukankan      html  css  js  c++  java
  • 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.总结

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

  • 相关阅读:
    JDBC的一些代码
    mysql
    【转载】如何简单地理解Python中的if __name__ == '__main__'
    【转载】用Scikit-Learn构建K-近邻算法,分类MNIST数据集
    数据科学入门---可视化数据
    Sum It Up
    Blue Jeans
    Zball in Tina Town
    Island Transport
    CD
  • 原文地址:https://www.cnblogs.com/norm/p/6246409.html
Copyright © 2011-2022 走看看