Have you tried using var_dump($result) to see what data you get? Illegal string offset means that the key you're trying to access does not exist in the response. It all depends on what data you get as a response.
Jun 11, 2021
3
Level 4
ACCESS THE VARIABLE FROM THIS STRING
I make ca curl request to paystack api and here is the result I get
{ "status": true, "message": "Authorization URL created", "data": { "authorization_url": "https://checkout.paystack.com/91ahxcwbqvfefrr", "access_code": "91ahxcwbqvfefrr", "reference": "4ge2lj12g1" } }
How do I access the value for status and authorization_url? Here is what I have tried
//execute post
$result = curl_exec($ch);
echo $result['status'];
Error: Illegal string offset 'status' then I tried
//execute post
$result = curl_exec($ch);
$responds = json_decode($result, true);
echo $responds;
ErrorException Array to string conversion
Here is the full code
$url = "https://api.paystack.co/transaction/initialize";
$fields_string = http_build_query($data);
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer " . getenv('PAYSTACK_SECRET_KEY'),
"Cache-Control: no-cache",
));
//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
Level 4
oops I got it solved. This is just confusion in my brain, probably from long hours of coding.
this here
$responds = json_decode($result, true);
Already converted the JSON data to an array and because I was trying to echo an array data I got Array to string conversion
1 like
Please or to participate in this conversation.