Yrtymd
Yrtymd

Reputation: 433

parse data from JSON callback using PHP

I receive a callback data from payment site and try to save some values into txt file

Getting an array:

$json = file_get_contents('php://input');

$obj = json_decode($json,true);

Saving all data to txt:

file_put_contents('robot.php', var_export($obj, true) . "\r\n\r\n", FILE_APPEND | LOCK_EX);

The result from txt:

array (
  'rrn' => '',
  'masked_card' => '444455XXXXXX1111',
  'sender_cell_phone' => '',
  'response_status' => 'success',
  'sender_account' => '',
  'fee' => '',
  'rectoken_lifetime' => '',
  'reversal_amount' => '0',
  'settlement_amount' => '0',
  'actual_amount' => '370000',
  'order_status' => 'approved',
  'response_description' => '',
  'verification_status' => '',
  'order_time' => '04.01.2018 21:53:47',
  'actual_currency' => 'JPY',
  'order_id' => 1515095627',
  'parent_order_id' => '',
  'merchant_data' => '[{"name":"custom-field-0","label":"fist_name","value":"john_doe"},{"name":"custom-field-2","label":"phone","value":"07777777777"}]',
  'tran_type' => 'purchase',
  'eci' => '5',
  'settlement_date' => '',
  'payment_system' => 'card',
  'rectoken' => '',
  'approval_code' => '56783909',
  'merchant_id' => 14066776208,
  'settlement_currency' => '',
  'payment_id' => 7487818gg3,
  'product_id' => '',
  'amount' => '370000',
  'sender_email' => '[email protected]',
)

How to save in txt separate values from nested array 'merchant_data' - first name and phone?

Upvotes: 0

Views: 783

Answers (1)

Vladimir
Vladimir

Reputation: 1391

You can make new array and save it to file:

$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$save = [];
foreach ($obj['merchant_data'] as $data){
    $save[] = [
        'name' => $data['first_name'],
        'phone' => $data['phone'],
    ];
}
file_put_contents('robot.php', var_export($save, true) . "\r\n\r\n", FILE_APPEND | LOCK_EX);

Upvotes: 2

Related Questions