美文网首页
PHP curl post/get 请求

PHP curl post/get 请求

作者: echo1028 | 来源:发表于2018-06-20 20:39 被阅读0次
    /**                                                              
     * curl 模拟post请求                                             
     *                                                               
     * @param string $url  请求地址                                  
     * @param array $data  需要post的数据                            
     * @param int $timeout 超时时间(秒)                              
     * @return mixed                                                 
     */                                                              
    function post($url, $data = [], $timeout = 3)                    
    {                                                                
        $ch = curl_init();                                           
        curl_setopt($ch, CURLOPT_URL, $url);                         
        curl_setopt($ch, CURLOPT_POST, 1);                           
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // 设置超时     
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);             
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);             
        $result = curl_exec($ch);                                    
        curl_close($ch);                                             
        return $result;                                              
    }   
    
    /**
     * curl 模拟get请求
     *
     * @param string $url   请求地址
     * @param int $timeout  超时时间(秒)
     * @return mixed
     */
    function get($url, $timeout = 3)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // 设置超时
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    
                                                                  
    

    相关文章

      网友评论

          本文标题:PHP curl post/get 请求

          本文链接:https://www.haomeiwen.com/subject/zrxfyftx.html