调用方式:
$header = self::getHeader();
$data = self::postUrl($url, $header);
/**
* 组合Header
* @return type
*/
protected static function getHeader()
{
return ["AppKey: ".self::$appKey."\nNonce: ".self::$nonce."\nCurTime: ".self::getCurTime()."\nCheckSum: ".self::getCheckNum()."\nContent-Type: ".self::$content_type['get_token_type']."\n"];
}
/**
* 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;
}