How to Display Json in PHP

Asked

Viewed 1,866 times

3

Caught a BD user via API.

<?php
require "vendor/autoload.php";

    use Intercom\IntercomClient;
        $client = new IntercomClient(App_ID, App_Key);

    $client->leads->getLeads(['email' => '[email protected]']); 
?>

NOTE: I have tried this:

Echo "< br>< pre>< br>";
Echo $client;
Echo "< br>< br>";

How I display the data I take?

  • 2

    json_encode and json_decode does not resolve? http://php.net/manual/en/ref.json.php

3 answers

4


According to the documentation, you will receive a JSON like this:

{
  "type": "contact.list",
  "total_count": 105,
  "contacts": [
    {
      "type": "contact",
      "id": "530370b477ad7120001d",
    },
    {
      "type": "contact",
      "id": "530370b477ad7120001d",
      "user_id": "8a88a590-e1c3-41e2-a502-e0649dbf721c",
      "email": "[email protected]",
      "name": "Winston Smith",
    },
    {
      "type": "contact",
      "id": "530370b477ad7120001d",
      "user_id": "8a88a590-e1c3-41e2-a502-e0649dbf721c",
      "email": "[email protected]",
      "name": "Winston Smith",
    },
    {
      "type": "contact",
      "id": "530370b477ad7120001d",
      "user_id": "8a88a590-e1c3-41e2-a502-e0649dbf721c",
      "email": "[email protected]",
      "name": "Winston Smith",
    }
  ],
  "pages": {
    "next": "https://api.intercom.io/contacts?per_page=50&page=2",
    "page": 1,
    "per_page": 50,
    "total_pages": 3
  }
}

Being as follows:

require "vendor/autoload.php";
use Intercom\IntercomClient;

$client = new IntercomClient(App_ID, App_Key);
$leads = $client->leads->getLeads(['email' => '[email protected]']); 

foreach($leads->contacts as $contact){
    echo "type: " . $contact->type;
    echo "id: " . $contact->id;
    echo "user_id: " . $contact->user_id;
    echo "email: " . $contact->email;
    echo "name: " . $contact->name;
}
  • Opa Marcelão blz ? guy returned me the following: &#xA;Warning: json_decode() expects parameter 1 to be string, object given in /home/u213882019/public_html/lead/get.php on line 7&#xA;&#xA;Warning: Invalid argument supplied for foreach() in /home/u213882019/public_html/lead/get.php on line 9

  • Gives a var_dump in $leads before the method json_decode and tell me his return.

  • Opa , that of our friend Leandro: he returned me a huge code, bad with the information. not wanting to abuse but as I only get an example field the ID. [ideone.com/Iy2wx1]

  • 1

    Comment on the line $leads = json_decode($leads);, within the foreachcomment the fields that will not be shown as well and see what happens

  • The Code is like this: <?php&#xA; require "vendor/autoload.php";&#xA; use Intercom\IntercomClient;&#xA; $client = new IntercomClient(App-ID, App_Key);&#xA; &#xA; $res = $client->leads->getLeads(['email' => '[email protected]']);&#xA; echo json_encode($res, JSON_PRETTY_PRINT);&#xA; &#xA;?>

  • the $res is returning the array, so you just insert inside the foreach to get the keys and their values.

  • echo $res[contacts][id]; would that be ?! I tried to return me Fatal error: Cannot use object of type stdClass as array in /home/u213882019/public_html/lead/get.php on line 9

  • 1

    No, use the example of that reply, just by removing the line $leads = json_decode($leads);

  • Friends, excuse the lack of knowledge @Leandroamaral does not have this line in my code. I just added the two lines you answered and worked :D

  • Lucas, take a look at the code of my reply after this issue. Like the API is already returning an object, just iterate on it.

  • Solved , Thank you very much for your attention, for the answers you all gave. by all people worked after Edit and the complement of @Marcelodeandrade and Leandroamaral

Show 6 more comments

3

$response = $client->leads->getLeads(['email' => '[email protected]']);
echo json_encode($response, JSON_PRETTY_PRINT);

[EDIT] Get a specific key

$response = $client->leads->getLeads(['email' => '[email protected]']);
foreach($response as $contact){
    echo "id: " . $contact->id;
}
  • Perfect worked! it returned me a huge code, bad with the information. not wanting to abuse but as I only get an example field the ID. [http://ideone.com/IY2wx1]

  • Leandro , is not displaying the ID value

  • On the foreach line change $Sponse, to $rest['users']

2

Use the json_decode:

$leads = $client->leads->getLeads(['email' => '[email protected]']); 

$clients = json_decode(json_encode($leads), true);

foreach ($clients as $chave => $valor){
    echo "$chave => $valor \n";
}

Editing: As can be seen in the answers of Marcelo de Andrade and Leandro Amaral, the json_decode is not necessary, you can iterate directly on the object returned from the API as shown in the responses.

  • I’m starting in this area which would pass in $key and $value ? me returned the errors: Warning: json_decode() expects Parameter 1 to be string, Object Given in /home/u213882019/public_html/lead/get.php on line 9 Warning: Invalid argument supplied for foreach() in /home/u213882019/public_html//lead/get.php on line 10

  • @Lucasbicalleto Do it like this: $clients = json_decode(json_encode($json), true);and try again. chave and valor is information about the current iteration item on the array returned.

  • -> Fatal error: Call to undefined function json_enc‌​ode() in /home/u213882019/public_html/lead/get.php on line 9 .If you want to add skype. I’ll give you some details. programacao.ciclum

  • @Lucasbicalleto Can create an example on http://ideone.com/ and post the link here?

  • http://ideone.com/ORrncy

  • @Lucasbicalleto What appears when you perform var_dump(function_exists('json_encode')); ??

  • how I got the returned ID from your Script ?

Show 2 more comments

Browser other questions tagged

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