Magento – SOAP-ERROR: Parsing WSDL: Couldn’t load from URL Failed to load External Entity

magento2soapsoap-api-v1soap-api-v2webapi

I'm trying to get all products in $productArrarray from magento 2 API via SOAP client. I tried the below function but it throws this error:

Error:

SOAP-ERROR: Parsing WSDL: Couldn't load from
'https://shop.mysite.com/soap/default?wsdl&services=catalogProductRepositoryV1'
: failed to load external entity
"https://shop.mysite.com/soap/default?wsdl&services=catalogProductRepositoryV1"

Code:

public function getMagentoProducts() {

    $productArr = array('' => 'Select Product');

    try {
        $request = new SoapClient("https://shop.mysite.com/soap/?wsdl&services=integrationAdminTokenServiceV1", array("soap_version" => SOAP_1_2));
        $token = $request->integrationAdminTokenServiceV1CreateAdminAccessToken(array("username"=>"user", "password"=>"pass"));
        $opts = array(
            'http'=>array(
                'header' => 'Authorization: Bearer '.json_decode($token->result),
                'user_agent' => 'PHPSoapClient'
            ),
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $wsdlUrl = 'https://shop.mysite.com/soap/default?wsdl&services=catalogProductRepositoryV1';

        $context = stream_context_create($opts);
        $soapClientOptions = array(
            'stream_context' => $context,
            'cache_wsdl' => WSDL_CACHE_NONE
        );
        libxml_disable_entity_loader(false);

        $soapClient = new SoapClient($wsdlUrl, ['version' => SOAP_1_2, 'context' => $soapClientOptions]);
        var_dump($soapClient);exit;
        $soapResponse = $soapClient->__getFunctions();


    } catch (Exception $e) {

      $this->forward404($e->getMessage());
    }
  }

Best Answer

If you are using a development site with an SSL that is not signed by a major CA, then when PHP goes to load external entities from your domain, your SSL certificate can cause this error.

I just had to add my root CA certificate that signed my SSL to the trusted roots of my server and the error went away.

For Ubuntu 16.04 I added the PEM version of my root CA to /usr/local/share/ca-certificates then ran sudo update-ca-certificates and it was added.

Related Topic