PHP/JSON - how to parse data returned via REST

I’m not sure if this entire value returned is JSON or a combination of stuff. I’m trying to get the name field from the carrier array inside this return.

When I call print_r($return) I get this:

        HTTP/1.1 200 OK Access-Control-Allow-Credentials: true 
    Access-Control-Allow-Headers: Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-     None-Match, If-Unmodified-Since Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS Access-Control-Allow-Origin: * Access-Control-Expose-Headers: ETag Content-Type: application/json Date: Thu, 31 Mar 2016 19:36:55 GMT Strict-Transport-Security: max-age=15768000 Twilio-Request-Duration: 0.153 Twilio-Request-Id: RQ499d995634d1424aab9ac6ba2476d11d X-Powered-By: AT-5000 X-Shenanigans: none Content-Length: 335 Connection: keep-alive {"caller_name": null, "country_code": "US", "phone_number": "+16145555555", "national_format": "(614) 555-5555", "carrier": {"mobile_country_code": "310", "mobile_network_code": "120", "name": "Sprint Spectrum, L.P.", "type": "mobile", "error_code": null}, "url": "https://lookups.twilio.com/v1/PhoneNumbers/+16148866678?Type=carrier"}

here is the JSON part:

          keep-alive 
        {
            "caller_name": null, 
            "country_code": "US", 
            "phone_number": "+16145555555", 
            "national_format": "(614) 555-5555", 
            "carrier": 
            {
                "mobile_country_code": "310", 
                "mobile_network_code": "120", 
                "name": "Sprint Spectrum, L.P.", 
                "type": "mobile", 
                "error_code": null
            }, 
        "url": "https://lookups.twilio.com/v1/PhoneNumbers/+16145555555?Type=carrier"}

How do I get just the name field from the carrier array? ex. Sprint Spectrum, L.P.

I’ve tried playing around with son_encode, but I get null values.

$jsond = '[{
            "caller_name": null, 
            "country_code": "US", 
            "phone_number": "+16145555555", 
            "national_format": "(614) 555-5555", 
            "carrier": 
            {
                "mobile_country_code": "310", 
                "mobile_network_code": "120", 
                "name": "Sprint Spectrum, L.P.", 
                "type": "mobile", 
                "error_code": null
            }, 
        "url": "https://lookups.twilio.com/v1/PhoneNumbers/+16145555555?Type=carrier"
    
}]';

$cell = json_decode($jsond, true);

echo $cell[0]['carrier']['name']; //echo

Example is here:

If you are using curl exclude the headers. The headers are being included in the response which results in invalid json.

I prefer using guzzlehttp instead of curl because it provides a much more sophisticated interface than just curl calls alone. One of which is the getBody() method which will return just the body of the response without the headers.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.