如何在同一PHP脚本中访问2个api端点,并使用第1个的结果从第2个获得的结果

问题描述 投票:0回答:1

我需要从api端点获取每日发票号,然后使用这些发票号来获取其他发票来宾信息。

此代码访问API,并返回3/8/20的已付款发票编号

$curl = curl_init();
curl_setopt_array($curl, array(
 CURLOPT_URL => "https://api.example.com/api/4.0/invoices?startMin=2020-03-07T00:00:00&endMax=2020-03-08T00:00:00&statusId=PAID",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "GET",
 CURLOPT_HTTPHEADER => array(
   "Accept: /",
   "Accept-Encoding: gzip, deflate",
   "Content-Type: application/json",
   "Authorization: Basic YOUCANTHAVEIT",
   "Cache-Control: no-cache",
   "Connection: keep-alive",
   "Host: api.example.com",
   "cache-control: no-cache"
 ),
));
$response = curl_exec($curl);
$err = curl_error($curl);

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

$json = json_decode($response, true);
$json = array_values($json['data']);

$codes = array();

foreach ($json as $BKcodes) {

        $codes[] = $BKcodes['code'];

}

curl_close($curl);
}

我需要获取每个发票编号并传递到第二个端点,以从预订发票中返回其他客人详细信息。我已经更改了上面的代码,并添加了另外的$ curl2 = curl_init();使用相同的连接信息将CURLOPT_URL更改为

CURLOPT_URL => "https://api.example.com/api/4.0/bookings/".$BKcodes['code']."/guests",

此方法有效,但只有40张左右的发票却非常缓慢。

更好的方法的任何方向将不胜感激

php curl
1个回答
0
投票

添加中

© www.soinside.com 2019 - 2024. All rights reserved.