Reputation: 123
I'm building a web api and i need the prestashop's products for it to work.
The thing is that there are over 80000 active products and that is really painfull.
So i'm trying to filter products that have been updated the last couple days in order to minimize the request load time.
the code i have so far is this
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://'.PS_WS_KEY.'@'.PS_WS_URL.'/api/products/?filter[date_upd]=[2021-01-01 00:00:00, 2021-04-07 00:00:00]&date=1&output_format=JSON',
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',
));
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
The above cURL is returning an empty array. The other issue i have is that i can not filter specific fields when i get all products, i.e. id, name, price, description etc. Instead i get the id only.
Prestashop version 1.7.3, PHP 7.4
Upvotes: 0
Views: 1137
Reputation: 19
If anyone still struggling with it. Here is the URL for the request to get order from two dates. The issue is the url encode for filter parameter.
Below is an working exanple of the PHP cURL to get orders of today date.
// PrestaShop API endpoint (replace with your store's domain)
$prestashop_url = 'https://YOURSHOPURLHERE.com/api/';
// Your PrestaShop API key
$api_key = 'Your API Key';
// Get today's date in the format PrestaShop uses
$today = date('Y-m-d'); // As of today the date will be like '2025-01-06'
// Prepare the URL for fetching orders created today
$order_url = $prestashop_url . 'orders?filter[date_add]=[' . urlencode($today . ' 00:00:00') . ',' . urlencode($today . ' 23:59:59') . ']&date=1&output_format=JSON';
// cURL setup for fetching order IDs
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $order_url); // URL to the orders endpoint
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic ' . base64_encode($api_key . ':')
));
// Execute the cURL request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
exit;
}
// Close the cURL session
curl_close($ch);
Upvotes: 0
Reputation: 170
In my case, removing the time part of the date time used as filter solve the problem.
Something like this: 'https://'.PS_WS_KEY.'@'.PS_WS_URL.'/api/products/?filter[date_upd]=[2021-01-01|2021-04-07]&date=1&output_format=JSON'
.
UPDATE: The problem is maybe caused by the space between the date and the time. So you need to encode the query before sending the request.
Upvotes: 0
Reputation: 948
To get all fields you must specify display=full, for example:
'https://'.PS_WS_KEY.'@'.PS_WS_URL.'/api/products/?display=full
When cURL responses empty erray, is status code 200? Try:
'https://'.PS_WS_KEY.'@'.PS_WS_URL.'/api/products/?filter[date_upd]=[2021-01-01 00:00:00|2021-04-07 00:00:00]&date=1&output_format=JSON'
Upvotes: 0