Magento – How to call magento2 SOAP or REST API outside magento2

magento2restsoap

I am using below code to call the API but its showing error:

$request = new SoapClient(
    'http://magento2-dev.local/index.php/soap/? wsdl&services=foggylineSliderSlideRepositoryV1',
    array(
        'soap_version' => SOAP_1_2,
        'stream_context' => stream_context_create(array(
                'http' => array(
                    'header' => 'Authorization: Bearer pk8h93nq9cevaw55bohkjbp0o7kpl4d3'
                )
            )
        )
    )
);
$response = $request->foggylineSliderSlideRepositoryV1GetById(array('slideId' => 1));
var_dump($response);

Best Answer

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

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

With this code also u can get access token dynamically and try to integrate it in above code

Related Topic