php curl json 工具


/**
 * CURL工具
 * @author YANGJIAN
 *
 */
class Curl {
	private static $_ch;
	private static $_header;
	private static $_body;
	
	private static $_cookie = array();
    private static $_options = array();
    private static $_url = array ();
    private static $_referer = array ();

    /**
     * 调用外部url
     * @param $queryUrl
     * @param $param 参数
     * @param string $method
     * @return bool|mixed
     */
    public static function callWebServer($queryUrl, $param='', $method='get', $is_json=true, $is_urlcode=true) {
        if (empty($queryUrl)) {
            return false;
        }
        $method = strtolower($method);
        $ret = '';
        $param = empty($param) ? array() : $param;
        self::_init();
        if ($method == 'get') {
            $ret = self::_httpGet($queryUrl, $param);
        } elseif($method == 'post') {
            $ret = self::_httpPost($queryUrl, $param, $is_urlcode);
        } elseif($method == 'post_json') {
            $ret = self::_httpPostJson($queryUrl, $param, $is_urlcode);
        }
        if(!empty($ret)){
            if($is_json){
                return json_decode($ret, true);
            }else{
                return $ret;
            }
        }
        return true;
    }

    private static function _init() {
        self::$_ch = curl_init();

        curl_setopt(self::$_ch, CURLOPT_HEADER, true);
        curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt(self::$_ch, CURLOPT_FRESH_CONNECT, true);
    }

    public static function setOption($optArray=array()) {
		foreach($optArray as $opt) {
			curl_setopt(self::$_ch, $opt['key'], $opt['value']);
		} 
	}
	
	private static function _close() {
		if (is_resource(self::$_ch)) {  
            curl_close(self::$_ch);  
        }
        
        return true;
	}
	
	private static function _httpGet($url, $query=array()) {
          
        if (!empty($query)) {  
            $url .= (strpos($url, '?') === false) ? '?' : '&';  
            $url .= is_array($query) ? http_build_query($query) : $query;  
        }  
          
        curl_setopt(self::$_ch, CURLOPT_URL, $url);
        curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt(self::$_ch, CURLOPT_HEADER, 0);
        curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1);

        $ret = self::_execute();
        self::_close();
        return $ret;  
	}
	
	private static function _httpPost($url, $query=array(), $is_urlcode=true) {
  		if (is_array($query)) {
            foreach ($query as $key => $val) {  
				if($is_urlcode){
                    $encode_key = urlencode($key);
                }else{
                    $encode_key = $key;
                }
				if ($encode_key != $key) {  
					unset($query[$key]);  
				}
                if($is_urlcode){
                    $query[$encode_key] = urlencode($val);
                }else{
                    $query[$encode_key] = $val;
                }

            }  
        }
        curl_setopt(self::$_ch, CURLOPT_URL, $url);
        curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt(self::$_ch, CURLOPT_HEADER, 0);
        curl_setopt(self::$_ch, CURLOPT_POST, true );
        curl_setopt(self::$_ch, CURLOPT_POSTFIELDS, $query);
        curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1);


        $ret = self::_execute();
        self::_close();
        return $ret;  
	}
	
	
	private static function _httpPostJson($url, $query=array(), $is_urlcode=true) {
		if (is_array($query)) {
			foreach ($query as $key => $val) {
				if($is_urlcode){
					$encode_key = urlencode($key);
				}else{
					$encode_key = $key;
				}
				if ($encode_key != $key) {
					unset($query[$key]);
				}
				if($is_urlcode){
					$query[$encode_key] = urlencode($val);
				}else{
					$query[$encode_key] = $val;
				}
	
			}
		}
		$post_json_data = json_encode($query);
		curl_setopt(self::$_ch, CURLOPT_URL, $url);
		curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt(self::$_ch, CURLOPT_HEADER, 0);
		curl_setopt(self::$_ch, CURLOPT_POST, true );
		curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt(self::$_ch, CURLOPT_POSTFIELDS, $post_json_data);
		curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1);
		curl_setopt(self::$_ch, CURLOPT_HTTPHEADER, array(
		'accept: application/json;charset=UTF-8',
		'Content-Type: application/json',
		'Content-Length: ' . strlen($post_json_data))
		);
	
		$ret = self::_execute();
		self::_close();
		return $ret;
	}
	
	private static function _put($url, $query = array()) {
		curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'PUT');  
	
		return self::_httpPost($url, $query);
	}  

	private static function _delete($url, $query = array()) {
		curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'DELETE');  
	
		return self::_httpPost($url, $query);
	}  

	private static function _head($url, $query = array()) {
		curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
		
		return self::_httpPost($url, $query);
	}  
	
	private static function _execute() {
		$response = curl_exec(self::$_ch);
		$errno = curl_errno(self::$_ch);

		if ($errno > 0) {
			throw new \Exception(curl_error(self::$_ch), $errno);
		}
		return  $response;
	}
}

?>
$queryUrl = 'http://wwww.baidu.com'
Curl::callWebServer ($queryUrl, $param = $post_data, $method = 'post_json', $is_json = true, $is_urlcode = true );

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值