2

I have created a custom API i need to know how can i get request and response using this custom API ? I use this link to create custom API : http://inchoo.net/magento/api-magento/magento-2-custom-api/

2
5

Example : Response JSON Output:

{
    "Cancelled": false,
    "MessageID": "402f481b-c420-481f-b129-7b2d8ce7cf0a",
    "Queued": false,
    "SMSError": 2,
    "SMSIncomingMessages": null,
    "Sent": false,
    "SentDateTime": "/Date(-62135578800000-0500)/"
}

If your $result variable is a string json like, you must use json_decode function to parse it as an object or array: OUTPUT

Array
(
    [Cancelled] => 
    [MessageID] => 402f481b-c420-481f-b129-7b2d8ce7cf0a
    [Queued] => 
    [SMSError] => 2
    [SMSIncomingMessages] => 
    [Sent] => 
    [SentDateTime] => /Date(-62135578800000-0500)/
)
OUTPUT

Array
(
    [Cancelled] => 
    [MessageID] => 402f481b-c420-481f-b129-7b2d8ce7cf0a
    [Queued] => 
    [SMSError] => 2
    [SMSIncomingMessages] => 
    [Sent] => 
    [SentDateTime] => /Date(-62135578800000-0500)/
)

Now you can work with $json variable as an array:

echo $json['MessageID'];
echo $json['SMSError']; 

// other stuff

=======================

Find the Below Example

Here I have created example for get product by SKU using magento 2 rest api. Here is list of API that supported by magento 2.

Magento 2 REST API Authentication

//Authentication rest API magento2.Please change url accordingly your url
$adminUrl='http://127.0.0.1/magento2/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "wsuser", "password" => "password123");                                                                    
$data_string = json_encode($data);                       
$ch = curl_init($adminUrl); 
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);
$token=  json_decode($token);

======================

Get Product By SKU REST API Magento 2

//Use above token into header

$headers = array("Authorization: Bearer $token"); 

$requestUrl='http://127.0.0.1/magento2/index.php/rest/V1/products/24-MB01';

//Please note 24-MB01 is sku

$ch = curl_init();
$ch = curl_init($requestUrl); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   

$result = curl_exec($ch);
$result=  json_decode($result);
print_r($result);
12
  • Hello @Ankit I have no idea what should i do from this. Can you please explain more about this ? It will be very helpful for me. – Ramkishan Suthar Sep 30 '16 at 5:20
  • See I updated Answer with example – Brad Hodge Sep 30 '16 at 8:30
  • Ok Ankit just one thing where should i need to add this code ? – Ramkishan Suthar Sep 30 '16 at 8:47
  • if you want to test the code you have to create one php script file on magento2 root folder & add the below code then Above code provided by me as asample code like that you can test <?php use Magento\Framework\App\Bootstrap; require DIR.'/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $obj = $bootstrap->getObjectManager(); $appState = $obj->get("Magento\Framework\App\State"); $appState->setAreaCode("developer"); – Brad Hodge Sep 30 '16 at 8:49
  • check now i updated – Brad Hodge Sep 30 '16 at 9:10

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.