Home > Software design >  How to declare variable from cURL PHP
How to declare variable from cURL PHP

Time:03-21

I have an cURL file that will return an array like below and I wonder how can I declare the variable of [data]=>[id].I tried like $decoded.data or $decoded.[data] but it does not work.

Array
(
    [data] => Array
        (
            [id] => 2
            [email] => [email protected]
            [first_name] => Janet
            [last_name] => Weaver
            [avatar] => https://reqres.in/img/faces/2-image.jpg
        )

    [support] => Array
        (
            [url] => https://reqres.in/#support-heading
            [text] => To keep ReqRes free, contributions towards server costs are appreciated!
        )

)

PHP file:

<?php
  $ch = curl_init();
  $url = "https://reqres.in/api/users/2";
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $resp = curl_exec($ch);
  $decoded = json_decode($resp,true);
  print_r($decoded);
  curl_close($ch);
?>

CodePudding user response:

$decoded['data']
$decoded['data']['id']

is the syntax. But the endpoint must return the array that you have printed with json_encode()

CodePudding user response:

I have an cURL file that will return an array like below

Let's break down what you're trying to say here, and use clearer terms that will help you understand what's going on.

  $ch = curl_init();
  $url = "https://reqres.in/api/users/2";
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $resp = curl_exec($ch);
  curl_close($ch); // you can call this straight away, you're done with $ch

This uses a library called "curl" to make an HTTP request, and gets the response body. The value in $resp is a string - not a file, and not anything curl or even HTTP specific, just a string.

$decoded = json_decode($resp,true);

This takes the string, and parses it according to a format called JSON. Setting the second parameter to true says that you want PHP arrays, not a mixture of arrays and stdClass objects. Assuming there are no errors, $decoded is now an array; not a JSON array, just a regular PHP array.

print_r($decoded);

This is what gives the output you put in the question. It's important to understand that that isn't "the array", it's just a way of showing it. Other ways include var_dump($decoded); and var_export($decoded);.


So, let's reword your first sentence:

I have a PHP array, which looks like this when displayed with print_r. (It's based on a JSON response I fetched using curl, but that's not really relevant right now.)


Now, onto your question:

how can I declare the variable of [data]=>[id]?

I think what you're trying to say is how do I retrieve the value shown in "[data]=>[id]". (Trust me, getting to know the right terminology will make your life much easier when you're searching and asking for help in future.)

And the answer is simple: in PHP, array elements are accessed using the syntax $array['key']. So $decoded['data'] accesses everything shown in [data] => in the print_r output, and $decoded['data']['id'] accesses the content of [id] => inside that.

  • Related