HTTP Guzzle未返回所有数据



我创建了一个函数,该函数使用Guzzle联系远程API,但我无法让它返回所有可用的数据。

我在这里调用函数:

$arr = array(
'skip' => 0,
'take' => 1000,
);
$sims = api_request('sims', $arr);

这是一个函数,我在$response变量中尝试了以下操作

json_decode($x->getBody(), true)
json_decode($x->getBody()->getContents(), true)

但两人都没有再出示任何记录。它返回了10条记录,我知道有超过51条记录可以返回。

use GuzzleHttpClient;
function api_request($url, $vars = array(), $type = 'GET') {
$username = '***';
$password = '***';

//use GuzzleHttpClient;
$client = new Client([
'auth' => [$username, $password],
]);

$auth_header = 'Basic '.$username.':'.$password;
$headers = ['Authorization' => $auth_header, 'Content-Type' => 'application/json'];
$json_data = json_encode($vars);
$end_point = 'https://simportal-api.azurewebsites.net/api/v1/';

try {
$x = $client->request($type, $end_point.$url, ['headers' => $headers, 'body' => $json_data]);
$response = array(
'success' => true,
'response' => // SEE ABOVE //
);
} catch (GuzzleHttpExceptionClientException $e) {
$response = array(
'success' => false,
'errors' => json_decode($e->getResponse()->getBody(true)),
);
}

return $response;
}

通过阅读https://simportal-api.azurewebsites.net/Help/Api/GET-api-v1-sims_search_skip_take我假设服务器不接受GET请求正文中的参数,并假设默认值为10,因为在许多应用程序中这是正常的,所以GET请求往往只使用查询字符串参数。

在该函数中,我会尝试更改它,以便在POST/PUT/PATCH请求的情况下发送正文;查询";在GET/DELETE请求的情况下没有json_encode。狂饮文档示例:

$client->request('GET', 'http://httpbin.org', [
'query' => ['foo' => 'bar']
]);

来源:https://docs.guzzlephp.org/en/stable/quickstart.html#query-字符串参数

最新更新