目前,我可以按照shopify的API限制(每次调用250个产品)来获取并回显。我进行了一些研究,发现我需要对商店中的产品总数[5000产品/每页250产品= 20页]进行分页。 我想在shopify中获得所有产品 所以我试图解决。 但我无法获得所有产品。 结果始终是“错误.....”。什么是问题?
$pages = ceil($products_cnt->count/250); // Count products / 250
for($i = 0; $i < $pages; $i++){
$api_url = 'https://apikey:password@store.myshopify.com';
$get_url = $api_url . '/admin/products.json?limit=250&page='.($i+1);
$products_content = @file_get_contents( $get_url );
if (!empty($products_all)) {
print_r('ok');
} else {
print_r('error.....');
}
$products_json = json_decode( $products_content, true );
$products = $products_json['products'];
答案 0 :(得分:0)
我想您对Shopify API rate limit有疑问。但是要确保需要检查Shopify API的响应。对于HTTP请求,最好使用curl或某些HTTP客户端程序包,例如Guzzle。
尝试使用以下代码代替@file_get_contents($get_url)
:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30);
$products_content = curl_exec($ch);
if(curl_errno($ch)){
print_r('Curl error.' . curl_error($ch));
}
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if(in_array($status_code, [200, 201])){
print_r('ok');
} else {
print_r(
'Shopify API error. ' .
'HTTP Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . '; '
'Error: ' . $products_content
);
}
curl_close($ch);
答案 1 :(得分:0)
您要使用的分页方法已被弃用。 Shopify从API版本2019-07引入了新的基于游标的分页。要了解有关基于游标的分页的更多信息,请前往Shopify Docs for Cursor based Pagination。最好使用一些提供速率限制等功能的PHP库。但是,使用cURL的示例实现如下所示。检查代码注释以获取详细信息。
<?php
// username and password for API
$username = "";
$password = "";
$nextPage = NULL;
$curl = curl_init();
// set result limit and Basic auth
curl_setopt_array(
$curl,
array(
CURLOPT_URL => "https://store-name.myshopify.com/admin/api/2020-07/products.json?limit=50",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_USERPWD => $username . ":" . $password
)
);
// call back function to parse Headers and get Next Page Link
curl_setopt(
$curl,
CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$nextPage) {
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
if (trim($header[0]) === "Link" && strpos($header[1], 'next') !== false) {
$links = explode(',', $header[1], 2);
$link = count($links) === 2 ? $links[1] : $links[0];
if (preg_match('/<(.*?)>/', $link, $match) === 1) $nextPage = $match[1];
}
return $len;
}
);
// First request
$response = curl_exec($curl);
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
print_r($error_msg);
}
$parsedResponse = json_decode($response);
$result = $parsedResponse->products;
// generate new requests till next page is available
while ($nextPage !== NULL) {
curl_setopt($curl, CURLOPT_URL, $nextPage);
$parsedResponse->products = [];
$nextPage = NULL;
$response = curl_exec($curl);
$parsedResponse = json_decode($response);
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
} else {
$result = array_merge($result, $parsedResponse->products);
sleep(2);
}
};
echo "Products Count: ";
echo count($result);
curl_close($curl);
Geoffrey的响应标头解析功能