Php 如何解决此解码和语法错误
我的php脚本有问题。 (我和我的朋友写这封信,为我的错误感到抱歉) 这是一个错误,告诉我: 致命错误:未捕获的InvalidArgumentException:json_解码错误: 中的语法错误 /opt/html/wordpress_eagle/forums/vendor/guzzlehttp/guzzle/src/functions.php:306Php 如何解决此解码和语法错误,php,json,restcord,Php,Json,Restcord,我的php脚本有问题。 (我和我的朋友写这封信,为我的错误感到抱歉) 这是一个错误,告诉我: 致命错误:未捕获的InvalidArgumentException:json_解码错误: 中的语法错误 /opt/html/wordpress_eagle/forums/vendor/guzzlehttp/guzzle/src/functions.php:306 堆栈跟踪:0 /opt/html/wordpress\u eagle/forums/vendor/restcord/restcord/src/
堆栈跟踪:
0 /opt/html/wordpress\u eagle/forums/vendor/restcord/restcord/src/DiscordClient.php(229): GuzzleHttp\json_解码(“”)1 /opt/html/wordpress\u eagle/forums/vendor/restcord/restcord/src/DiscordClient.php(184): RestRecord\DiscordClient->convertResponseToResult('guild',数组, 对象(GuzzleHttp\Psr7\Response),对象(GuzzleHttp\Command\Command))
#2/opt/html/wordpress_eagle/forums/vendor/guzzlehttp/command/src/ServiceClient.php(215): RestCord\DiscordClient->RestCord{closure}(对象(GuzzleHttp\Psr7\Response), 对象(GuzzleHttp\Psr7\Request),对象(GuzzleHttp\Command\Command))
#3/opt/html/wordpress_eagle/forums/vendor/guzzlehttp/command/src/ServiceClient.php(177): GuzzleHttp\Command\ServiceClient->transformResponseToResult(对象(GuzzleHttp\Psr7\Response), 对象(GuzzleHttp\Psr7\Request),对象(GuzzleHttp\Command\Command))
#4[内部函数]:GuzzleHtt in/opt/html/wordpress_eagle/forums/vendor/guzzlehttp/command/src/Exception/CommandException.php 在线57 这就是json_decode抛出错误的地方:http/src/functions.php:
*
*@param string$json要解析的json数据
*@param bool$assoc如果为true,则返回的对象将被转换
*转换为关联数组。
*@param int$depth用户指定的递归深度。
*@param int$options JSON解码选项的位掩码。
*
*@返回混合
*@throws\InvalidArgumentException(如果无法解码JSON)。
*@linkhttp://www.php.net/manual/en/function.json-decode.php
*/
函数json_decode($json,$assoc=false,$depth=512,$options=0)###306行,在function.php中
{
$data=\json\u decode($json、$assoc、$depth、$options);
if(JSON_ERROR_NONE!==JSON_last_ERROR()){
抛出新\InvalidArgumentException(
“json_解码错误:”.json_last_error_msg()
);
}
返回$data;
}
/**
*出现错误时抛出的JSON编码的包装器。
*
*@param mixed$value正在编码的值
*@param int$options JSON编码选项位掩码
*@param int$depth设置最大深度。必须大于零。
*
*@返回字符串
*@throws\InvalidArgumentException(如果无法对JSON进行编码)。
*@linkhttp://www.php.net/manual/en/function.json-encode.php
*/
函数json_encode($value、$options=0、$depth=512)
{
$json=\json_encode($value、$options、$depth);
if(JSON_ERROR_NONE!==JSON_last_ERROR()){
抛出新\InvalidArgumentException(
'json_encode error:'。json_last_error_msg()
);
}
返回$json;
这是commandexception.php中的一行:
//创建异常。
返回新的$class($message、$command、$prev、$request、$response);####这像致命错误一样带下划线。
}
discordclient.php:####restcord
类不协调客户端
{
/**
*@var数组
*/
私人美元期权;
/**
*@var GuzzleClient[]
*/
私人$categories=[];
/**
*@var记录器
*/
私人电脑;
/**
*客户端构造函数。
*
*@param数组$options
*/
公共函数_构造(数组$options=[])
{
$this->options=$this->validateOptions($options);
$this->logger=$this->options['logger'];
$stack=HandlerStack::create();
$stack->push(
新税率限制器(
$this->options['rateLimitProvider'],
$this->options,
$this->logger
)
);
$stack->push(
中间件::日志(
$this->logger,
新的MessageFormatter({response},$this->options['token'])
)
);
$stack->push(
中间件::日志(
$this->logger,
新的MessageFormatter({url}{request}',$this->options['token'])
)
);
$defaultGuzzleOptions=[
'base_uri'=>$this->options['apirl'],
“标题”=>[
'Authorization'=>$this->getAuthorizationHeader($this->options['tokenType'],$this->options['token']),
“用户代理”=>“DiscordBot(https://github.com/aequasi/php-restcord,{$this->getVersion()})”,
'内容类型'=>'应用程序/json',
],
'http_errors'=>isset($this->options['httpErrors'])?$this->options['httpErrors']:true,
'handler'=>$stack,
];
$this->options['guzzleOptions']=array_merge($this->options['guzzleOptions'],$defaultGuzzleOptions);
$client=新客户端($this->options['guzzleOptions']);
$this->buildDescriptions($client);
}
/**
*@param string$name
*
*@throws\Exception
*
*@return-guzzle客户端
*/
公共函数获取($name)
{
如果(!isset($this->categories[$name])){
抛出new\Exception('没有名称为'.$name'的类别);
}
返回$this->categories[$name];
}
/**
*@param数组$options
*
*@return数组
*/
私有函数验证选项(数组$选项)
{
$currentVersion=6;
$resolver=新选项resolver();
$resolver->setDefaults(
[
“版本”=>$currentVersion,
“记录器”=>新记录器(“记录器”),
“rateLimitProvider”=>新的MemoryRateLimitProvider(),
“throwOnRatelimit”=>错误,
'apiUrl'=>“https://discord.com/api/v{$currentVersion}/“,
“象征性
class DiscordClient
{
/**
* @var array
*/
private $options;
/**
* @var GuzzleClient[]
*/
private $categories = [];
/**
* @var Logger
*/
private $logger;
/**
* Client constructor.
*
* @param array $options
*/
public function __construct(array $options = [])
{
$this->options = $this->validateOptions($options);
$this->logger = $this->options['logger'];
$stack = HandlerStack::create();
$stack->push(
new RateLimiter(
$this->options['rateLimitProvider'],
$this->options,
$this->logger
)
);
$stack->push(
Middleware::log(
$this->logger,
new MessageFormatter('{response}', $this->options['token'])
)
);
$stack->push(
Middleware::log(
$this->logger,
new MessageFormatter('{url} {request}', $this->options['token'])
)
);
$defaultGuzzleOptions = [
'base_uri' => $this->options['apiUrl'],
'headers' => [
'Authorization' => $this->getAuthorizationHeader($this->options['tokenType'], $this->options['token']),
'User-Agent' => "DiscordBot (https://github.com/aequasi/php-restcord, {$this->getVersion()})",
'Content-Type' => 'application/json',
],
'http_errors' => isset($this->options['httpErrors']) ? $this->options['httpErrors'] : true,
'handler' => $stack,
];
$this->options['guzzleOptions'] = array_merge($this->options['guzzleOptions'], $defaultGuzzleOptions);
$client = new Client($this->options['guzzleOptions']);
$this->buildDescriptions($client);
}
/**
* @param string $name
*
* @throws \Exception
*
* @return GuzzleClient
*/
public function __get($name)
{
if (!isset($this->categories[$name])) {
throw new \Exception('No category with the name: '.$name);
}
return $this->categories[$name];
}
/**
* @param array $options
*
* @return array
*/
private function validateOptions(array $options)
{
$currentVersion = 6;
$resolver = new OptionsResolver();
$resolver->setDefaults(
[
'version' => $currentVersion,
'logger' => new Logger('Logger'),
'rateLimitProvider' => new MemoryRateLimitProvider(),
'throwOnRatelimit' => false,
'apiUrl' => "https://discord.com/api/v{$currentVersion}/",
'tokenType' => 'Bot',
'cacheDir' => __DIR__.'/../../../cache/',
'guzzleOptions' => [],
]
)
->setDefined(['token'])
->setAllowedValues('tokenType', ['Bot', 'OAuth'])
->setAllowedTypes('token', ['string'])
->setAllowedTypes('apiUrl', ['string'])
->setAllowedTypes('rateLimitProvider', [AbstractRateLimitProvider::class])
->setAllowedTypes('throwOnRatelimit', ['bool'])
->setAllowedTypes('logger', ['\Psr\Log\LoggerInterface'])
->setAllowedTypes('version', ['string', 'integer'])
->setAllowedTypes('guzzleOptions', ['array']);
return $resolver->resolve($options);
}
/**
* @param Client $client
*/
private function buildDescriptions(Client $client)
{
$description = \GuzzleHttp\json_decode(
file_get_contents(__DIR__.'/Resources/service_description-v'.$this->options['version'].'.json'),
true
);
$base = [
'baseUri' => $this->options['apiUrl'],
'version' => $description['version'],
'models' => $this->prepareModels($description['models']),
];
foreach ($description['operations'] as $category => $operations) {
$this->categories[$category] = new OverriddenGuzzleClient(
$client,
new Description(array_merge($base, ['operations' => $this->prepareOperations($operations)])),
function ($res, $req, $com) use ($category, $description) {
return $this->convertResponseToResult($category, $description, $res, $com);
},
$category
);
}
}
/**
* @param string $category
* @param array $description
* @param ResponseInterface $response
* @param CommandInterface $command
*
* @throws \Exception
*
* @return Result|mixed
*
* @internal param RequestInterface $request
*/
private function convertResponseToResult(
$category,
array $description,
ResponseInterface $response,
CommandInterface $command
) {
if ($response->getStatusCode() >= 400) {
throw new \Exception($response->getBody()->__toString(), $response->getStatusCode());
}
$operation = $description['operations'][$category][$command->getName()];
if (!isset($operation['responseTypes']) || count($operation['responseTypes']) === 0) {
try {
$content = $response->getBody()->__toString();
if (empty($content)) {
$content = '{}';
}
return new Result(json_decode($content, true));
} catch (\Exception $e) {
dump($response->getBody()->__toString());
throw $e;
}
}
$data = json_decode($response->getBody()->__toString());
$array = strpos($operation['responseTypes'][0]['type'], 'Array') !== false;
$responseType = $operation['responseTypes'][0]['type'];
if ($array) {
$matches = [];
preg_match('/Array<(.+)>/', $responseType, $matches);
$responseType = $matches[1];
}
$firstType = explode('/', $this->dashesToCamelCase($responseType, true));
$class = $this->mapBadDocs(
sprintf(
'\\RestCord\\Model\\%s\\%s',
ucwords($firstType[0]),
ucwords($firstType[1])
)
);
if (!class_exists($class)) {
return new Result($data);
}
if ($data === null) {
return new Result([]);
}
$mapper = new \JsonMapper();
$mapper->bStrictNullTypes = false;
if ($array) {
return array_map(
function ($item) use ($class, $mapper) {
return $mapper->map($item, new $class());
},
$data
);
}
return $mapper->map($data, new $class());
}
private function dashesToCamelCase($string, $capitalizeFirstCharacter = false)
{
$str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
if (!$capitalizeFirstCharacter) {
$str[0] = strtolower($str[0]);
}
return $str;
}
private function mapBadDocs($cls)
{
switch ($cls) {
case 'Channel\Invite':
case '\RestCord\Model\Channel\Invite':
case '\RestCord\Model\Guild\Invite':
return '\RestCord\Model\Invite\Invite';
case '\RestCord\Model\Guild\GuildChannel':
return '\RestCord\Model\Channel\GuildChannel';
case '\RestCord\Model\Guild\User':
case '\RestCord\Model\Channel\User':
return '\RestCord\Model\User\User';
default:
return $cls;
}
}
/**
* @param array $operations
*
* @return array
*/
private function prepareOperations(array $operations)
{
foreach ($operations as $operation => &$config) {
$config['uri'] = ltrim($config['url'], '/');
unset($config['url']);
$config['httpMethod'] = strtoupper($config['method']);
unset($config['method']);
if (isset($config['responseTypes']) && count($config['responseTypes']) === 1) {
$class = ucwords($config['resource']).'\\';
$class .= str_replace(' ', '', ucwords($config['responseTypes'][0]['name']));
$config['responseModel'] = $class;
} else {
$config['responseModel'] = 'getResponse';
}
if (isset($config['parametersArray']) && $config['parametersArray']) {
$config['type'] = 'array';
}
unset($config['parametersArray']);
foreach ($config['parameters'] as $parameter => &$parameterConfig) {
$this->updateParameterTypes($parameterConfig);
if (!isset($parameterConfig['required'])) {
$parameterConfig['required'] = false;
}
}
}
return $operations;
}
/**
* @param array $parameterConfig
*/
private function updateParameterTypes(array &$parameterConfig)
{
if ($parameterConfig['type'] === 'snowflake') {
$parameterConfig['type'] = 'integer';
}
if ($parameterConfig['type'] === 'bool') {
$parameterConfig['type'] = 'boolean';
}
if ($parameterConfig['type'] === 'file contents') {
$parameterConfig['type'] = 'string';
}
if (stripos($parameterConfig['type'], 'object') !== false) {
$parameterConfig['type'] = 'array';
}
}
/**
* @return string
*/
private function getVersion()
{
return trim(file_get_contents(__DIR__.'/../VERSION'));
}
/**
* @param array $toParse
*
* @return array|mixed
*/
private function prepareModels(array $toParse)
{
$models = [
'getResponse' => [
'type' => 'object',
'additionalProperties' => [
'location' => 'json',
],
],
];
foreach ($toParse as $category => $m) {
foreach ($m as $name => $model) {
$class = ucwords($category).'\\'.ucwords($name);
$models[$class] = [
'type' => 'object',
'properties' => [],
'additionalProperties' => [
'location' => 'json',
],
];
foreach ($model['properties'] as $n => $property) {
if ($property['type'] !== 'array' && $property['type'] !== 'object') {
$models[$class]['properties'][$n] = [
'type' => $property['type'],
'location' => 'json',
];
}
}
}
}
// Maps!
$models['Guild\\Channel'] = $models['Channel\\Channel'];
return $models;
}
/**
* @param string $tokenType
* @param string $token
*
* @return string
*/
private function getAuthorizationHeader($tokenType, $token)
{
switch ($tokenType) {
default:
$authorization = 'Bot ';
break;
case 'OAuth':
$authorization = 'Bearer ';
}
return $authorization.$token;
}
}