Magento 2: oAuth-based Web API calls

Once we have obtained OAuth access token, from the my previous tutorial, we can start making Web API calls to other methods.

Posted on June 23, 2017 in Magento2

cURL SOAP API call

Once we have obtained OAuth access token , from my previous tutorial Magento 2: Creating Custom Web APIS, we can start making Web API calls to other methods.
The console cURL SOAP can be called by passing Authorization: Bearer {Access Token value } into the request header. The example is as following:


curl -X POST http://magento2.local/index.php/soap/default?services=customerGroupRepositoryV1 -H 'Content-Type: application/soap+xml;charset=utf-8;action="customerGroupRepositoryV1Save"' -H 'Authorization: Bearer tjdux7ur3vu5i2q7xmd53tuh97ujacje' -d @request.xml

Where request.xml contains content as follows:


<?xml version="1.0" encodeing="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://magento2.ce/index.php/soap/default?services=customerGroupRepositoryV1">
   <env:Body>
        <ns1:customerGroupRepositoryV1SaveRequest>
            <group>
                <code>Broker</code>
                <taxClassId>3</taxClassId>
            </group> 
        </ns1:customerGroupRepositoryV1SaveRequest>
   </env:Body> 
</env:Envelope>

PHP cURL SOAP-like request call


$access_token = 'tjdux7ur3vu5i2q7xmd53tuh97ujacje';

$request = new SoapClient('http://magento2.local/index.php/soap/?wsdl&services=customerGroupRepositoryV1',
    array(
        'soap_version' => SOAP_1_2,
        'stream_context' => stream_context_create(array( 'http' => array( 'header' => 'Authorization: Bearer ' . $access_token )))
    )
);

$response = $request->customerGroupRepositoryV1Save(array('group' => array('code'=>'Broker', 'taxClassId' => 3)));

PHP cURL style for executing POST /V1/jeffSliderSlides Call

jeffSliderSlides Api has been generated by my previous tutorial. please check >>HERE


$access_token = 'tjdux7ur3vu5i2q7xmd53tuh97ujacje';

$slide = json_encode(['slide' => ['title' => 'API test']]);

$ch = curl_init('http://magento2.local/rest/V1/jeffSliderSlides');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $slide);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($slide), 'Authorization: Bearer ' . $access_token));

$result = curl_exec($ch);

var_dump($result);
curl_close($ch);

The result is like following:


string(40) "{"id":4,"slide_id":4,"title":"API test"}"

PHP cURL style for executing GET /V1/jeffSliderSlides/search Call

We can see that the getList method takes only one parameter, object instance, that complies with SearchCriteriaInterface called $searchCriteria.


<?php
$access_token = 'tjdux7ur3vu5i2q7xmd53tuh97ujacje';

$searchCriteria = '{
    "criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "title",
                        "value": "%",
                        "condition_type": "like"
                    }
                ]
            }
        ],
        "current_page": 1,
        "page_size": 10,
        "sort_orders": []
    }
}';

$searchCriteriaString = http_build_query(json_decode($searchCriteria));
echo  "\n";

$ch = curl_init('http://magento2.local/rest/V1/jeffSliderSlide/search?' . $searchCriteriaString);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token));

$result = curl_exec($ch);

$obj = json_decode($result);
var_dump($obj->items);
curl_close($ch);

The result screen shot is following


array(4) {
  [0] =>
  class stdClass#2 (1) {
    public $title =>
    string(9) "one slide"
  }
  [1] =>
  class stdClass#3 (1) {
    public $title =>
    string(9) "two slide"
  }
  [2] =>
  class stdClass#4 (1) {
    public $title =>
    string(11) "three slide"
  }
  [3] =>
  class stdClass#5 (1) {
    public $title =>
    string(8) "API test"
  }
}

PHP cURL style for executing DELETE /V1/jeffSliderSlide/:slideId Call


<?php
$access_token = 'tjdux7ur3vu5i2q7xmd53tuh97ujacje';

$slideId = 4;

$ch = curl_init('http://magento2.local/rest/V1/jeffSliderSlide/' . $slideId);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token));

$result = curl_exec($ch);

var_dump($result);

curl_close($ch);

The result should be like:



string(4) "true"

Look up all APIs list:

http://magento2.local/swagger


comments powered by Disqus