Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

successdav's avatar

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);
0 likes
3 replies
viorel's avatar

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.

1 like
successdav's avatar

vardum returns this

array(3) { ["status"]=> bool(true) ["message"]=> string(25) "Authorization URL created" ["data"]=> array(3) { ["authorization_url"]=> string(45) "https://checkout.paystack.com/292vr70wls1kztm" ["access_code"]=> string(15) "292vr70wls1kztm" ["reference"]=> string(10) "0leqbj26fe" } }
1 like
successdav's avatar
successdav
OP
Best Answer
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.