skip to Main Content

I am currently developing application to integrate with shopify, and just tried the SDK from Shopify SDK, then I tried to make a function for get order with paginated request, for example like below

public function getAllOrders(){

// $config is the array that containing accesstoken, password and domain url of shopify store
$shopify = new ShopifySDK($config);
        $filters = [
            'limit' => 250,
            'financial_status' => 'paid',
            'fulfillment_status' => 'unfulfilled',
            'created_at_min ' => '2020-04-09',
            'created_at_max' => '2020-04-10'
        ];
        $orders_resource = $shopify->Order();
        $orders = $orders_resource->get($filters);
        $next_page = $orders_resource->getNextPageParams();
        while ($next_page) {
            $next_page_orders = $orders_resource->get($orders_resource->getNextPageParams());
            $orders = array_merge($orders, $next_page_orders);
            $next_page = $orders_resource->getNextPageParams();
        }
        return $orders;

}

But it’s give me a memory leaks, instead of being return the response it is give me PHP Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 1679360 bytes)

I have reads the advice for setup or tuning the memory limit in php.ini but i thinks it is not good at all, because it’s will force the server to take any size of its request, is there any best practice for something like this or for limit the request page or response data? any advice would be appreciate

2

Answers


  1. I have updated my code, i hope it can helps someone who has same problem like me

    public function getAllOrders(){
    
    // $config is the array that containing accesstoken, password and domain url of $shopify = new ShopifySDK($config);
            $filters = [
                'limit' => 250,
                'financial_status' => 'paid',
                'fulfillment_status' => 'unfulfilled',
                'created_at_min ' => '2020-04-10T13:00:00-07:00' //adding time specifically
            ];
            $orders_resource = $shopify->Order();
            $orders = $orders_resource->get($filters);
            $next_page = $orders_resource->getNextPageParams();
            $page = 1;
            while ($next_page) {
                if ($page > 5) { // adding condition for stop after 5 page for escaping memory exhausted
                    break;
                }else{
                    $next_page_orders = $orders_resource->get($next_page);
                    $orders = array_merge($orders, $next_page_orders);
                    $next_page = $orders_resource->getNextPageParams();
                    $page++;
                }
            }
            return $orders;
    
    }
    
    Login or Signup to reply.
  2. That is not the way to do it. Shopify no longer supports paging like that and has told everyone for many months not to do it that way. April 1 was supposed be the end of that pattern. You now get your resource with a call, and then check in the headers if there is more data to get. You use that pattern, clearly described by Shopify. So even if you got this old code going, it is soon to be useless to you. Read up on the new pattern.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search