<?php
namespace app\components;

class CurlHelper{	

	//POST发送数据
	public static function sendPostData($url, $port, $arrPostInfo){
		$ch = curl_init();
		if(stripos($url,"https://")!==FALSE){
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	        curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
	    }
		if(!empty($port) && is_numeric($port)==true){
			curl_setopt($ch, CURLOPT_PORT, $port);
		}
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $arrPostInfo);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}

	//发送Json数据
	public static function sendJsonData($url, $port, $json){
		$ch = curl_init();
		if(stripos($url,"https://")!==FALSE){
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	        curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
	    }
	    if(!empty($port) && is_numeric($port)==true){
			curl_setopt($ch, CURLOPT_PORT, $port);
		}
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; encoding=utf-8','Content-Length: ' . strlen($json)));
		curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}

	//GET获取数据
	public static function fetchUrlData($url){
		$ch = curl_init();
		if(stripos($url,"https://")!==FALSE){
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	        curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
	    }
	    curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
	
}
?>