Magento – Sample php code to programmatically check SOAP connection in Magento 2

magento2soap

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: en\r\nConnection: close\r\n")); 
$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 Magento1.x. Can anyone say, what changes the above code need to work fine for Magent2.x ?

I copied the view source code and pasted in a text file. Checked it thoroughly. There is no space at all.

Best Answer

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

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');

$wsdlurl = 'http://10.16.16.190//magentoce27/index.php/soap/default?wsdl&services=customerCustomerRepositoryV1';

/*Token Generated from SYstem Integratio*/

$token = 'dx2x7pl5mxq9hp8jr8efcl5crv2pk699';


$opts = ['http' => ['header' => "Authorization: Bearer ".$token]];
$context = stream_context_create($opts);
//$arguments =
$serviceArgs = array('searchCriteria'=> 
        array('filterGroups' => 
            array ('filters' =>
                array('field' =>'firstname',
                      'value' => 'Veronica' , 
                      'condition_type' => 'eq')
                )
         )
);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$result = $soapClient->customerCustomerRepositoryV1GetList($serviceArgs);//array('searchCriteria' => $serviceArgs));
//$result = $soapClient->customerCustomerRepositoryV1GetById(array('customerId' => 1));
/*'search_criteria'=> 
        array('filter_groups' => 
            array ('filters' =>
                array('field' =>'firstname',
                      'value' => 'Veronica' , 
                      'condition_type' => 'eq')
                )
         )
);*/
echo "<pre>"; print_r($result); 
?>

This is test script for soap request so far I tried. You can do as per your need changing request and params

Related Topic