之前有项目接入steam平台,这儿整理了常用到的WebApi封装,可以参考
usage
$steam = new SteamUtils([
'appid' => $this->currentConfig['appid'],
'appkey' => $this->currentConfig['appkey'],
'steamid' => $this->steamid,
'ticket' => $this->params['encrypted_loginkey'] ? :Cache::redis_get('_STEAM_LOGIN_'.$this->steamid),
]);
$ret = $steam->ISteamUserAuth->AuthenticateUserTicket();
if ( 0!==strcasecmp($ret['response']['params']['result'], 'OK') ){
// TODO u business
}
/**
* SteamUtils
* present by YCZ
* Author: Nomandia
* Date: 2020/6/19 13:53
* @see https://partner.steamgames.com/doc/features/microtransactions/implementation#error_codes 错误代码
*
* @property SteamUtils_IBroadcastService IBroadcastService
* @property SteamUtils_IGameInventory IGameInventory
* @property SteamUtils_ISteam ISteam
* @property SteamUtils_ISteamApps ISteamApps
* @property SteamUtils_ISteamCommunity ISteamCommunity
* @property SteamUtils_ISteamEconomy ISteamEconomy
* @property SteamUtils_ISteamGameServerStats ISteamGameServerStats
* @property SteamUtils_ISteamMicroTxn ISteamMicroTxn
* @property SteamUtils_ISteamNews ISteamNews
* @property SteamUtils_ISteamUser ISteamUser
* @property SteamUtils_ISteamUserAuth ISteamUserAuth
* @property SteamUtils_ISteamUserStats ISteamUserStats
* @property SteamUtils_ISteamWebAPIUtil ISteamWebAPIUtil
*
* @usage
* $steam = new SteamUtils([
* 'appid' => $this->currentConfig['appid'],
* 'appkey' => $this->currentConfig['appkey'],
* 'steamid' => $this->steamid,
* 'ticket' => $this->params['encrypted_loginkey'] ? :Cache::redis_get('_STEAM_LOGIN_'.$this->steamid),
* ]);
* $ret = $steam->ISteamUserAuth->AuthenticateUserTicket();
* if ( 0!==strcasecmp($ret['response']['params']['result'], 'OK') ){
* // TODO u business
* }
*/
class SteamUtils
{
/**
* @var string appid
*/
public $appid;
/**
* @var string appkey
*/
public $appkey;
/**
* @var string ticket
*/
public $ticket;
/**
* @var string gameid
*/
public $gameid;
/**
* @var string steamid
*/
public $steamid;
/**
* @var bool use_ssl 开启SSL(https)
*/
public $use_ssl = false;
/**
* @var bool return_json 返回JSON数据,否则返回文本
*/
public $return_json = true;
/**
* @var string language 语言代码
* @see https://baike.baidu.com/item/ISO%20639-1/8292914?fr=aladdin
*/
public $language = 'zh';
/**
* @var string currency Steam支持的币种(默认软妹币,单位分即100的整数倍)
* @see https://partner.steamgames.com/doc/store/pricing/currencies
*/
public $currency = 'CNY';
/**
* @var bool sandbox 是否开启沙箱(MicroTxn用到,仅改变URL)
*/
public $sandbox = false;
/**
* Author: Great Nomandia
* Created At 2020/6/19 14:36
*/
function __construct()
{
if (is_array($func = func_get_arg(0))) {
$this->appid = $func['appid'];
$this->appkey = $func['appkey'];
$this->steamid = $func['steamid'];
$this->ticket = $func['ticket'];
} else if (func_num_args() > 3) {
$this->appid = func_get_arg(0);
$this->appkey = func_get_arg(1);
$this->steamid = func_get_arg(2);
$this->ticket = func_get_arg(3);
}
}
function __destruct()
{
static::$implements = null;
$this->appid = $this->appkey = $this->steamid = $this->ticket = null;
}
/*********** ALIAS *************/
protected static $implements = [];
/**
* @param string $name
* @return mixed
* @throws SteamException
* Author: Great Nomandia
* Created At 2020/6/19 15:16
*/
function __get($name)
{
if (in_array($name, static::$implements)) {
return static::$implements[$name];
}
$clazz = 'SteamUtils_' . $name; // 增加一个前缀
if (class_exists($clazz) || !($clazz instanceof SteamUtils_ISteam)) {
/** @var SteamUtils_ISteam $implements */
static::$implements[$name] = new $clazz();
return static::$implements[$name]->bind($this);
}
throw new SteamException('Interface[' . $name . '] not exists', 502);
}
/*********** CURL TOOLS *************/
/**
* @param string $url
* @param mixed $params
* @param bool $json
* @param string $cookie
* @param bool $use_ssl
* @return string
* Author: Great Nomandia
* Created At 2020/6/19 14:06
*/
static function get($url, $params = null, $json = true, $cookie = '', $use_ssl = false)
{
return $json ?
static::requestJson($url, $params, 'GET', $cookie, $use_ssl) :
static::request($url, $params, 'GET', $cookie, $use_ssl);
}
/**
* @param string $url
* @param mixed $params
* @param bool $json
* @param string $cookie
* @param bool $use_ssl
* @return string
* Author: Great Nomandia
* Created At 2020/6/19 14:06
*/
static function post($url, $params = null, $json = true, $cookie = '', $use_ssl = false)
{
return $json ?
static::requestJson($url, $params, 'POST', $cookie, $use_ssl) :
static::request($url, $params, 'POST', $cookie, $use_ssl);
}
/**
* @var int _curl_port 默认请求端口
*/
static $_curl_port = 0;
/**
* @var array _curl_headers 附加的Header数据
*/
static $_curl_headers = [];
/**
* @var int _curl_connect_timeout 连接超时时间
*/
static $_curl_connect_timeout = 2;
/**
* @var int _curl_timeout 执行超时时间
*/
static $_curl_timeout = 5;
/**
* @var mixed fetch_result
*/
static $fetch_result;
/**
* @var mixed fetch_error
*/
static $fetch_error;
/**
* @param string $url
* @param mixed $params
* @param string $method
* @param string $cookie
* @param bool $use_ssl
* @return mixed
* Author: Great Nomandia
* Created At 2020/6/19 14:47
*/
static function requestJson($url, $params = null, $method = 'GET', $cookie = '', $use_ssl = false)
{
$ret = static::request($url, $params, $method, $cookie, $use_ssl);
return json_decode($ret, 1);
}
/**
* @param string $url
* @param mixed $params
* @param string $method
* @param string $cookie
* @param bool $use_ssl
* @return string
* Author: Great Nomandia
* Created At 2020/6/19 14:06
*/
static function request($url, $params = null, $method = 'GET', $cookie = '', $use_ssl = false)
{
$ch = curl_init();
if ('GET' == strtoupper($method)) {
$param_str = '';
// 默认范文为ipv4
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
if (is_array($params)) {
$param_str = http_build_query($params);
} else if (is_string($params) && strlen($params) > 0) {
$param_str = $params;
}
$url .= $param_str ? '?' . trim($param_str) : '';
curl_setopt($ch, CURLOPT_URL, $url);
} else {
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $params,
));
}
curl_setopt_array($ch, array(
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true, // 成功时返回取到的结果(如HTML或json)
CURLOPT_CONNECTTIMEOUT => self::$_curl_connect_timeout,
CURLOPT_TIMEOUT => self::$_curl_timeout,
CURLOPT_HTTPHEADER => array(
'Expect:',
),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
));
if (!empty(self::$_curl_headers)) {
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => self::$_curl_headers,
));
}
// disable 100-continue
if (!empty($cookie)) {
if (is_array($cookie)) {
$cs = [];
foreach ($cookie as $k => $v) {
$cs[] = $k . '=' . $v;
}
$cookie = join('; ', $cs);
}
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
// 关闭SSL设置,通常这样就可以直接访问HTTPS地址了
if (!$use_ssl) {
curl_setopt_array($ch, array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
));
}
if (self::$_curl_port) {
curl_setopt($ch, CURLOPT_PORT, intval(self::$_curl_port));
}
self::$fetch_result = curl_exec($ch);
self::$fetch_error = curl_error($ch);
curl_close($ch);
// 需要恢复的配置
self::$_curl_port = 0;
return self::$fetch_result ?: self::$fetch_error;
}
}
/*********** EXCEPTION *************/
class SteamException extends Exception
{
}
/*********** SUPPORT INTERFACE *************/
abstract class SteamUtils_ISteam
{
/**
* @var SteamUtils steam
*/
protected static $steam;
function bind(&$steam)
{
static::$steam = $steam;
return $this;
}
/**
* @param string $url
* @param array $params
* @param bool $no_merge 不增加默认参数表
* @return string|array|mixed
* Author: Great Nomandia
* Created At 2020/6/19 15:42
*/
function post($url, $params = [], $no_merge = false)
{
return (static::$steam)::post($url, $no_merge ? $params : array_merge([
'key' => static::$steam->appkey,
'appid' => static::$steam->appid,
'steamid' => static::$steam->steamid,
], array_filter($params)), static::$steam->return_json);
}
/**
* @param string $url
* @param array $params
* @param bool $no_merge
* @return string|array|mixed
* Author: Great Nomandia
* Created At 2020/6/19 17:11
*/
function get