How to View Json in PHP

3

I get a user from a DB 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 already tried this:

  

Echo "

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

How do I display the data I get?

    
asked by anonymous 17.08.2016 / 14:25

3 answers

4

According to documentation , you will receive 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
  }
}

It looks like this:

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;
}
    
17.08.2016 / 15:08
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;
}
    
17.08.2016 / 14:41
2

Use 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 responses from Marcelo de Andrade and < the json_decode is not necessary, you can iterate directly over the object returned from the API as shown in the responses.

    
17.08.2016 / 14:37