I tried the below code though I am not sure whether these are the required set of scripts, but it didn’t work and gives
SOAP-ERROR: Parsing WSDL: Couldn’t load from : Start tag expected, ‘<‘ not found
$wsdlUrl = 'http://localhost/magento20_0407/soap/default?wsdl_list=1'; $apiUser = 'testUser'; $apiKey = 'admin123'; $token = 'xioxnuuebn7tlh8ytu7781t14w7ftwmp'; $opts = array('http' => array('method' => "GET", 'header' => "Accept-language: enrnConnection: closern")); $context = stream_context_create($opts); stream_context_set_option($context, "http", "protocol_version", 1.1); fpassthru(fopen($wsdlUrl, 'r', false, $context)); $opts = array('http'=>array('header' => 'Authorization: Bearer '.$token)); $serviceArgs = array("id"=>1); try{ $context = stream_context_create($opts); $soapClient = new SoapClient($wsdlUrl, array('version' => SOAP_1_2, 'context' => $context)); $soapResponse = $soapClient->customerCustomerAccountServiceV1($serviceArgs); }catch(Exception $e){ $e->getMessage(); } var_dump($soapResponse);exit;
Can anyone share the code to make SOAP connection in Magento2.x
In Magento1.x the below code works fine to connect SOAP
$apiUrl = 'http://localhost/magento_28_03/index.php/api/soap?wsdl'; $apiUser = 'testUser'; $apiKey = 'admin123'; ini_set("soap.wsdl_cache_enabled", "0"); try{ $client = new SoapClient($apiUrl, array('cache_wsdl' => WSDL_CACHE_NONE)); } catch (SoapFault $e) { echo 'Error in Soap Connection : '.$e->getMessage(); } try { $session = $client->login($apiUser, $apiKey); if($session) echo 'SOAP Connection Successful.'; else echo 'SOAP Connection Failed.'; } catch (SoapFault $e) { echo 'Wrong Soap credentials : '.$e->getMessage(); }
But the above doesn’t work for Magento 1. Can anyone say, what changes the above code needs to work fine for Magento 2?
Advertisement
Answer
For SOAP API call follow the below
test.php
<?php $token = 'YOUR_ACCESS_TOKEN'; require('vendor/zendframework/zend-server/src/Client.php'); require('vendor/zendframework/zend-soap/src/Client.php'); require('vendor/zendframework/zend-soap/src/Client/Common.php'); $addArgs = array('num1'=>2, 'num2'=>1);// Get Request $sumArgs = array('nums'=>array(2,1000));// Post request //$wsdlUrl = YOUR_BASE_URL."soap?wsdl&services=customerAccountManagementV1,customerCustomerRepositoryV1,alanKentCalculatorWebServiceCalculatorV1";//To declar multiple $wsdlUrl = YOUR_BASE_URL."soap?wsdl&services=alanKentCalculatorWebServiceCalculatorV1"; try{ $opts = ['http' => ['header' => "Authorization: Bearer " . $token]]; $context = stream_context_create($opts); $soapClient = new ZendSoapClient($wsdlUrl); $soapClient->setSoapVersion(SOAP_1_2); $soapClient->setStreamContext($context); }catch(Exception $e){ echo 'Error1 : '.$e->getMessage(); } try{ $soapResponse = $soapClient->alanKentCalculatorWebServiceCalculatorV1Add($addArgs);print_r($soapResponse); $soapResponse = $soapClient->alanKentCalculatorWebServiceCalculatorV1Sum($sumArgs);print_r($soapResponse); }catch(Exception $e){ echo 'Error2 : '.$e->getMessage(); } ?>
http://YOUR_BASE_URL/test.php