Skip to content

Commit 3a280a5

Browse files
author
Openset
committed
Add: parseJSON
1 parent db6ae8b commit 3a280a5

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/HttpClient.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@
55
use GuzzleHttp\Client;
66
use Psr\Http\Message\ResponseInterface;
77

8+
/**
9+
* Class HttpClient
10+
* @package Openset
11+
*/
812
class HttpClient
913
{
14+
/**
15+
* @var
16+
*/
1017
protected static $client;
1118

19+
/**
20+
* @return Client
21+
*/
1222
public static function getClient()
1323
{
1424
if (is_null(self::$client)) {
@@ -21,6 +31,13 @@ public static function getClient()
2131
return self::$client;
2232
}
2333

34+
/**
35+
* @param $method
36+
* @param string $uri
37+
* @param array $options
38+
* @return bool|\Psr\Http\Message\StreamInterface
39+
* @throws \GuzzleHttp\Exception\GuzzleException
40+
*/
2441
public static function request($method, $uri = '', array $options = [])
2542
{
2643
$response = self::getClient()->request($method, $uri, $options);
@@ -31,4 +48,41 @@ public static function request($method, $uri = '', array $options = [])
3148
return false;
3249
}
3350

51+
/**
52+
* @param $uri
53+
* @param array $options
54+
* @return bool|\Psr\Http\Message\StreamInterface
55+
* @throws \GuzzleHttp\Exception\GuzzleException
56+
*/
57+
public static function get($uri, array $options = [])
58+
{
59+
return self::request('GET', $uri, $options);
60+
}
61+
62+
/**
63+
* @param $method
64+
* @param array $args
65+
* @return bool|mixed
66+
*/
67+
public static function parseJSON($method, array $args)
68+
{
69+
$body = call_user_func_array([self::class, $method], $args);
70+
if ($body instanceof ResponseInterface) {
71+
$body = $body->getBody();
72+
}
73+
74+
if (empty($body)) {
75+
return false;
76+
}
77+
78+
$contents = $body->getContents();
79+
$res = json_decode($contents, true);
80+
81+
if (JSON_ERROR_NONE !== json_last_error()) {
82+
return false;
83+
}
84+
85+
return $res;
86+
}
87+
3488
}

0 commit comments

Comments
 (0)