Skip to content

Instantly share code, notes, and snippets.

@AurelienLavorel
Created December 30, 2018 15:36
Show Gist options
  • Save AurelienLavorel/a81275492fbba7cd0db0d2fd8768f6ff to your computer and use it in GitHub Desktop.
Save AurelienLavorel/a81275492fbba7cd0db0d2fd8768f6ff to your computer and use it in GitHub Desktop.
Magento REST API call example
<?php
$userData = array("username" => "xxx", "password" => "xxx");
$domain = "https://www.example.com";
$orderId = "159053";
//$orderId = "?searchCriteria[filter_groups][0][filters][0][field]=entity_id&searchCriteria[filter_groups][0][filters][0][value]=150526";
$ch = curl_init("{$domain}/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_USERPWD, "test:test");
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("{$domain}/index.php/rest/V1/orders/{$orderId}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "test:test");
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment