0

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?

3
  • You only show what you have. Edit the post and include an example of what you need. Commented Jan 5, 2018 at 8:07
  • 2
    to save in txt separate what do you mean? Commented Jan 5, 2018 at 8:07
  • 1
    Use json_decode once more - $obj = json_decode($json,true); $obj['merchant_data'] = json_decode($obj['merchant_data'], true); Commented Jan 5, 2018 at 8:12

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

$obj['merchant_data'] - invalid argument supplied for foreach()
try use var_dump($obj['merchant_data']); before loop. We need to check type of $obj['merchant_data'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.