首页 > 解决方案 > 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参数,但链接为空,如下所示。

这是根据我的 PHP 代码

我的 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 的专家。因此,我无法更进一步:(

另外,我不明白为什么邮递员的标题是正确的,而我的是小写的

提前致谢!

标签: phpcurlshopifyphp-curlshopify-api

解决方案


找到我的答案: 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 的角度来看根本没有意义。


推荐阅读