I have issued with my PHP code, recently i want to fetch data from API but when i echo my code it's appears this message "Warning: Illegal string offset 'today_usage'".
Here's the Input :
<?php
function apiRequest(array $params) {
$httpStreamOptions = [
'method' => $params['method'] ?? 'GET',
'header' => [
'Content-Type: application/json',
'Authorization: Bearer ' . ($params['token'] ?? '')
],
'timeout' => 15,
'ignore_errors' => true
];
if ($httpStreamOptions['method'] === 'POST') {
$httpStreamOptions['header'][] = sprintf('Content-Length: %d', strlen($params['payload'] ?? ''));
$httpStreamOptions['content'] = $params['payload'];
}
// Join the headers using CRLF
$httpStreamOptions['header'] = implode("\r\n", $httpStreamOptions['header']) . "\r\n";
$stream = stream_context_create(['http' => $httpStreamOptions]);
$response = file_get_contents($params['url'], false, $stream);
// Headers response are created magically and injected into
// variable named $http_response_header
$httpStatus = $http_response_header[0];
preg_match('#HTTP/[\d\.]+\s(\d{3})#i', $httpStatus, $matches);
if (! isset($matches[1])) {
throw new Exception('Can not fetch HTTP response header.');
}
$statusCode = (int)$matches[1];
if ($statusCode >= 200 && $statusCode < 300) {
return [
'body' => $response,
'statusCode' => $statusCode,
'headers' => $http_response_header
];
}
throw new Exception($response, $statusCode);
}
try {
$reqParams = [
'token' => 'getenv['API_KEY']',
'url' => 'https://api.[SECRET_SITES].['DOMAIN']/[VERSION]/['PARAMS']'
];
$response = apiRequest($reqParams);
$echo = $response['body']['today_usage']; <-- This is with [today_usage] and got warning
} catch (Exception $e) {
print_r($e);
}
?>
NOTES - I'm sorry but if you're confuse why the code not working because the link and token are confidential. Thank you for understanding.
Here's the Output with ['today_usage'] :
Warning: Illegal string offset 'today_usage' in D:\xampp\htdocs\wa.php on line 59
Here's the Output without ['today_usage'] :
array(3) { ["body"]=> string(406) "{"quota":{"message_per_day":100,"incoming_message_per_day":-1,"incoming_media_bytes_per_month":100000000,"batch_message_per_request":"50","today_usage":1,"today_incoming_usage":0,"current_month_incoming_media_bytes":0,"current_date":"2022-10-21T13:01:59.084Z","next_reset":"2022-10-22T00:00:00.000Z","daily_next_reset":"2022-10-22T00:00:00.000Z","countdown_reset":39481,"daily_next_countdown_reset":39481}}" ["statusCode"]=> int(200) ["headers"]=> array(9) { [0]=> string(15) "HTTP/1.1 200 OK" [1]=> string(35) "Date: Fri, 21 Oct 2022 13:01:59 GMT" [2]=> string(45) "Content-Type: application/json; charset=utf-8" [3]=> string(19) "Content-Length: 406" [4]=> string(17) "Connection: close" [5]=> string(21) "X-RateLimit-Limit: 60" [6]=> string(25) "X-RateLimit-Remaining: 59" [7]=> string(29) "X-RateLimit-Reset: 1666357354" [8]=> string(41) "ETag: W/"196-Qffp7E1jNYumvr7Ww6tZUzREmfE"" } }
The point is, it's not only today_usage but i want to get others data but i have no idea, i need help.
Thank you