chapperash
(Ashley Chaplin)
July 27, 2021, 7:55am
1
Im new to Postman and API’s but have managed to create the following in Postman to GET my data:
<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://payments.pabbly.com/api/v1/subscription/60fe59194ffedb5c6');
$request->setMethod(HTTP_Request2::METHOD_GET);
$request->setConfig(array(
'follow_redirects' => TRUE
));
$request->setHeader(array(
'Authorization' => 'Basic XXXXXXXXXXXXXXX'
));
try {
$response = $request->send();
if ($response->getStatus() == 200) {
echo $response->getBody();
}
else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
}
}
catch(HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
My question is, how do i decode the JSON data and present it in a way that looks a bit cleaner?
Welcome to the community @chapperash !
Can you show us a screenshot of how the json looks in Postman? The php is good to see but doesn’t help as much for the backend (unless something needs to change there).
Thanks!
Orest
chapperash
(Ashley Chaplin)
July 27, 2021, 7:55pm
3
Sure. Thanks for the help. The JSON is:
{
"status": "success",
"message": "Customer data",
"data": {
"company_name": "Test Co",
"website": "",
"phone": "",
"billing_address": {
"street1": "11, My Street",
"city": "My City",
"state": "My State",
"state_code": "LAN",
"zip_code": "BB12 7GH",
"country": "GB"
},
"shipping_address": {
"attention": "",
"street1": "",
"street2": "",
"city": "",
"state": "",
"zip_code": "",
"country": ""
},
"portal_status": true,
"createdAt": "2021-07-26T06:38:04.707Z",
"updatedAt": "2021-07-26T06:38:52.937Z",
"id": "60fe584c4ffedb5c645f1bf5",
"first_name": "Harley",
"last_name": "Smith",
"email_id": "harley@mydomain.co.uk"
}
}
Im also wondering how i can just get (for example) the first_name and last_name fields rather than everything and then style the output like:
<div>
<span class="first_name">Harley</span>
<span class="last_name">Smith</span>
</div>