IT源码网

phpcurl类

qq123 2021年02月16日 编程语言 372 0

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

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

 

评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

phpcookie类