curl post数据

调用方式:
$header = self::getHeader();
$data = self::postUrl($url, $header);

/**
 * 组合Header
 * @return type
 */
protected static function getHeader()
{
	return ["AppKey: ".self::$appKey."
Nonce: ".self::$nonce."
CurTime: ".self::getCurTime()."
CheckSum: ".self::getCheckNum()."
Content-Type: ".self::$content_type['get_token_type']."
"];
}

/**
 * post方式访问云信服务器
 * @param type $url
 * @param array $header
 * @return type
 * @throws Exception
 * @throws TmacClassException
 */
protected static function postUrl($url, array &$header)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, self::$apiTimeout);//*秒超时设置
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	$curl_data = curl_exec($ch); //执行
	$curl_errno = curl_errno($ch);//最後一次執行的错误号0-没有错误(int)
	$curl_error = curl_error($ch);//返回错误信息 string
	curl_close($ch);
	if ($curl_errno > 0) {
		throw new Exception("CURL connection ($url) error: " ."$curl_errno $curl_error",$curl_errno);
	}
	if ((int)$curl_errno === 0){
		$result = json_decode($curl_data, true);
		if (isset($result["error"])) {
			$code = isset($result["code"]) ? $result["code"] : -1;
			throw new TmacClassException("{$code} {$result['error']}", $code);
		}
	}else {
		$result = $curl_error;
	}
	return $result;
}
原文地址:https://www.cnblogs.com/xyyphp/p/6053407.html