0

I have a problem with my code for a work, I actually use a JSON API of vehicle park information:

When I use curl for GET the JSON like this:

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://demo.kepler-soft.net/api/v2.3/api-key/"key"/vehicles/?brand=Peugeot");
  curl_setopt($ch, CURLOPT_HTTPGET);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  //Decode and display the output
  $result =  curl_exec($ch);
  curl_close($ch);

  $data = json_decode($result,true);
  var_dump($data);

it returns me:

array(7) { [0]=> array(58) { ["totalResult"]=> string(1) "7" ["nbPageList"]=> int(1) ["createdAt"]=> string(24) "2018-05-09T10:25:15+0200" ["updatedAt"]=> string(24) "2018-05-09T10:28:47+0200" ["reference"]=> string(6) "1aqyy0" ["reportNumber"]=> int(6) ["country"]=> string(2) "FR" ["state"]=> string(18) "vehicle.state.parc" ["brand"]=> array(2) { ["reference"]=> string(13) "571a35428256b" ["name"]=> string(7) "PEUGEOT" } ["model"]=> array(2) { ["reference"]=> string(13) "59d5e93c66fcd" ["name"]=> string(3) "308" } ["version"]=> array(2) { ["reference"]=> string(13) "5a16a5018cd0a" ["name"]=> string(37) "308 1.6 BlueHDi 120ch S&S EAT6 Allure" } ["typeVoVn"]=> array(2) { ["reference"]=> string(13) "56f50a800ff53" ["name"]=> string(2) "VO" } ["vehicleType"]=> array(2) { ["reference"]=> string(13) "587e9d16446ec" ["name"]=> string(2) "VP" } ........

I know its in an array, and one vehicle is like an "object", so I want for all vehicles to get only "brand", "model", etc, and put it in cards on my website.

I already read questions like: How to use cURL to get jSON data and decode the data?

But it didn't works for me :(

I try many things like :

var_dump($data["brand"]["name"]); etc...

Or

$data=$result->vehicles->brand->name;

So if anyone can answer me that will help me so much!

I hope my post is clear, my English is not really good and its my first post on stackoverflow!

Thx. Tensei

Example of JSON API response:

  [  
   {  
      totalResult:"37",
      nbPageList:4,
      createdAt:"2018-05-09T10:25:15+0200",
      updatedAt:"2018-05-09T10:28:47+0200",
      reference:"1aqyy0",
      reportNumber:6,
      country:"FR",
      state:"vehicle.state.parc",
      brand:{  
         reference:"571a35428256b",
         name:"PEUGEOT"
      },
      model:{  
         reference:"59d5e93c66fcd",
         name:"308"
      },
      version:{  
         reference:"5a16a5018cd0a",
         name:"308 1.6 BlueHDi 120ch S&S EAT6 Allure"
      },
      typeVoVn:{  
         reference:"56f50a800ff53",
         name:"VO"
      },
      vehicleType:{  
         reference:"587e9d16446ec",
         name:"VP"
      },
      vin:"VF5JUHG5696YTVRFV",
      licenseNumber:"AZ-569-EE",
      energy:{  
         reference:"56f524a90c378",
         name:"Diesel"
      },
      firstHand:false,
      bodywork:{  
         reference:"56f52328c21ef",
         name:"Berline"
      },
      gearbox:{  
         reference:"56f50a7f3074f",
         name:"Boîte automatique"
      },
      color:{  
         reference:"56f50a7e6d344",
         name:"Blanc"
      },
      colorAccuracy:"vehicle.color_accuracy.glossy",
      pricePublicWithoutTax:"12000.00",
      pricePublic:"12000.00",
      priceSellerWithoutTax:"12000.00",
      priceSeller:"12000.00",
      dateOfDistribution:"2017-03-25T00:00:00+0100",
      year:"2017",
      distanceTraveled:120000,
      distanceTraveledWarranty:false,
      gallery:[  
         {  
            photo:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/109ce27d0e08b98a028e2a7dc47c30f9a66cc813.jpeg",
            large:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44888_admin_large.jpeg",
            big:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44888_admin_big.jpeg",
            thumb:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44888_admin_small.jpeg",
            position:0,
            reference:"d04b5bf398ef9a025251e1aaa8b4893b2bf71ba5",
            updatedAt:"2018-05-09T10:27:50+0200"
         },
         {  
            photo:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/1fccf17bffe828898ca3e6d7ddfd769643b890d8.jpeg",
            large:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44889_admin_large.jpeg",
            big:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44889_admin_big.jpeg",
            thumb:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44889_admin_small.jpeg",
            position:1,
            reference:"85fc496dbd5b2aa5b10e34a128d065c3c838a02e",
            updatedAt:"2018-05-09T10:27:53+0200"
         },
         {  
            photo:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/cfe766347a19c0e215e58114b1cb9cf3725f9710.jpeg",
            large:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44890_admin_large.jpeg",
            big:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44890_admin_big.jpeg",
            thumb:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44890_admin_small.jpeg",
            position:2,
            reference:"455d14dda58e59fe73782bdc21c18c43c173c73b",
            updatedAt:"2018-05-09T10:27:56+0200"
         },
         {  
            photo:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/8c42508ba993f603f3c4d3875d41390a3327b31c.jpeg",
            large:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44891_admin_large.jpeg",
            big:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44891_admin_big.jpeg",
            thumb:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44891_admin_small.jpeg",
            position:3,
            reference:"1de79d822e36061df0fcf7b6ed010b02202792e1",
            updatedAt:"2018-05-09T10:27:59+0200"
         },
         {  
            photo:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/0b42463c944706e24c39c1b939922379075af47e.jpeg",
            large:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44892_admin_large.jpeg",
            big:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44892_admin_big.jpeg",
            thumb:"https://demo.kepler-soft.net/uploads/demo/media/0001/45/thumb_44892_admin_small.jpeg",
            position:4,
            reference:"7d26039416029e2446c0c435f42facd62264c912",
            updatedAt:"2018-05-09T10:28:02+0200"
         }
      ],
      vo:507,
      gender:{  
         reference:"56f50a7ed5c2c",
         name:"VP"
      },
      taxHorsepower:6,
      horsepower:120,
      carEngine:1560,
      doors:5,
      seats:5,
      urbanConsumption:4.1,
      extraUrbanConsumption:3.4,
      mixteConsumption:3.6,
      extraUrbanKmConsumption:95,
      isWorkshop:false,
      isRental:false,
      isRecoverableVat:false,
      warrantyType:"vehicle.warranty.type.with",
      warrantyOrganization:"VOTH",
      warrantyLabel:{  
         reference:"570b8682b5b53",
         name:"Moteur, boite, pont"
      },
      warrantyKilometric:5000,
      warrantyDuration:3,
      supplier:{  
         phoneNumber:"02 51 98 09 09",
         address:{  
            address:"1 Bd Jean moulin",
            postalCode:"44100",
            city:"NANTES",
            country:"FR"
         },
         legalFormContact:"SARL",
         corporateNameContact:"KEPLER AUTOS",
         commonName:"KEPLER AUTOS",
         siret:"50822227000011",
         tvaIntracom:"FR 15 8025733"
      },
      expertise:{  
         elementList:[  

         ]
      },
      origin:"Négociant",
      valueOption:"12308.00",
      valueNew:"30200.00",
      valueNewOption:"42508.00",
      equipmentStandard:[  
Bushikot
  • 783
  • 3
  • 10
  • 26
Tensei
  • 37
  • 6

2 Answers2

0

It returns a list of arrays, therefore you might want to use something like:

foreach ($data as $vehicle) {
    var_dump($vehicle);
}

Or:

var_dump($data[0]['brand']['name']);

Here is a small sample code demonstrating the behaviour

$array = array('car1' => array('totalResult' => 7, 'nbPageList' => 1, 'brand' => array(' reference'=> "571a35428256b", 'name' => "PEUGEOT" )),
    'car2' => array('totalResult' => 1, 'nbPageList' => 1, 'brand' => array(' reference'=> "571a35428376b", 'name' => "RENAULT" )));

var_dump($array);

foreach ($array as $vehicleName => $vehicleDetails) {
    var_dump($vehicleDetails);
}
Florian
  • 845
  • 1
  • 9
  • 17
  • Hi Florian, first thx for the help, i understand i need to do a loop but how I define $vehicule like "one array of the array", do i have to make like : $vehicle=$data[] before ? – Tensei May 23 '18 at 14:23
  • You can also do something like `foreach ($data as $key => $value)` and use `$value` in the following code. Read the documentation from php.net [foreach-loop] (http://php.net/manual/en/control-structures.foreach.php) for more information regarding this. – Florian May 23 '18 at 14:32
0

You can achieve this using code below:

<?php
$data = array_map(function($item) {
    return [
        'brand' => $item['brand']['name'],
        'model' => $item['model']['name'],
        //add any other fields here
    ];
}, $data);

Check this example in action: https://3v4l.org/WvvMp

You could read more about array_map in official documentation: http://php.net/manual/ru/function.array-map.php

Bushikot
  • 783
  • 3
  • 10
  • 26
  • Hi and thx for the help @Bushikot, i adapt your code to my and i think its worked, do you know how hide " array(7) { [0]=> array(2) { ["brand"]=> string(7)" for just print "Peugeot" or "2008" ? Thx a lot ! – Tensei May 23 '18 at 14:34
  • @Tensei Sure, check example above. I've just fixed it. – Bushikot May 23 '18 at 14:47
  • I also strongly recommend you to read more about PHP arrays and how them works. In your $data, you can see structure like this [{..}, {..}, {..}]. It means that you have array of objects, and you can access any by it's index, which is started from 0 and ends by (number_of_objects-1). Then, you can access any object property via `->` operator or in a same way as array, using `['field_name']`. So, if you want to get first object brand name, it will be $data[0]['brand']['name] or $data[0]->brand->name. – Bushikot May 23 '18 at 14:58
  • Thx again for your help, i will learn more about arrays, i didnt see your last change for print only "Peugeot" for exemple. Thx a lot ! – Tensei May 23 '18 at 15:03
  • Oh, `$item['brand']` changed to `$item['brand']['name']`, same with 'model'. – Bushikot May 23 '18 at 15:05
  • Ha Ok, my mistake, I already do this change in my code before ask you, that why i didnt see any changement! That actually print me : array(7) { [0]=> array(2) { ["brand"]=> string(7) "PEUGEOT" ["model"]=> string(3) "308" } [1]=> array(2) { ["brand"]=> string(7) "PEUGEOT" ["model"]=> string(4) "2008" } and i want only "Peugeot" for exemple to print in my website, i try to put 'code' balise but that didnt work! sorry! – Tensei May 23 '18 at 15:12
  • If you want to build array containing only brands, for example, then replace whole returning array in anonymous function to `return $item['brand']['name']`. – Bushikot May 23 '18 at 15:25
  • Well, i want to put the informations in cards like : https://getbootstrap.com/docs/4.1/components/card/. I dont know if you mean what i want, I dont know if i have to put my html in a loop or if there is other solution than "var_dumb" for return only the text like "Peugeot" or "308". – Tensei May 23 '18 at 15:48
  • Well, I depends on how are you rendering HTML on your website. Are you using any PHP framework or CMS? `var_dump` is not designed to show something to end-users :) It's only for debugging for sure. – Bushikot May 23 '18 at 15:56
  • If you are calling this PHP code via AJAX, then replace `var_dump($data);` with `return $data;` and you will receive an array in you JS code. – Bushikot May 23 '18 at 15:58
  • I'm using Bootstraap, so only PHP/HTML/CSS i guess, i want to show of this informations in PHP. – Tensei May 23 '18 at 16:03
  • Then check this page http://php.net/manual/en/tutorial.firstpage.php. Basically you have to build whole page. When you made a request to your PHP file it will return any static HTML as is, and if you want to return something from PHP code, add blocks like ``. You can find another example here: https://stackoverflow.com/a/8286526/2562686. Still, it's a bad practice for serious projects to render HTML in such way nowadays. I recommend you to check template engines like Blade or Twig(most popular) later. – Bushikot May 23 '18 at 16:16