Shopify REST API Pagination Link Empty
Asked Answered
G

2

1

Situation

I am trying to make a call to the Shopify REST API where I have more than 50-250 results but I am not able to get the Link Header from the cURL Response which contains the Pagination Links.

Sample of Link Headers from the API Documentation for Cursor-Pagination (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}"
#...

The link rel parameter does show up, but the Link is empty as below.

This is as per my PHP Code

My Shopify Call function

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));

    }
    
}

But when I use a POSTMAN Collection, I get a proper formatted response without the Link getting truncated/processed.

This is the Header when I use a Postman Collection

I have tried a lot of things here available via the StackOverflow Forums as well as Shopify Community, but I'm unable to parse the Response Header the same way as shown by API Examples or POSTMAN

My issue does seem to be with the PHP Code, but I'm not a pro with cURL. Thus, I'm not able to make it further :(

Also, I'm not able to understand why POSTMAN's Headers are in Proper Case whereas mine are in Lower Case

Thanks in Advance!

Goddamned answered 29/6, 2021 at 1:2 Comment(2)
if I use var_dump($result) from the curl call, the "link" header shows a string of 300+ length which clearly means the error is in parsing/processing.Goddamned
Hi there, Any luck for this? I am getting the same issueElvaelvah
G
8

Found my answer : https://community.shopify.com/c/Shopify-APIs-SDKs/Help-with-cursor-based-paging/m-p/579640#M38946

I was using a browser to view my log files. So the data is there but it's hidden because of your use of '<'s around the data. I had to use the browser inspector to see the data. Not sure who decided this syntax was a good idea. Preference would be two headers that one can see and more easily parse since using link syntax is not relative to using an API.

My suggestion would be 2 headers:

X-Shopify-Page-Next: page_info_value (empty if no more pages)

X-Shopify-Page-Perv: page_info_value (empty on first page or if there is no previous page).

Easy to parse and use.

But having this buried as an invalid xml tag, having them both in the same header and using 'rel=' syntax makes no sense at all from an API perspective.

Goddamned answered 29/6, 2021 at 1:49 Comment(0)
E
0

Need to get header like

$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = htmlspecialchars(substr($response, 0, $header_size));

Here is complete solution https://miraclewebsoft.com/shopify-rest-api-pagination-link-empty/

Elvaelvah answered 15/11, 2023 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.