Php json: Take specific values from an API in Json and display them through a php script

Asked

Viewed 195 times

1

I am working with an API and would like to "get" some values of the same as this in JSON format. At first I researched some questions similar to mine, but unfortunately I still could not find a right way to do this:

  1. Json path to get api value
  2. How to catch a JSON from a URL?
  3. Get JSON result with php
  4. How to print a specific JSON value in PHP

In case I’m using the following Curl code in php to get the data from the API:

<?php

$token = "TOKEN_PROD";

$ch = curl_init('URL_DA_API');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Content-Type: application/json',
   'Authorization: Bearer ' . $token
   ));

$data = curl_exec($ch);
$info = curl_getinfo($ch);

echo $data;

curl_close($ch);

?>

When the above script is executed, the command echo $data; displays the following values from the API:

{
   "paging":{
      "total":1,
      "limit":30,
      "offset":0
   },
   "results":[
      {
         "metadata":{
            
         },
         "corporation_id":null,
         "operation_type":"regular_payment",
         "fee_details":[
            
         ],
         "notification_url":null,
         "date_approved":null,
         "money_release_schema":null,
         "payer":{
            "entity_type":null,
            "identification":{
               
            },
            "phone":{
               
            },
            "operator_id":null,
            "last_name":null,
            "id":"300287182",
            "type":"guest",
            "first_name":null,
            "email":null
         },
         "transaction_details":{
            "total_paid_amount":10,
            "acquirer_reference":null,
            "installment_amount":0,
            "financial_institution":null,
            "net_received_amount":0,
            "overpaid_amount":0,
            "external_resource_url":"https://www.mercadopago.com/mlb/payments/ticket/helper?payment_id=10598109806&payment_method_reference_id=9640456199&caller_id=307652082&hash=c70230f6-567c-4f1f-9f40-0a43213df8f6e",
            "payable_deferral_period":null,
            "payment_method_reference_id":"9642476599",
            "verification_code":"9644359199"
         },
         "statement_descriptor":null,
         "call_for_authorize_id":null,
         "installments":1,
         "pos_id":null,
         "external_reference":null,
         "date_of_expiration":"2020-10-18T22:59:59.000-04:00",
         "charges_details":[
            
         ],
         "id":10590861806,
         "payment_type_id":"ticket",
         "barcode":{
            "content":"23798712200000010003380260964249919230973330"
         },
         "order":{
            "id":"1880577004",
            "type":"mercadopago"
         },
         "counter_currency":null,
         "brand_id":null,
         "status_detail":"pending_waiting_payment",
         "differential_pricing_id":null,
         "additional_info":{
            "ip_address":"345.24.881.02",
            "nsu_processadora":null,
            "available_balance":null
         },
         "live_mode":true,
         "marketplace_owner":null,
         "card":{
            
         },
         "integrator_id":null,
         "status":"pending",
         "transaction_amount_refunded":0,
         "transaction_amount":10,
         "description":"Plano Trimestral",
         "money_release_date":null,
         "merchant_number":null,
         "refunds":[
            
         ],
         "authorization_code":null,
         "captured":true,
         "collector_id":587663946,
         "merchant_account_id":null,
         "taxes_amount":0,
         "date_last_updated":"2020-10-15T18:27:46.000-04:00",
         "coupon_amount":0,
         "store_id":null,
         "date_created":"2020-10-15T18:27:46.000-04:00",
         "acquirer_reconciliation":[
            
         ],
         "sponsor_id":null,
         "shipping_amount":0,
         "issuer_id":null,
         "payment_method_id":"bolbradesco",
         "binary_mode":false,
         "platform_id":null,
         "deduction_schema":null,
         "processing_mode":"aggregator",
         "currency_id":"BRL",
         "shipping_cost":0
      }
   ]
}

However, according to the JSON response above the API, I would like the PHP script to only return some objects as below:

  1. id
  2. date_created
  3. total_payment_amount
  4. payment_method_id
  5. status_detail

I tried to insert the code below in the php script to get an idea of how I could solve this issue:

$json = json_decode($data);
echo $json->results->id[0];

In case how can I make the php script only display the values I mentioned above? Thanks in advance.

1 answer

1


When decoded Results is an array. So I believe it should look like this:

$json = json_decode($data);
echo $json->results[0]->id;
echo $json->results[0]->date_created;
echo $json->results[0]->transaction_details->total_paid_amount;
echo $json->results[0]->payment_method_id;
echo $json->results[0]->status_detail;

When it comes to decoded json manipulation I recommend giving a var_dump in the $json to better visualize.

  • Hello Rene, thanks for the return. It worked your script. Thank you very much. Just one last question. In case I want to use a foreach to appear all the corresponding results would look like this? ** $Decode = json_decode( $data, true ); foreach ( $Decode["id"]["date_created"] as $value){$id = $value[""];echo $id;}**... Thanks again.

  • I managed to implement it like this and it worked: $Decode = json_decode( $data, TRUE ); foreach ( $Decode["Results"] as $value){ echo 'Id:'. $value["id"]. PHP_EOL.'<br>'; echo 'Rel.:'. $value["date_created"]. PHP_EOL.'<br>'; echo 'Rel.:'. $value["status_details"]. PHP_EOL.'<br>'; echo PHP_EOL; } Valeu Renê. Thank you.

Browser other questions tagged

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