Search code examples
phpweb-servicessoapwsdl

php soapclient wsdl SOAP-ERROR: Parsing WSDL: Couldn't load from


I have googled and looked here in stackoverflow but I have not found a solution to my specific problem. I keep getting the error

 SOAP-ERROR: Parsing WSDL: Couldnt load from "https://sampleurl.com/MerchantQueryService.asmx?WSDL" : failed to load external entity "https://sampleurl.com/MerchantQueryService.asmx?WSDL"

I am trying to use a SOAP API with a URL like

https://sampleurl.com/MerchantQueryService.asmx?WSDL 

I am running MAMP on my localhost and using godaddy shared hosting, I have tried on both with the wsdl file can be found here

http://clemdemo.com/test.wsdl

In PHP I use the code below

error_reporting(-1);
ini_set('display_errors', 'On');
ini_set('soap.wsdl_cache_enabled', 0);

echo "<pre>";

try {

$url     = "https://sampleurl.com/MerchantQueryService.asmx?WSDL ";
$headers = [
    'Host: sampleurl.com',
    'Connection: Keep-Alive',
    'User-Agent: PHP-SOAP/5.3.29',
    'Content-Type: text/xml; charset=utf-8',
    'SOAPAction: "RequestTransaction"',
    'Content-Length: 409'];

$rq = [
"userName" => "username_here",
"passWord" => "password_here",
"referenceId" => "3455566694",
"msisdn" => "346774313"];


try {

$cient = new SoapClient($url,
    [
    'soap_version' => SOAP_1_2,
    'exceptions' => 1,
    'cache_wsdl' => WSDL_CACHE_NONE,
    'trace' => 1,
    'stream_context' => stream_context_create(array('http' => array('header' => $headers)))
]);

print_r($cient);
} catch (SoapFault $e) {
    echo "\nFault Code: ".$e->faultcode;
    echo "\nFault String: ".$e->faultstring;
}

On my MAMP localhost i have SOAP,openssl and curl.

ALSO, I tried using (sending request) to the API with an online WSDL http://wsdlbrowser.com it WORKS but on code using PHP it fails


Solution

  • It often happens that providers neglect their SSL certificates and hence the sites and services end up having an invalid certificates - I suspect this is the case here.

    Disabling the certificate validation as described by Kaii here or even better get your provider to fix their certificate.

    Your code could/should then be something like:

    $url     = "https://sampleurl.com/MerchantQueryService.asmx?WSDL ";
    $context = stream_context_create(array(
            'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
            )
    ));
    
    $rq = ["userName" => "username_here",
           "passWord" => "password_here",
           "referenceId" => "3455566694",
           "msisdn" => "346774313"];
    
    $service = new SoapClient($url, array('stream_context' => $context));
    $service->RequestTransaction($rq);