3

Trying to echo a couple of values from the CURL JSON response so i can put them in to a foreach loop but i can only get single indexed values to work.

$request = curl_init($api); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
//curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment if you get no gateway response and are using HTTPS
curl_setopt($request, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($request, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/x-www-form-urlencoded"
));

$response = (string)curl_exec($request); // execute curl fetch and store results in $response

curl_close($request); // close curl object

$result = json_decode($response, true); // true turns it into an array
echo 'First Name: ' . $result['first_name'] . '<br />'; // why doesnt this work
echo 'Last Name: ' . $result[0]['last_name'] . '<br />'; // yet i can return the first value

Example Array output

Array
(
    [0] => Array
        (
            [id] => 34761
            [first_name] => A
            [last_name] => Bailes
            [clinic] => 
            [phone] => 7409923279
            [fax] => 7409926740
            [address1] => 507 Mulberry Heights Rd
            [address2] => 
            [city] => Pomeroy
            [subdivision] => OH
            [country_code] => 
            [postal_code] => 45769-9573
            [timezone] => Array
                (
                    [timezone_type] => 3
                    [timezone] => America/New_York
                )

            [state] => OH
        )
)

I have json decode set to true for array output

$result = json_decode($response, true); // true turns it into an array

but when i try to echo the 'first_name' values it just returns empty.

echo 'First Name: ' . $result['first_name'] . '<br />'; // why doesnt this work

Yet i can return an indexed value

echo 'First Name: ' . $result[0]['first_name'] . '<br />';

What am i doing wrong here?

2
  • 1
    $result[0]['first_name'] is correct. See your array structure. Otherwise, you can set $result = json_decode( $response, true )[0] (on php >= 5.5) Commented Mar 9, 2016 at 14:36
  • 1
    what's the problem. you are getting the value by $result[0]['first_name'] right? $result is an array with 1 element in which is your associative massive. Commented Mar 9, 2016 at 14:36

2 Answers 2

2

Your result array is nested 2 deep. $result is an array of arrays. So:

$result[0]['first_name']

or

foreach ($result as $r) {
    echo $r['first_name'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry i'm not understanding. $result[0]['first_name'] works but will only get one result, the first in the index. I need to output all the results in a foreach loop but i only want specific e.g. the first_name values. When i use it in a foreach loop foreach ($result as $r) { echo $r['first_name']; }i get empty results (nothing is shown).
That's strange. The foreach should definitely work. I suggest running it through a debugger and checking the values.
Turns out it was an undefined index error because i had $result['first_name'] earlier. That was my problem all along. foreach loop working now. Thanks.
0

Response depend on server answer your request, the following work perfect to extract access token for example, you can try the following steps to test what you have and extract what you need too
After

$response = curl_exec($ch);
 curl_close($ch);

I start to show what I get by
Test:

print_r ($response);

Then go to extract (where server response ) header and body to use what available in each of them

list($header, $body) = explode("\r\n\r\n", $response, 2);

Test:

echo $header;
echo $body;

then I’m extract item in $body to use in my code using stdClass

$bodyObject = json_decode($body);

Test:

print_r($bodyObject);

Test:

echo $bodyObject->access_token;

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.