Return of the Curl call in PHP is not in JSON

Asked

Viewed 81 times

-1

Can anyone tell me why the data doesn’t return in a structured way? I need to get the data out of the api https://swapi.dev/api/people/ and I would like it to return in json format, but I have tried many ways and could not.

<?php

$headers = array("Content-Type: application/json; charset=utf-8");
$url = "https://swapi.dev/api/people/";
$ch = curl_init($url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$resultado = json_decode(curl_exec($ch));

var_dump($resultado);
?>

The ideal would be the return as in the structure below:

{
    "birth_year": "19 BBY",
    "eye_color": "Blue",
    "films": [
        "https://swapi.dev/api/films/1/",
        ...
    ],
    "gender": "Male",
    "hair_color": "Blond",
    "height": "172",
    "homeworld": "https://swapi.dev/api/planets/1/",
    "mass": "77",
    "name": "Luke Skywalker",
    "skin_color": "Fair",
    "created": "2014-12-09T13:50:51.644000Z",
    "edited": "2014-12-10T13:52:43.172000Z",

}

But it’s returning to me like this:

object(stdClass)#2 (4) { ["count"]=> int(82) ["next"]=> string(35) "http://swapi.dev/api/people/?page=2" ["previous"]=> NULL ["results"]=> array(10) { [0]=> object(stdClass)#3 (16) { ["name"]=> string(14) "Luke Skywalker" ["height"]=> string(3) "172" ["mass"]=> string(2) "77" ["hair_color"]=> string(5) "blond" ["skin_color"]=> string(4) "fair" ["eye_color"]=> string(4) "blue" ["birth_year"]=> string(5) "19BBY" ["gender"]=> string(4) "male" ["homeworld"]=> string(31) "http://swapi.dev/api/planets/1/" ["films"]=> array(4) { [0]=> string(29) "http://swapi.dev/api/films/1/" [1]=> string(29) "http://swapi.dev/api/films/2/" [2]=> string(29) "http://swapi.dev/api/films/3/" [3]=> string(29) "http://swapi.dev/api/films/6/" } ["species"]=> array(0) { } ["vehicles"]=> array(2) { [0]=> string(33) "http://swapi.dev/api/vehicles/14/" [1]=> string(33) "http://swapi.dev/api/vehicles/30/" } ["starships"]=> array(2) { [0]=> string(34) "http://swapi.dev/api/starships/12/" [1]=> string(34) "http://swapi.dev/api/starships/22/" } ["created"]=> string(27) "2014-12-09T13:50:51.644000Z" ["edited"]=> string(27) "2014-12-20T21:17:56.891000Z" ["url"]=> string(30) "http://swapi.dev/api/people/1/" } [1]=> object(stdClass)#4 (16) { ["name"]=> string(5) "C-3PO" ["height"]=> string(3) "167" ["mass"]=> string(2) "75" ["hair_color"]=> string(3) "n/a" ["skin_color"]=> string(4) "gold" ["eye_color"]=> string(6) "yellow" ["birth_year"]=> string(6) "112BBY" ["gender"]=> string(3) "n/a" ["homeworld"]=> string(31) "http://swapi.dev/api/planets/1/" ["films"]=> array(6) { [0]=> string(29) "http://swapi.dev/api/films/1/" [1]=> string(29) "http://swapi.dev/api/films/2/" [2]=> string(29) "http://swapi.dev/api/films/3/" [3]=> string(29) "http://swapi.dev/api/films/4/" [4]=> string(29) "http://swapi.dev/api/films/5/" [5]=> string(29) "http://swapi.dev/api/films/6/" } ["species"]=> array(1) { [0]=> string(31) "http://swapi.dev/api/species/2/" } ["vehicles"]=> array(0) { } ["starships"]=> array(0) { } ["created"]=> string(27) "2014-12-10T15:10:51.357000Z" ["edited"]=> string(27) "2014-12-20T21:17:50.309000Z" ["url"]=> string(30) "http://swapi.dev/api/people/2/" } [2]=> object(stdClass)#5 (16) { ["name"]=> string(5) "R2-D2" ["height"]=> string(2) "96" ["mass"]=> string(2) "32" ["hair_color"]=> string(3) "n/a" ["skin_color"]=> string(11) "white, blue" ["eye_color"]=> string(3) "red" ["birth_year"]=> string(5) "33BBY" ["gender"]=> string(3) "n/a" ["homeworld"]=> string(31) "http://swapi.dev/api/planets/8/" ["films"]=> array(6) { [0]=> string(29) "http://swapi.dev/api/films/1/" [1]=> string(29) "http://swapi.dev/api/films/2/" [2]=> string(29) "http://swapi.dev/api/films/3/" [3]=> string(29) "http://swapi.dev/api/films/4/" [4]=> string(29) "http://swapi.dev/api/films/5/" [5]=> string(29) "http://swapi.dev/api/films/6/" } ["species"]=> array(1) { [0]=> string(31) "http://swapi.dev/api/species/2/" } ["vehicles"]=> array(0) { } ["starships"]=> array(0) { } ["created"]=> string(27) "2014-12-10T15:11:50.376000Z" ["edited"]=> string(27) "2014-12-20T21:17:50.311000Z" ["url"]=> string(30) "http://swapi.dev/api/people/3/" } [3]=> object(stdClass)#6 (16) { ["name"]=> string(11) "Darth Vader" ["height"]=> string(3) "202" ["mass"]=> string(3) "136" ["hair_color"]=> string(4) "none" ["skin_color"]=> string(5) "white" ["eye_color"]=> string(6) "yellow" ["birth_year"]=> string(7) "41.9BBY" ["gender"]=> string(4) "male" ["homeworld"]=> string(31) "http://swapi.dev/api/planets/1/" ["films"]=> array(4) { [0]=> string(29) "http://swapi.dev/api/films/1/" [1]=> string(29) "http://swapi.dev/api/films/2/" [2]=> string(29)
  • When removing json_decode you have to have the string the way it was returned. That is, equal to the result you said you wanted. Why use json_decode? Since the job of this function is to return an object or array exactly as it is returning you.

  • I’m doing this code based on a class. In this class is not put headers, only json_decode, the headers then includes to test and see if it ran. I took out json_decode and keeps returning as text :c

1 answer

0

Good morning. The problem is in json_decode, it has one more parameter that defines whether the return will be an associative array or an object, as you are not passing this parameter it uses the default parameter which is false and returns an object. pass the second parameter as true to return an array.

$resultado = json_decode(curl_exec($ch), true);

Source: https://www.php.net/manual/en/function.json-decode.php

  • Friend, I put how you passed and continue the same way. Deletes the headers of both the Curl and the variable above and also remains the same.

  • Hello. So, if you want to work with the data in PHP you don’t have much choice, or you decode the data for an object or an array. Now if you just want to print the result on the screen equal to the example, you can try using print_r instead of var_dump and re-enter the result back to json by passing the JSON_PRETTY_PRINT parameter in json_encode. EX: print_r( json_encode( $resultado, JSON_PRETTY_PRINT ) );

  • I just don’t get it, because this guy link worked and mine didn’t?

  • I do not think that the format of the results are different is a code error, maybe it is a browser plugin that is formatting the result of it. I found this plugin for the Chrome that left the result on my machine equal to the video.

  • Ok friend, with the plugin ran. Thank you!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.