Bireon
Bireon

Reputation: 189

How to combine 2 json respinse in one hit from 2 URLs request

I have 2 json response from 2 different urls, the first step one if user fill the customernumber with post method to get the billNo value as below:

{  
   "Customer":[  
      { 
         "billNo":"1001337"
      }
   ],
}

The second one to another url user fill the billNo (got from the first step above) in a form with post method to get details result response like this:

{  
   "Billing":[  
      { 
         "billAccName":"John Doe",
         "billAccStatus":"Active"
      }
   ],
}

My question is it possible to combine this result with only using first step only fill up the customernumber? Within the expected result is:

{  
       "Billing":[  
          { 
             "billAccName":"John Doe",
             "billAccStatus":"Active"
          }
       ],
    }

I am using Curl with PHP to get those responses, is there any other way to achieve this, maybe need to insert to the temp DB table first?

Edited add the script.

<form class="form-response" method="POST" action="postform.php">
    <h2 class="form-response-heading">get Response</h2>
    <input name="customernumber" class="form-control" type="text" autofocus="" required="" placeholder="phonenumber"customernumber">
    <button class="btn btn-lg btn-primary btn-block" type="submit">Get Response</button>
    </form>



<?php
$customernumber = $_POST['customernumber'];
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.example.com/AccountDetails",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS   => "{ \"customernumber\":\"" .$customernumber . "\"}",
 CURLOPT_HTTPHEADER => array(
    "accept: application/json;charset=UTF8",
    "api-key: myapikeynumbers",
    "cache-control: no-cache",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Update Script:

<?php
$phone = $_POST['customernumber'];
$curl = curl_init();
$data = array("customernumber" => $phone);

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.example.com/AccountDetails",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS   =>  json_encode($data),
 CURLOPT_HTTPHEADER => array(
    "accept: application/json;charset=UTF8",
    "api-key: myapikey",
    "cache-control: no-cache",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  $result = json_decode($response);
  $billno = $result->Customer[0]->billNo;
  //now here, make your second API call using the Bill Number retrieved from the response

  $billAccNo = $_POST['billNo'];



$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.example.com/getBillingDetails",

  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{ \"billAccNo\":" .$billAccNo . "}",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json;charset=UTF8",
    "api-key: myapike",
    "cache-control: no-cache",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);



$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo header('Content-Type: application/json');
  echo json_encode($response, JSON_PRETTY_PRINT);
}

}

Thanks for any suggestions

Upvotes: 1

Views: 263

Answers (1)

ADyson
ADyson

Reputation: 61914

You just need to programatically retrieve the Bill No from the data returned by the first request, and then put it in a variable to be included in your second request. You can do that by decoding the JSON data into a PHP object and then extracting the correct field from it.

I've assumed that you either only ever get one Customer back from the first request, or that you only ever are interested in the first Customer record returned. If that's not the case you'll have to modify the code accordingly.

Also, you haven't provided the code used to run the second request, so I'll have to leave it to you to use the $billno variable in a suitable way in that request.

The change occurs in the else statement at the bottom of the code, where instead of just echoing the raw response we instead decode it and take the Bill No from it.

<?php
$phone = $_POST['customernumber'];
$curl = curl_init();
$data = array("customernumber" => $phone);

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.example.com/AccountDetails",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS   =>  json_encode($data),
 CURLOPT_HTTPHEADER => array(
    "accept: application/json;charset=UTF8",
    "api-key: myapikeynumbers",
    "cache-control: no-cache",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  $result = json_decode($response);
  $billno = $result->Customer[0]->billNo;
  //now here, make your second API call using the Bill Number retrieved from the response
}

P.S. I also changed your input JSON data to be created reliably using json_encode. Building JSON by hand, even a simple string like this, is potentially prone to failure due to unexpected syntax errors, typos etc - instead used the tools provided to convert a PHP variable with a known-good structure into a valid JSON string in a reliable, repeatable way.

Upvotes: 1

Related Questions