【问题标题】:How to combine 2 json respinse in one hit from 2 URLs request如何在 2 个 URL 请求中一击合并 2 个 json 响应
【发布时间】:2019-03-07 14:47:29
【问题描述】:

我有来自 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 来获取这些响应,有没有其他方法可以实现这一点,可能需要先插入临时 DB 表?

编辑添加脚本。

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

}

感谢您的任何建议

【问题讨论】:

  • 您预期的 JSON 不是有效的。你不能有"a":"b":[]
  • 感谢您的输入,您能否建议如何在验证 json 中进行这项工作?我应该先记录到数据库吗?谢谢
  • 到目前为止你尝试过什么?你可以编写代码来完成你的任务
  • @Bireon 如果你尝试过,请告诉我们你做了什么
  • 请在问题中包含您的代码此处,否则您的问题可能会被版主关闭为“离题”(请参阅​​this help page 上的第 1 项) )。此外,你不想让这里的志愿者的工作比它需要的更难帮助你。我们不想转到多个单独的页面来收集所有信息,我们希望它们都在我们面前,clearly presented 并准备好使用。可能正在寻求类似解决方案的该问题的未来读者也是如此。谢谢。

标签: php json curl


【解决方案1】:

您只需以编程方式从第一个请求返回的数据中检索账单编号,然后将其放入要包含在您的第二个请求中的变量中。为此,您可以将 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
}

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

【讨论】:

  • 谢谢,我会试试的。真的很感激
  • @Bireon 嗨。你成功了吗?答案对您有帮助吗?
  • 嗨@ADyson,很抱歉回复晚了。我尝试使用您的指南,但第二次回复失败,我仍在检查任何错误或拼写错误。我会告诉你的
  • 如果您需要具体帮助,请将第二个请求的代码添加到您的问题中,并告诉我
  • 嗨@ADyson,我根据你的建议更新了我的问题,但第二个请求返回无效,我认为 billaccno 没有被第二个请求抓住:(。到目前为止尝试了很多方法,但没有运气
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
相关资源
最近更新 更多