本文整理汇总了PHP中Json::getError方法的典型用法代码示例。如果您正苦于以下问题:PHP Json::getError方法的具体用法?PHP Json::getError怎么用?PHP Json::getError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Json
的用法示例。
在下文中一共展示了Json::getError方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: serialize
/**
* Serialize array to JSON string.
*
* @param array $args
* @return string
* @throws GraphCommons\Util\JsonException
*/
public function serialize(...$args) : string
{
$json = new Json($this->unserialize());
if ($json->hasError()) {
$jsonError = $json->getError();
throw new JsonException(sprintf('JSON error: code(%d) message(%s)', $jsonError['code'], $jsonError['message']), $jsonError['code']);
}
return (string) $json->encode($args);
}
示例2: fromJson
/**
* Create a signal collection from JSON string.
*
* @param string $json
* @return GraphCommons\Graph\SignalCollection
* @throws GraphCommons\Util\JsonException, \InvalidArgumentException
*/
public static final function fromJson(string $json) : SignalCollection
{
$json = new Json($json);
if ($json->hasError()) {
$jsonError = $json->getError();
throw new JsonException(sprintf('JSON error: code(%d) message(%s)', $jsonError['code'], $jsonError['message']), $jsonError['code']);
}
$data = $json->decode(true);
if (!isset($data['signals'])) {
throw new \InvalidArgumentException("'signals' field is required!");
}
$array = array();
foreach ($data['signals'] as $i => $signal) {
if (!isset($signal['action'])) {
throw new \InvalidArgumentException("Signal 'action' and 'parameters' fields are required!");
}
$array[$i]['action'] = Signal::detectAction(Util::arrayPick($signal, 'action'));
foreach ($signal as $key => $value) {
$array[$i]['parameters'][$key] = $value;
}
}
return self::fromArray($array);
}
示例3: serializeBody
/**
* Serialize request body as JSON.
*
* @param GraphCommons\Graph\Graph|array $body
* @return string
* @throws GraphCommons\Util\JsonException
*/
private final function serializeBody($body) : string
{
// check body if lib's object
if (is_object($body) && method_exists($body, 'serialize')) {
return $body->serialize();
}
$json = new Json($body);
if ($json->hasError()) {
$jsonError = $json->getError();
throw new JsonException(sprintf('JSON error: code(%d) message(%s)', $jsonError['code'], $jsonError['message']), $jsonError['code']);
}
return (string) $json->encode();
}
示例4: request
/**
* Perform a request.
*
* @param string $uri
* @param array $uriParams
* @param string $body
* @param array $headers
* @return GraphCommons\Http\Response
* @throws \InvalidArgumentException,
* GraphCommons\Http\Exception\Request, GraphCommons\Util\JsonException
*/
public final function request(string $uri, array $uriParams = null, string $body = '', array $headers = null) : Response
{
// match for a valid request i.e: GET /foo
preg_match('~^([a-z]+)\\s+(/.*)~i', $uri, $match);
if (!isset($match[1], $match[2])) {
throw new \InvalidArgumentException('Usage: <REQUEST METHOD> <REQUEST URI>');
}
$uri = sprintf('%s/%s/%s', $this->graphCommons->apiUrl, $this->graphCommons->apiVersion, trim($match[2]));
$uri = preg_replace('~(^|[^:])//+~', '\\1/', trim($uri, '/'));
$this->request->setMethod($match[1])->setUri($uri, (array) $uriParams);
if (!empty($headers)) {
foreach ($headers as $key => $value) {
$this->request->setHeader(trim($key), $value);
}
}
$requestMethod = $this->request->getMethod();
if ($requestMethod == Request::METHOD_POST || $requestMethod == Request::METHOD_PUT) {
// set body stuff
$body = trim($body);
$bodyLength = strlen($body);
$this->request->setBody($body);
$this->request->setBodyLength($bodyLength);
// set content headers stuff
$this->request->setHeader('Content-Type', 'application/json');
$this->request->setHeader('Content-Length', (string) $bodyLength);
}
$result = $this->request->send();
if ($result === null) {
$fail = $this->request->getFail();
throw new RequestException(sprintf('HTTP error: code(%d) message(%s)', $fail['code'], $fail['message']), $fail['code']);
}
unset($headers, $body);
// split headers/body pairs
@(list($headers, $body) = explode("\r\n\r\n", $result, 2));
if (!isset($headers)) {
throw new ResponseException('No headers received from server!');
}
if (!isset($body)) {
throw new ResponseException('No body received from server!');
}
// parse response headers
$headers = Util::parseResponseHeaders($headers);
if (isset($headers['status'])) {
$this->response->setStatus($headers['status']);
$this->response->setStatusCode($headers['status_code']);
$this->response->setStatusText($headers['status_text']);
}
$this->response->setHeaders($headers);
$this->response->setBody($body);
$json = new Json($body);
// render response body
$bodyData = $json->decode(true);
if ($json->hasError()) {
$jsonError = $json->getError();
throw new JsonException(sprintf('JSON error: code(%d) message(%s)', $jsonError['code'], $jsonError['message']), $jsonError['code']);
}
$this->response->setBodyData($bodyData);
return $this->response;
}