kefu/extend/game/GameSrvApi.php

91 lines
2.3 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: zjz
* Date: 2018/7/26
* Time: 11:40
*/
namespace game;
use GuzzleHttp\Client;
use think\Log;
class GameSrvApi
{
protected $host = null;
public function __construct()
{
$this->host =config('game_host');
}
/**
* post请求
* @param $action
* @param $params
* @param bool $json_decode
* @return string|\stdClass
*/
public function doPost($action, $params, $json_decode = true)
{
$client = new Client([
'timeout' => 30
]);
$json = json_encode($params);
list($microsecond, $time) = explode(' ', microtime()); //' '中间是一个空格
$ts = floor((floatval($microsecond) + floatval($time)) * 1000);
$sign = md5(implode(';', ['5a7553560c9cf51c105678f3', '/' . $action, $json, $ts]));
$query = sprintf("ts=%s&sign=%s", $ts, $sign);
$fullUrl = $this->host . '/' . $action . '?' . $query;
Log::record('fullUrl:' . $fullUrl);
Log::record('json:' . $json);
$resBody = null;
try {
$res = $client->post($fullUrl, ['body' => $json]);
if ($res) {
if ($res->getStatusCode() == 200) {
$resBody = $res->getBody()->getContents();
if ($json_decode) {
return json_decode($resBody, true);
}
} else {
Log::record('response status code = ' . $res->getStatusCode());
//exception('游服status不通',404);
}
} else {
Log::record('client request null');
//exception('游服client不通',404);
}
} catch (\Exception $e) {
Log::record('游服'.$e->getMessage());
//exception('游服不通',404);
}
return $resBody;
}
/**
* 用户详情
*
* """Param"": {
* ""ID"":323, //用户id
* }"
*
*
* "{
* ""State"": 1,
* ""ErrMes"":"""",
* ""Data"": {
* }
* }"
*
* @param $params
* @return \stdClass|string
*/
public static function PlayerData($params)
{
return (new self)->doPost('api/Player/PlayerData', ['Param' => $params]);
}
}