首页 > 解决方案 > 如何从 2 个 URL 请求中一次性组合 2 个 json 响应

问题描述

我有来自 2 个不同 url 的 2 个 json 响应,如果用户使用 post 方法填写 customernumber 以获取 billNo 值,则第一步是:

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

第二个到另一个 url 用户使用 post 方法在表单中填写 billNo(从上面的第一步获得),以获取详细信息结果响应,如下所示:

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

我的问题是可以将此结果与仅使用第一步仅填写客户编号相结合吗?在预期的结果是:

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

我正在使用 Curl 和 PHP 来获取这些响应,有没有其他方法可以实现这一点,可能需要先插入临时数据库表?

编辑添加脚本。

<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;
}

更新脚本:

<?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);
}

}

感谢您的任何建议

标签: phpjsoncurl

解决方案


您只需要以编程方式从第一个请求返回的数据中检索 Bill No,然后将其放入要包含在您的第二个请求中的变量中。为此,您可以将 JSON 数据解码为 PHP 对象,然后从中提取正确的字段。

我假设您要么只从第一个请求中获得一个客户,要么您只对返回的第一个客户记录感兴趣。如果不是这种情况,您将不得不相应地修改代码。

此外,您还没有提供用于运行第二个请求的代码,所以我必须让您$billno在该请求中以合适的方式使用该变量。

更改发生在else代码底部的语句中,我们不只是回显原始响应,而是对其进行解码并从中获取 Bill No。

<?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
}

PS 我还更改了您的输入 JSON 数据,以便使用 json_encode 可靠地创建。手动构建 JSON,即使是像这样的简单字符串,也可能由于意外的语法错误、拼写错误等而失败 - 而是使用提供的工具将具有已知良好结构的 PHP 变量转换为可靠的有效 JSON 字符串,可重复的方式。


推荐阅读