本文介绍了购物睡觉接口分页链接为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况

我正在尝试调用购物睡觉接口,其中有50-250个结果,但无法从包含分页链接的cURL响应中获取链接头。

游标分页的API Documentation中的链接头示例(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("/

|

|

/", $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));

    }

}

但是,当我使用邮递员集合时,在没有链接获取截断/处理的情况下,我得到了正确的格式化响应。

我在这里尝试了StackOverflow论坛和Shopify社区提供的很多东西,但我无法像API示例或邮递员所示那样解析响应头

我的问题似乎确实与PHP代码有关,但我不是cURL方面的专家。因此,我无法进一步说明:(

另外,我无法理解为什么邮递员的信头大小写而我的信头是小写

提前感谢!

推荐答案

找到我的答案: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的角度来看毫无意义。

这篇关于购物睡觉接口分页链接为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 13:03