0
votes

Currently i can fetch as per shopify's API limit of 250 products per call and echo this out. I have done some research and found that i need to paginate the request on the overall count of products [5000 products / 250 products per page = 20 pages] in the store. I want get all products in shopify so I tried to solved. but i can not get all products. the result is always 'error.....'.what is problem?

    $pages = ceil($products_cnt->count/250); // Count products / 250 
    for($i = 0; $i < $pages; $i++){
        $api_url = 'https://apikey:[email protected]';
        $get_url = $api_url . '/admin/products.json?limit=250&page='.($i+1);
        $products_content = @file_get_contents( $get_url );
        if (!empty($products_all)) {
            print_r('ok');
        } else {
            print_r('error.....');
        }
        $products_json = json_decode( $products_content, true );
        $products = $products_json['products'];
2

2 Answers

0
votes

I guess you have a problem with Shopify API rate limit. But to be sure of this need to check the response from the Shopify API. For the HTTP request better to use the curl or some HTTP client package for example the Guzzle.

Try instead of the @file_get_contents($get_url) use this code:

  $ch = curl_init();

  curl_setopt($ch,CURLOPT_URL, $url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30);

  $products_content = curl_exec($ch);

  if(curl_errno($ch)){ 
     print_r('Curl error.' . curl_error($ch));
  }
  
  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

  if(in_array($status_code, [200, 201])){
     print_r('ok');
  } else {
     print_r(
          'Shopify API error. ' .
          'HTTP Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . '; '
          'Error:     ' . $products_content
     );
  }
  curl_close($ch);
0
votes

The Pagination method you are trying to use has been deprecated. Shopify introduced new cursor based pagination from API version 2019-07. To read more about Cursor based pagination, head over to Shopify Docs for Cursor based Pagination. It is better if you use some PHP library that offers rate limiting and other things. However, a sample implementation using cURL would look something like below. Check code comments for details.

<?php
// username and password for API
$username = "";
$password = "";

$nextPage = NULL;

$curl = curl_init();

// set result limit and Basic auth

curl_setopt_array(
    $curl,
    array(
        CURLOPT_URL => "https://store-name.myshopify.com/admin/api/2020-07/products.json?limit=50",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_USERPWD => $username . ":" . $password

    )
);

// call back function to parse Headers and get Next Page Link
curl_setopt(
    $curl,
    CURLOPT_HEADERFUNCTION,
    function($curl, $header) use (&$nextPage) {
        $len = strlen($header);
        $header = explode(':', $header, 2);

        if (count($header) < 2) // ignore invalid headers
        return $len;

        if (trim($header[0]) === "Link" && strpos($header[1], 'next') !== false) {
            $links = explode(',', $header[1], 2);

            $link = count($links) === 2 ? $links[1] : $links[0];
            if (preg_match('/<(.*?)>/', $link, $match) === 1) $nextPage = $match[1];
        }

        return $len;
    }
);

// First request

$response = curl_exec($curl);

if (curl_errno($curl)) {
    $error_msg = curl_error($curl);
    print_r($error_msg);
}
$parsedResponse = json_decode($response);
$result = $parsedResponse->products;

// generate new requests till next page is available

while ($nextPage !== NULL) {
    curl_setopt($curl, CURLOPT_URL, $nextPage);
    $parsedResponse->products = [];
    $nextPage = NULL;

    $response = curl_exec($curl);
    $parsedResponse = json_decode($response);

    if (curl_errno($curl)) {
        $error_msg = curl_error($curl);
    } else {
        $result = array_merge($result, $parsedResponse->products);
        sleep(2);
    }
};
echo "Products Count: ";
echo count($result);
curl_close($curl);

Response Headers parsing function by Geoffrey