0

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>";
print_r($response);
curl_close($curl);
2
  • how did you pass the credetials? Paste the code plz
    – Detzler
    Commented May 17, 2016 at 12:29
  • Hello Detzler, I have added below code. Commented May 17, 2016 at 12:49

2 Answers 2

0

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

http://devdocs.magento.com/guides/m1x/api/rest/introduction.html#RESTAPIIntroduction-PHPExamples

2
  • You are right, but I need to connect with cURL not using Oauth. With Oauth I can easily use. Commented May 17, 2016 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
    Commented May 17, 2016 at 13:53
0

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

$url="http://localhost/magento2/index.php/";
$token_url=$url."rest/V1/integration/admin/token";
$product_url=$url. "rest/V1/products";
$username="admin";
$password="admin@123";


//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.

3
  • hey @akgola i am getting response with 404 error page. Can you help me here
    – Jay Momaya
    Commented Apr 17, 2020 at 12:46
  • @JayMomaya hope you have replaced the $url, $username and $password values and tried?
    – Thamo
    Commented Jun 23, 2021 at 11:09
  • It's been more than 1 year now. I don't remember anything now. @Thamo
    – Jay Momaya
    Commented Jun 24, 2021 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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