zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    数据库基础——EXISTS和IN
    C#基础——加密
    C#基础——派生和继承
    SQL Server——报表服务
    SQL Server——SQL Server Profiler
    UML基础——UML简介和历史
    C#基础——密码加密
    C#(ASP.NET)错误: 无法获取属性“0”的值: 对象为 null 或未定义 关键字 'user' 附近有语法错误。
    SQL Server——存储过程
    链表的声明及操作
  • 原文地址:https://www.cnblogs.com/xyyphp/p/6053407.html
Copyright © 2011-2022 走看看