-1

I have tried getting a response from my request but it didn't work.

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://sandbox.monnify.com/api/v1/bank-transfer/reserved-accounts",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS =>"{\n    \"accountReference\": \"0111443-JP098\",\n    \"accountName\": \"Joe Philips\",\n    \"currencyCode\": \"NGN\",\n    \"contractCode\": \"6146592431\",\n    \"customerEmail\": \"jp@tester.com\",\n    \"customerName\": \"Joe Philips\",\n    \"incomeSplitConfig\": [\n        {\n            \"subAccountCode\": \"MFY_SUB_275274693326\",\n            \"feePercentage\": 10.5,\n            \"splitPercentage\": 20,\n            \"feeBearer\": true\n        }\n    ]\n}",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibW9ubmlmeS1wYXltZW50LWVuZ2luZSJdLCJzY29wZSI6WyJwcm9maWxlIl0sImV4cCI6MTU5NzI2MzAyNCwiYXV0aG9yaXRpZXMiOlsiTVBFX01BTkFHRV9MSU1JVF9QUk9GSUxFIiwiTVBFX1VQREFURV9SRVNFUlZFRF9BQ0NPVU5UIiwiTVBFX0lOSVRJQUxJWkVfUEFZTUVOVCIsIk1QRV9SRVNFUlZFX0FDQ09VTlQiLCJNUEVfQ0FOX1JFVFJJRVZFX1RSQU5TQUNUSU9OIiwiTVBFX1JFVFJJRVZFX1JFU0VSVkVEX0FDQ09VTlQiLCJNUEVfREVMRVRFX1JFU0VSVkVEX0FDQ09VTlQiLCJNUEVfUkVUUklFVkVfUkVTRVJWRURfQUNDT1VOVF9UUkFOU0FDVElPTlMiXSwianRpIjoiYTQ5YjIxNDgtNTJkMy00ZGI1LTg2NGYtYzdiM2NjM2M4NzUzIiwiY2xpZW50X2lkIjoiTUtfVEVTVF9DSFZRRlJBN1NHIn0.mqwi5y7wnXpBCk6R9dC3ORhf9pNkwHVOCJr2SHUfk9TFYpVMnGuBUVyxJFOwLHROyKVodquPr1eS2AT1nTUDCrW0YXlX9tX5BPrfckvDoPPza7Klc8uQrw1aVxF6sAK-hFZgC79lKOq9gowOqWP1frbJ5BqozZfYiQ6ZsZcf2LubDOoen_G6_13wGtCM58-9BcY6aMKv--Vxr0AFwSqujBMny1D-x2SgsqT98asoYvtaHGtiC4MbVg-jFwwJuG4BYststO0k1J0YI5frpyLyQfaNEJSR6Y-WJiCqFWIDpHgDFINl65xtMIE_15OV2BKfBsruCo5mkx_rHPH_4_tXWw"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

These is the request code in curl, and below is the response

    {
  "requestSuccessful": true,
  "responseMessage": "success",
  "responseCode": "0",
  "responseBody": {
    "contractCode": "6146592431",
    "accountReference": "abc123",
    "accountName": "Test Reserved Account",
    "currencyCode": "NGN",
    "customerEmail": "test@tester.com",
    "accountNumber": "3000004533",
    "bankName": "Providus Bank",
    "bankCode": "101",
    "collectionChannel": "RESERVED_ACCOUNT",
    "reservationReference": "W3HP7E6VZP78TUKV4U9E",
    "reservedAccountType": "GENERAL",
    "status": "ACTIVE",
    "createdOn": "2020-08-12 18:56:49.0",
    "contract": {
      "name": "Default Contract",
      "code": "6146592431",
      "description": null,
      "supportsAdvancedSettlementAccountSelection": false,
      "sweepToExternalAccount": false
    },
    "transactionCount": 0,
    "restrictPaymentSource": false
  }
}

In all of the response body, I need just the following response;

"accountNumber": "3000004533",
"customerEmail": "test@tester.com",
"bankName": "Providus Bank",

Please, how do I get this done.

Michael
  • 90
  • 7
  • Response contains `"requestSuccessful": true` together with additional data, what makes you think it didn't work? – Álvaro González Sep 26 '20 at 13:31
  • Oh.. sorry, I guess I didn't come out clear. What I actually need is to get out the response body mentioned above out of the whole response – Michael Sep 26 '20 at 13:36

1 Answers1

0

Parse json string to PHP object or array and then you get it. Get your info to $rp variable

...
$response = curl_exec($curl);

$obj = json_decode($response, true);

$i = 0;
$nw= array_filter($obj['responseBody'], function($v,$k){
  return in_array($k, ['accountNumber', 'customerEmail', 'bankName']);
}, ARRAY_FILTER_USE_BOTH);
$i=0;
foreach($nw as $k => $v){
  $last = $i !== count($nw)-1 ? ','.PHP_EOL : '';
  echo "\"$k\": \"$v\"".$last;
  $i++;
}


curl_close($curl);

EDIT: Just show the result in the

Pascal Tovohery
  • 888
  • 7
  • 19
  • Hello, thanks for helping out. I really appreciate... However, it didn't work. Still getting the same response as the one I have been getting – Michael Sep 26 '20 at 13:59
  • @Michael what is the output ? – Pascal Tovohery Sep 26 '20 at 14:03
  • @Michael i've edit my answer and you can see exaclty the output in server – Pascal Tovohery Sep 26 '20 at 14:34
  • Thanks a bunch. This is what I got; (1) Invalid argument supplied for foreach() in... (2) array_filter() expects parameter 1 to be array – Michael Sep 26 '20 at 14:44
  • I tweaked the first code you gave me and It gave me the values.. but all on the same line. Here's the code. $response = curl_exec($curl); $objs = json_decode($response, true); $rp = $objs['responseBody']; echo $rp['accountNumber']; echo $rp['accountName']; echo $rp['bankName']; curl_close($curl); – Michael Sep 26 '20 at 15:03