Hi,为确保您的账号安全,请大家修改登陆密码为包含大小写字母、标点、数字的复合密码,不要使用过于简单的登陆密码。
您当前的位置:首页 :: PHP

PHP CURL GET POST 方法

时间:2016-10-12 10:44:53  来源:  作者:

PHP CURL GET POST 方法 小编直接奉上,喜欢拿去

    /**
     * CURL工具
     *
     * @return mixed
     */
    public static function curl_post($url, $data, $timeout_ms = 3000, $header = array()) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "server curl");
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout_ms);
        if (!empty($header) && is_array($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
        $result = curl_exec($ch);
        $curl_info = curl_getinfo($ch);
 
        $curl_errno = curl_errno($ch);
        if($curl_errno){
            $loginfo = array(
                    'url'       => $url,
                    'data'      => $data,
                    'curl_info' => $curl_info,
                    'curl_errno'=> $curl_errno,
            );
            return false;
        }else{
            $loginfo = array(
                    'url'       => $url,
                    'curl_info' => $curl_info,
                    'result'    => $result,
            );
        }
        curl_close($ch);
        return $result;
    }
    
    
    /**
     *
     * @param string $url        请求链接
     * @param int    $timeout_ms 超时时间毫秒
     * @param array  $header     header 数组形式 例: array('Host : xxx.ezubo.com')
     * @return boolean|mixed
     */
    public static function curl_get($url, $timeout_ms = 2000, $header = array(
        'Connection: Keep-Alive',
        'Content-type: application/x-www-form-urlencoded;charset=UTF-8')){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "server curl");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $timeout_ms);
        if(! empty($header) && is_array($header)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        $result = curl_exec($ch);
        $curl_info = curl_getinfo($ch);
        $curl_errno = curl_errno($ch);
        
        if($curl_errno){
            $loginfo = array(
                'url'       => $url,
                'curl_info' => $curl_info,
                'curl_errno'=> $curl_errno,
            );
            return false;
        }else{
            $loginfo = array(
                'url'       => $url,
                'curl_info' => $curl_info,
                'result'    => $result,
            );
        }
        curl_close($ch);
        return $result;
    }

 

举报
收藏0次 / 评论0
评论(0)
还可以输入 2000 个字符
还可以输入 2000 个字符
取消回复
举报×

还可以输入 264 字符

收藏(0)×