我有来自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);
}
}
感谢任何建议
答案 0 :(得分:1)
您只需要从第一个请求返回的数据中以编程方式检索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
}
P.S。我还更改了您使用json_encode可靠创建的输入JSON数据。手工构建JSON(即使是像这样的简单字符串)也可能由于意外的语法错误,拼写错误等而导致失败-而是使用提供的工具将具有已知结构的PHP变量可靠地转换为有效的JSON字符串。 ,可重复的方式。