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.总结

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

  • 相关阅读:
    4-18
    Vue学习 2017-4-9
    前端杂谈
    不错的博客哦!
    待整理知识杂项
    Vue学习历程
    王工的权限理解
    【NX二次开发】图标图像
    【转】C++怎么读写windows剪贴板的内容?比如说自动把一个字符串复制.
    获取计算机名
  • 原文地址:https://www.cnblogs.com/norm/p/6246409.html
Copyright © 2011-2022 走看看