2

Every time I run this script. I get an error. I placed this file in the root directory of my magento.

soapexample.php:

<?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 = 'https://www.chemcopharmacy.com/soap/default?wsdl&services=salesOrderRepositoryV1';

$token = 'u46losnl6ia01ctmxxxxxxxxxxxxx'; //access token


$opts = ['http' => ['header' => "Authorization: Bearer ".$token]]; 
$context = stream_context_create($opts); //$arguments = $serviceArgs = 
$serviceArgs = array('searchCriteria'=> 
    array('filterGroups' => 
        array ('filters' =>
            array('field' =>'increment_id',
                  'value' => '000000002' , 
                  'condition_type' => 'eq')
            )
     ) ); 
 $soapClient = new \Zend\Soap\Client($wsdlurl);
 $soapClient->setSoapVersion(SOAP_1_2); 
 $soapClient->setStreamContext($context);
 $result = $soapClient->salesOrderRepositoryV1GetList($serviceArgs);

//array('searchCriteria' => $serviceArgs)); //$result = $soapClient->customerCustomerRepositoryV1GetById(array('customerId' => 1));

Error:

Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing 
WSDL: Couldn't load from 'https://www.chemcopharmacy.com/soap/default? 
wsdl&services=salesOrderRepositoryV1' : failed to load external entity 
"https://www.chemcopharmacy.com/soap/default? 
wsdl&services=salesOrderRepositoryV1" in 
/var/www/html/chemcopharmacy/vendor/zendframework/zend- 
soap/src/Client/Common.php:33 Stack trace: #0 
/var/www/html/chemcopharmacy/vendor/zendframework/zend- 
soap/src/Client/Common.php(33): SoapClient- 
>SoapClient('https://www.che...', Array) #1 
/var/www/html/chemcopharmacy/vendor/zendframework/zend- 
soap/src/Client.php(1070): Zend\Soap\Client\Common->__construct(Array, 
'https://www.che...', Array) #2 
/var/www/html/chemcopharmacy/vendor/zendframework/zend- 
soap/src/Client.php(1245): Zend\Soap\Client->initSoapClientObject() #3 
/var/www/html/chemcopharmacy/vendor/zendframework/zend- 
soap/src/Client.php(1156): Zend\Soap\Client->getSoapClient() #4 
/var/www/html/chemcopharmacy/soapexample2.php(29): Zend\Soap\Client- 
>__call('salesOrderRepos. in 
/var/www/html/chemcopharmacy/vendor/zendframework/zend- 
soap/src/Client/Common.php on line 33

What I have done?

  • I have edited the /etc/hosts file and added the MY.IP.ADDRESS DOMAIN.COM

  • I have also set necessary setting in php.ini files for soap like

    extension=php_soap.dll; extension=php_openssl.dll;

2 Answers 2

5

I tried the SOAP Web Services on my local host and it was working fine. The reason why the Magento 2 was not able to give a response to the request was because it was not able to verify SSL Certificates enabled on the site.

The way I resolved it was to allow self signed SSL verification:

$opts = [
    'http' =>
     [
         'header' => "Authorization: Bearer ".$token
        ],
'ssl' => [
    'allow_self_signed' => true , 
    'verify_peer' => false, 
    'verify_peer_name' => false]
];
$context = stream_context_create($opts);

I hope it helps. ;)

1

This helped me to fix the failed to load external entity issue https://community.magento.com/t5/Magento-2-x-Technical-Issues/SOAP-Error-Failed-to-load-external-entity/m-p/38906#M917

Adding my snippet

$wsdl = 'http://m2.local.com/soap/?wsdl&services=integrationAdminTokenServiceV1';
    $client = $this->soapClient->create($wsdl, array("soap_version" => SOAP_1_2));

    $token = $client->integrationAdminTokenServiceV1CreateAdminAccessToken(array("username"=>"test", "password"=>"test"));

    $opts = [
        'ssl' => [
            'verify_peer'=>false,
            'verify_peer_name'=>false
        ],
        'http' => [
            'header' => 'Authorization: Bearer '. $token->result
        ]
    ];

    $params = [
        'stream_context' => stream_context_create($opts),
        'cache_wsdl' => WSDL_CACHE_NONE
    ];

    $wsdlUrl = 'http://m2.local.com/soap/default?wsdl&services=quoteCartRepositoryV1';
    $soapClient = $this->soapClient->create($wsdlUrl, $params);

    $soapResponse = $soapClient->__getFunctions();
    var_dump($soapResponse);

Where the SoapClient is Magento\Framework\Webapi\Soap\ClientFactory

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Jarnail S
    Aug 18, 2019 at 14:30
  • hi Jai Edited my answer
    – Raviraj
    Aug 20, 2019 at 14:01

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.