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',
        'soap_version' => SOAP_1_2,
        'stream_context' => stream_context_create(array(
                'http' => array(
                    'header' => 'Authorization: Bearer pk8h93nq9cevaw55bohkjbp0o7kpl4d3'
$response = $request->foggylineSliderSlideRepositoryV1GetById(array('slideId' => 1));
  • What is you error ? Jun 27, 2017 at 12:10

4 Answers 4

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


Here is demo for curl for rest using token.
Token you need to generate from admin System->Integration and configure in code below.
For demo purpose I have made below rest default customer call.

ini_set('display_errors', 1);

/*Token Generated from SYstem Integration*/

$token = '4hmpioj8qvtgh1d06ujgg52a52yqbmye' (This is token generated from system integration);
$rest_url = 'http://localhost/magentoce27/index.php/rest/V1/customers/1';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$rest_url);

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$token;

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec ($curl);

echo "<pre>"; print_r($result);
  • I am trying to create integration its generating Consumer Key, Consumer Secret but Access Token field showing empty.
    – Hari
    Jun 29, 2016 at 7:30
  • This code will allow you to generate dynamic access token.
    – Rushvi
    Jun 29, 2016 at 9:05
  • It gave : {"message":"No such entity with %fieldName = %fieldValue","parameters":{"fieldName":"customerId","fieldValue":1}} Aug 24, 2016 at 11:49

To call SOAP API outside Magento 2:

step1: app/code/Vendor/Module/Model/Product.php

namespace Vendor\Module\Model;
use Vendor\Module\Api\ProductInterface;

class Product extends \Magento\Framework\Model\AbstractModel implements ProductInterface{

    public function __construct( 

    * @param int|null $store.
    * @return string options in a json format. 
    public function addTemplateProducts($store){
        return 'store:'.$store;//.json_encode($arg_list);

step:2 app/code/Vendor/Module/Api/ProductInterface.php


namespace Vendor\Module\Api;

interface ProductInterface
    * @param int|null $store.
    * @return string options in a json format. 
    public function addTemplateProducts($store);

step3: app/code/Vendor/Module/etc/webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
    <route url="/V1/product/addTemplateProducts" method="POST">
        <service class="Vendor\Module\Api\ProductInterface" method="addTemplateProducts"/>
            <resource ref="anonymous"/>

step4: your-magento-base-url.com/soapTest.php

    error_reporting(E_ALL & ~E_NOTICE);//ini_set('display_errors',1);//error_reporting(0);

    $token = 'tf4ti4s8jjbt7jtpaxh9k2ex8txdf2vs';// Your Integration access token

    ini_set("soap.wsdl_cache_enabled", 0);

    $opts = array('http' => array('header' => "Authorization: Bearer " . $token));
    $context = stream_context_create($opts);

    $wsdlUrl = BASEPATH."soap?wsdl&services=vendorModuleProductV1";
    $param = array('store'=>2);
        $soapClient = new \Zend\Soap\Client($wsdlUrl);
        $soapClient->setSoapVersion(SOAP_1_2);//SOAP_1_1 or SOAP_1_2

        $soapResponse = $soapClient->vendorModuleProductV1MethodName($param);
    }catch(Exception $e){
        //echo 'Error : '.$e->getMessage();
        //echo '<br />'.$e->getTraceAsString();
        echo '<br />'.$e->__toString();
        //echo '<br />'.$e->getFile();echo '<br />'.$e->getLine();

Now run your-magento-base-url.com/soapTest.php in the browser and you will see

stdClass Object
    [result] => store:2

I'll update this answer regarding REST call outside Magento 2 soon.

The above worked fine for me. So check and let me know.


What error does it show?

The url pattern for the soap calls should be something like this


You can get a list of available services like this

  • it is showing below error: Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'magento2-dev.local/index.php/soap? wsdl&services=foggylineSliderSlideRepositoryV1' : failed to load external entity "magento2-dev.local/index.php/soap/…" in /vagrant/pub/index/index.php:21 Stack trace: #0 /vagrant/pub/index/index.php(21): SoapClient->SoapClient('magento2...', Array) #1 {main} thrown in /vagrant/pub/index/index.php on line 21
    – Hari
    Jun 29, 2016 at 7:09
  • I think it might be the /default? part being missing from the url.
    – DanCarlyon
    Jun 29, 2016 at 7:14

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.