I am accessing an API using a cURL request in PHP. The response I receive contains the data I would expect, but when I loop through to process it, certain keys cause the error "Uncaught TypeError: Cannot access offset of type string on string." I’ve read through the numerous help questions here related to the error, but can’t find anything with how to address a situation where "some" array keys cause an error.
Edit #2: I have determined that for some reason, the order of declaring the variables below is causing the error. If I simply move declaring $transactionType
above $transactionValue
with no other changes, the code works. Why would the order of accessing those values cause the code to fail? All data from the array is returned as strings from the API. (Leaving original statements unedited to give context and meaning to other answers)
My code:
$curl = curl_init($urlAPI);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'x-qn-api-version: 1',
'Content-Type: application/json'
]);
$response = curl_exec($curl);
curl_close($curl);
$decodedResponse = json_decode($response,true);
$transactionList = $decodedResponse["result"];
foreach($transactionList as $key => $value) {
$timestamp = $value["timeStamp"];
$hash = $value["hash"];
$transactionValue = $value["value"];
$transactionType = $value["functionName"]; //if I comment out this line with no other changes, it works as expected
//Do stuff
}
As noted, if I comment out $transactionType
it works perfectly and returns the 100+ results as expected. When I debug and print_r($transactionList)
, the functionName
key exists in every result (sometimes is empty, though). Another item of interest is if I use isset
to check the functionName
key first, it cannot find any instances where it is set – even though print_r clearly shows it is. An example of the result from the API:
Array (
[0] => Array (
[timeStamp] => 1624666351
[hash] => 0x37f2ec62dbd3f812215ae2e82dcfb11feb56d27255ab10ee102d4ce29942a347
[transactionIndex] => 95
[value] => 132506677002809378
[functionName] => )
[1] => Array (
[timeStamp] => 1624672333
[hash] => 0x9eebfa157829c632b58f743bbe7fb018add5de7a3881796cc6764e942cccf458
[transactionIndex] => 133
[value] => 122506677002809378
[functionName] => swap(uint256 amountOutMin, address[] path, address to, uint256 deadline) )
Where am I going wrong with this where my code breaks if I try to access functionName
?
2
Answers
UPDATE
If you do not have complete control of all the code you may need to roll back from version 8.1. There is the same problem in a WordPress plugin so their only option was to roll back to 8.0.
That is a completely different error than originally anticipated. The missing piece of the puzzle.
My best guess is this is the problem in the
swap()
function returned by the API:I do not know what swap returns, but that may be the source of the type error.
END OF UPDATE
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
It’s a type declaration issue.
Type declarations can be added to function arguments, return values, and, as of PHP 7.4.0, class properties. They ensure that the value is of the specified type at call time, otherwise a TypeError is thrown.
PHP Type declarations
Your variable type must match the type declared in the function.
You can change the type by using
intval(), strval(), boolval(), etc.
If you need pass
$transactionType
to a function or class as an integer, then you need to set the type:Or if it needs to be a string value:
If necessary in your trouble shooting, you can use
gettype()
which returns the type.