zoukankan      html  css  js  c++  java
  • php curl 传递数据

    <?php
    header("Content-type: text/html; charset=utf-8");    
    
    /**
    * curl 传递数据
    */
    class curl {
    	private $curl_resource;
    	private $url = '';
    	private $input = array();
    	private $curl_error = '';
    	private $curl_info = '';
    	public  $response;
    	public function __construct($url) {
    		$this->url = $url;
    	}
    	public function get_info() {
    		return array('url'=>$this->url,'input'=>$this->input,'curl_error'=>$this->curl_error,'curl_info'=>$this->curl_info);
    	}
    	private function connect() {
    		$this->curl_resource = curl_init();
    		curl_setopt($this->curl_resource, CURLOPT_HEADER, 0 ); // 过滤HTTP头
    		curl_setopt($this->curl_resource,CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
    		curl_setopt($this->curl_resource, CURLOPT_TIMEOUT, 30);    // 设置超时限制防止死循环
    	}
    	public function post($input) {
    		$this->connect();
    		$input = (array) $input;
    		$this->input = $input;
            $input = http_build_query($input); //根据数组产生一个urlencode之后的请求字符串
            curl_setopt($this->curl_resource, CURLOPT_URL, $this->url);
            curl_setopt($this->curl_resource,CURLOPT_POST,true); // post传输数据
            curl_setopt($this->curl_resource,CURLOPT_POSTFIELDS,$input); // post传输数据
            $this->curl_error = curl_error($this->curl_resource);
            $this->curl_info = curl_getinfo($this->curl_resource);
            $this->response = curl_exec($this->curl_resource);
            curl_close($this->curl_resource);
    		return $this->response;
    	}
    	public function get($input) {
    		$this->connect();
    		$input = (array) $input;
    		$this->input = $input;
            $input = $this->url.'?'.http_build_query($input);
            curl_setopt($this->curl_resource, CURLOPT_URL, $input);
            $this->response = curl_exec($this->curl_resource);
            curl_close($this->curl_resource);
    		return $this->response;
    	}
    }
    
    $curl = new curl('http://www.test.com/index.php');
    print_r($curl->post('post传递数据'));
    var_dump($curl->get_info());
    print_r($curl->get('get传递数据'));
    var_dump($curl->get_info());
    
  • 相关阅读:
    C#学习笔记——读写ini文件
    C#学习笔记——控件
    C#学习笔记——SerialPort类
    halcon学习笔记——遍历文件夹与文件选择
    halcon学习笔记——图像的预处理
    C#中创建和使用动态链接库
    C#学习笔记——控件的命名规范
    halcon学习笔记——(1)HDevelop language(语法结构特点)
    C#学习笔记——基础概念回顾
    C#学习笔记——继承窗体
  • 原文地址:https://www.cnblogs.com/buexplain/p/4693640.html
Copyright © 2011-2022 走看看