• 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;
    }
  • 相关阅读:
    C# 对XML操作-实例
    XML
    得到一个随机数组的方法
    Node Redis 小试
    Hexo快速搭建静态博客并实现远程VPS自动部署
    substr.js 字符串切割
    GraphicsMagick 学习笔记
    store.js 跨浏览器的localStorage
    bodyParser中间件的研究
    Sublime Text 使用指南
  • 原文地址:https://www.cnblogs.com/xyyphp/p/6053407.html
Copyright © 2020-2023  润新知