Question :
I get a user from a DB via API.
<?php
require "vendor/autoload.php";
use IntercomIntercomClient;
$client = new IntercomClient(App_ID, App_Key);
$client->leads->getLeads(['email' => 'kmodas@kmodas.com']);
?>
NOTE: I already tried this:
Echo “
"; Home Echo $ client; Home Echo "< br > < br >";
How do I display the data I get?
Answer :
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": "winstonsmith@truth.org",
"name": "Winston Smith",
},
{
"type": "contact",
"id": "530370b477ad7120001d",
"user_id": "8a88a590-e1c3-41e2-a502-e0649dbf721c",
"email": "winstonsmith@truth.org",
"name": "Winston Smith",
},
{
"type": "contact",
"id": "530370b477ad7120001d",
"user_id": "8a88a590-e1c3-41e2-a502-e0649dbf721c",
"email": "winstonsmith@truth.org",
"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 IntercomIntercomClient;
$client = new IntercomClient(App_ID, App_Key);
$leads = $client->leads->getLeads(['email' => 'kmodas@kmodas.com']);
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;
}
$response = $client->leads->getLeads(['email' => 'kmodas@kmodas.com']);
echo json_encode($response, JSON_PRETTY_PRINT);
[EDIT] Get a specific key
$response = $client->leads->getLeads(['email' => 'kmodas@kmodas.com']);
foreach($response as $contact){
echo "id: " . $contact->id;
}
Use json_decode
:
$leads = $client->leads->getLeads(['email' => 'kmodas@kmodas.com']);
$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.