我们正在为客户编程一个 WooCommerce 网上商店。目标是从客户的新 ERP 系统同步产品。 ERP系统目前也在开发中,并提供JSON接口用于测试。产品导入 JSON 目前包含 5000 多种具有多种变体的产品。
我尝试向邮递员提出请求进行测试。请求花费了较长时间(约8分钟),但成功了。响应 JSON 文件的大小约为。 2 MB。
现在我尝试使用 PHP 执行请求,但它不起作用。 1小时后(max_execution_time = 3600)在服务器协议中,我收到以下错误消息:“18454#0:*1484295上游超时(110:连接超时),同时从上游读取响应标头”
我设置了以下 PHP 设置:
memory_limit = 256M
post_max_size = 256M
upload_max_filesize = 256M
max_execution_time = 3600
max_input_time = 3600
cURL 请求的 PHP 代码如下所示:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.anqodo.com:8088/anqDataSvc/api/TofererExternal/webshopproducts?ws_id=' . $webshop_id . '&date_from=' . $date_from, // the date_from parameter can be set to limit the products to all edited products since this date. If i limit the date (eg 2024-01-01) the request works, because the response is smaller. But for the initial import i need all products
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 3600,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'RequestTimeClient: ' . date("Y-m-d\TH:i:s\Z"),
'Content-Type: application/json',
'Authorization: Bearer ' . $token // i retrieve the token in another function, wich works fine
),
));
$response = json_decode(curl_exec($curl));
curl_close($curl);
return $response;
我对此类界面没有太多经验,这就是我需要帮助的原因。我真的不知道是什么原因导致了这个问题。这种 JSON 响应的大小有限制吗?为什么它可以与 Postman 一起使用而不能与 PHP 一起使用?解决这个问题的最佳方法是什么?
我尝试向 Postman 提出请求,成功了。
此后,我尝试在我的 Wordpress 插件中以及根路径中的“正常”PHP 文件中发出此请求,以排除 Wordpress 可能出现的问题,但这不起作用。
如前所述,我还尝试设置“date_from”参数,以限制对过去两周左右编辑的所有产品的响应。这非常有效。
function fetchProducts($webshop_id, $date_from, $token) {
$batchSize = 100; // Number of products to fetch in each batch
$currentPage = 1;
$allProducts = [];
do {
$start = ($currentPage - 1) * $batchSize;
$url = 'https://api.anqodo.com:8088/anqDataSvc/api/TofererExternal/webshopproducts?ws_id=' . $webshop_id . '&date_from=' . $date_from . '&start=' . $start . '&limit=' . $batchSize;
$response = fetchBatch($url, $token);
if (!$response || empty($response->products)) {
// Handle error or break the loop if no more products
break;
}
// Merge products from the current batch into the result array
$allProducts = array_merge($allProducts, $response->products);
$currentPage++;
} while (count($response->products) == $batchSize); // Continue fetching until fewer products are returned than the batch size
return $allProducts;
}
function fetchBatch($url, $token) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 3600,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'RequestTimeClient: ' . date("Y-m-d\TH:i:s\Z"),
'Content-Type: application/json',
'Authorization: Bearer ' . $token
),
));
$response = json_decode(curl_exec($curl));
curl_close($curl);
return $response;
}
// Example usage:
$webshop_id = 123;
$date_from = '2024-01-01';
$token = 'your_access_token';
$products = fetchProducts($webshop_id, $date_from, $token);
批量处理: fetchProducts 函数通过在网址中设置起点和限制来获取较小组(批量)的产品。批量大小最初为 100,但可以根据您的要求进行更改。
循环批次: do-while 循环继续获取批次,直到收到的产品数量小于设置的批次大小。
合并结果: 每批次的产品都合并到 $allProducts 集合中。
独立功能: fetchBatch 函数处理对 ERP 系统的单个请求。
错误处理: 正确处理错误至关重要。如果出现问题或没有更多产品被退回,循环就会停止。
通过使用批处理,您可以更有效地处理大型数据集并降低遇到执行时间限制的风险。根据服务器的功能和 ERP 系统的响应时间调整批量大小。