php - Shopify REST API 分页链接为空
问题描述
情况
我正在尝试调用Shopify REST API,其中我有超过 50-250 个结果,但我无法从包含分页链接的 cURL 响应中获取链接标头。
光标分页API 文档中的链接标头示例( https://shopify.dev/tutorials/make-paginated-requests-to-rest-admin-api )
#...
Link: "<https://{shop}.myshopify.com/admin/api/{version}/products.json?page_info={page_info}&limit={limit}>; rel={next}, <https://{shop}.myshopify.com/admin/api/{version}/products.json?page_info={page_info}&limit={limit}>; rel={previous}"
#...
确实显示了链接rel参数,但链接为空,如下所示。
我的 Shopify 通话功能
function shopify_call($token, $shop, $api_endpoint, $query = array(), $method = 'GET', $request_headers = array()) {
// Build URL
$url = "https://" . $shop . ".myshopify.com" . $api_endpoint;
if (!is_null($query) && in_array($method, array('GET', 'DELETE'))) $url = $url . "?" . http_build_query($query);
$headers = [];
// Configure cURL
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
// this function is called by curl for each header received
curl_setopt($curl, CURLOPT_HEADERFUNCTION,
function($ch, $header) use (&$headers)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$headers[trim($header[0])] = trim($header[1]);
return $len;
}
);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);
// curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_USERAGENT, 'Sphyx App v.1');
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl,CURLOPT_ENCODING,'');
// Setup headers
$request_headers[] = "";
if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
$request_headers[] = 'Accept: */*'; // Copied from POSTMAN
$request_headers[] = 'Accept-Encoding: gzip, deflate, br'; // Copied from POSTMAN
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
if ($method !== 'GET' && in_array($method, array('POST', 'PUT'))) {
if (is_array($query)) $query = http_build_query($query);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $query);
}
// Send request to Shopify and capture any errors
$result = curl_exec($curl);
$response = preg_split("/\r\n\r\n|\n\n|\r\r/", $result, 2);
$error_number = curl_errno($curl);
$error_message = curl_error($curl);
// Close cURL to be nice
curl_close($curl);
// Return an error is cURL has a problem
if ($error_number) {
return $error_message;
} else {
// Return headers and Shopify's response
return array('headers' => $headers, 'response' => json_decode($response[1],true));
}
}
但是当我使用POSTMAN Collection时,我得到了正确格式化的响应,而Link没有被截断/处理。
我已经通过 StackOverflow 论坛和 Shopify 社区尝试了很多可用的东西,但是我无法像 API 示例或 POSTMAN 所示的那样解析响应标头
我的问题似乎与 PHP 代码有关,但我不是 cURL 的专家。因此,我无法更进一步:(
另外,我不明白为什么邮递员的标题是正确的,而我的是小写的
提前致谢!
解决方案
找到我的答案: https ://community.shopify.com/c/Shopify-APIs-SDKs/Help-with-cursor-based-paging/mp/579640#M38946
我正在使用浏览器查看我的日志文件。所以数据在那里,但由于您在数据周围使用了“<” ,所以它被隐藏了。我不得不使用浏览器检查器来查看数据。不确定是谁决定这种语法是个好主意。首选是两个可以看到并且更容易解析的标头,因为使用链接语法与使用 API 无关。
我的建议是 2 个标题:
X-Shopify-Page-Next:page_info_value(如果没有更多页面,则为空)
X-Shopify-Page-Perv:page_info_value (第一页为空或没有前一页)。
易于解析和使用。
但是将其作为无效的 xml 标记隐藏,将它们放在同一个标头中并使用“rel=”语法从 API 的角度来看根本没有意义。
推荐阅读
- python - 应该将哪些 URL 列入白名单以使用 conda 和 pip?
- sql - 在 sql 中存储项目分布
- reactjs - 如何使用 useRef() 进行验证
- python - 在 django 中编辑和更新用户模型
- java - JComboBox 错误阻止另一个字段显示
- postgresql - 带有子查询的 postgres case 语句
- ruby-on-rails - 如何修复 Rails 生产服务器中的 504 nginx 网关超时?
- rust - 克隆请求响应
- django - 通过侧边栏菜单项调用此表单时,从数据库模型填充 Django 表单字段标签
- angular - 在angular/typescript中根据id新建数组