情况
我正在尝试调用 Shopify REST API,其中有超过 50-250 个结果,但我无法从包含 分页链接的 cURL 响应中获取 Link 标头。
光标分页的 API 文档中的链接标头示例 (https://shopify.dev/tutorials/make-pagulated-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 的专业人士。因此,我无法进一步:(
另外,我无法理解为什么 POSTMAN 的标头采用 正确大小写,而我的标头采用 小写
提前致谢!
找到我的答案: https://community.shopify.com/c/Shopify-APIs-SDKs/Help-with-cursor-based-paging/m-p/579640#M38946
我正在使用浏览器查看我的日志文件。因此,数据就在那里,但由于您在数据周围使用了 '<'s,因此它被隐藏了。我必须使用浏览器检查器来查看数据。不确定是谁认为这种语法是个好主意。首选是两个可以看到并且更容易解析的标头,因为使用链接语法与使用 API 无关。
我的建议是 2 个标题:
X-Shopify-Page-Next:page_info_value(如果没有更多页面则为空)
X-Shopify-Page-Perv:page_info_value(第一页为空,或者如果没有上一页)。
易于解析和使用。
但是将其作为无效的 xml 标签埋藏起来,将它们放在同一标头中并使用“rel=”语法从 API 角度来看根本没有任何意义。
需要获取标题
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = htmlspecialchars(substr($response, 0, $header_size));
这是完整的解决方案 https://miraclewebsoft.com/shopify-rest-api-pagination-link-empty/