I would like to use cURL to get all orders using rest API. Can I use cURL instead of Oath.

I tried with cURL but returns 403 Forbidden.

I have tried with below code :

$params = array(
    "oauth_callback" => $oauthCallback,
    "oauth_consumer_key" => $oauthConsumerKey,
    "oauth_nonce" => $oauthNonce,
    "oauth_signature_method" => $oauthSignatureMethod,
    "oauth_timestamp" => $oauthTimestamp,
    "oauth_version" => $oauthVersion,

$header = "";
foreach ($params as $key=>$value){
    $header .=  $key.'="'.$value."\", ";

$header .= "oauth_signature=\"".$oauthSignature.'"';
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');  
curl_setopt($curl, CURLOPT_HTTPHEADER, array($header));
curl_setopt($curl, CURLOPT_URL, $endpointUrl);

$response = curl_exec($curl);
echo "<pre>";
  • how did you pass the credetials? Paste the code plz – Detzler May 17 '16 at 12:29
  • Hello Detzler, I have added below code. – Ankit Ahilwal May 17 '16 at 12:49

Can't event explain it better than the official magento rest documenation:


  • You are right, but I need to connect with cURL not using Oauth. With Oauth I can easily use. – Ankit Ahilwal May 17 '16 at 12:58
  • you need to follow the oauth protocol to authentificate. You can not bypass this method. If you can't use the OAuth php extension, use a php implementation like developer.github.com/v3/oauth – Detzler May 17 '16 at 13:53

Try Below code,it is working for me and return particular order detail in json format

$product_url=$url. "rest/V1/products";

//Authentication rest API magento2, get access token
$ch = curl_init();
$data = array("username" => $username, "password" => $password);
$data_string = json_encode($data);

$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
$token = curl_exec($ch);

$ch = curl_init("http://localhost/magento2/index.php/rest/V1/orders/1");
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);
echo '<pre>';print_r($result);

Hope,it will help you.

  • hey @akgola i am getting response with 404 error page. Can you help me here – Jay Momaya Apr 17 '20 at 12:46

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.