I'm getting crazy with PHP curl and query string against Magento 2 REST Api. I have the following query:


coping and pasting it in the browser it works fine. If I use PHP and curl like in this example:

$curl = curl_init("http://mystore/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=10");
curl_setopt_array($curl, [
return curl_exec($curl);

Magento return an "Invalid signature" error. I found the problem, is the encoding of "&" character, but I tried using urlencode() or htmlentities(), I cannot find a solution. It works only substituting "&" with "& amp;" (without space), but the query is not correctly interpreted. Which is the correct way to send the query string using PHP curl?


Try following way:

$userData = array("username" => "admin", "password" => "admin123");
$ch = curl_init("http://mystore/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

$token = curl_exec($ch);

$ch = curl_init("http://mystore/index.php/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=10");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

$result = curl_exec($ch);

$result = json_decode($result, 1);
| improve this answer | |
  • Thank you @sohel-rana, your example works fine. But doing the same thing, using OAuth instead of session based authentication, I obtain the "invalid signature" error. Do you have the same example with OAuth? – salgua Oct 3 '16 at 8:16
  • 'invalid signature' means you authentication is wrong. Check your authentication information. – Sohel Rana Oct 3 '16 at 8:30
  • Here is an example for oAuth: inchoo.net/magento-2/magento-2-api – Sohel Rana Oct 3 '16 at 8:32
  • 1
    I used the inchoo example, and it works fine. The problem is with query string. Without query string it works – salgua Oct 3 '16 at 10:03
  • Amazing this actually works, I've seen so many other similar examples that didnt so thanks so much ! – AdamJones Dec 13 '18 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.