1

I am trying to integrate SMS balance to my website through API. API provides JSON for that . I am trying to extract an element from JSON data. I have tried a few ways but no success . I had tried to convert the JSON data to PHP readable like this $data = json_decode($json);. but i am not able to extract a element I am getting error 'Trying to access array offset on value of type null' please help here is my JSON output .And i want to SMS data from that JSON through PHP Thank You

{
    "balance":{
        "sms":1852
    },
    "status":"success"
}

here is my code this json data is store in response in below code :-

$apiKey = urlencode('***************');
 
    // Prepare data for POST request
    $data = array('apikey' => $apiKey);
 
    // Send the POST request with cURL
    $ch = curl_init('https://api.textlocal.in/balance/');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    
    // Process your response here

  

    echo $response;

    $object = json_decode($response);
    echo $object[0];
4
  • Please include a minimal reproducible example. Commented Jan 20, 2022 at 11:50
  • ok i have updated my question . Commented Jan 20, 2022 at 11:53
  • Please include your PHP code - make sure it's a minimal reproducible example. Commented Jan 20, 2022 at 11:54
  • done now i updated with my code thank you Commented Jan 20, 2022 at 11:55

1 Answer 1

1

This is how you access the data:

$response = '{ "balance":{ "sms":1852 }, "status":"success" }';
$object = json_decode($response);
echo $object->balance->sms;
// 1852
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.