Search code examples
phpxmlsoapsoap-client

Correct XML request with Curl: getting blank XML response


I was trying to hit XML request from PHP SOAP Client according to the following provided SOAP API documentation.


WSDL Schema Refer to the following URL.

http://[SERVER_IP]/PowerSuite/PSXMLSearch.asmx?wsdl

4 Input/Output 4.1 Search Supplier Code and Name (PSXMLSearchSupplier)

  • Input : PSXML_SEARCH_SUPP
  • Output : PSXML_SEARCH_SUPP_Response

6 Field definition

6.1 PSXML_SEARCH_SUPP - The main entry point and the authentication information.

  • MSGID
  • USERID
  • PASSWORD
  • OPTION
  • SUPPNO

But when i run with SOAP client it gives me a following error

Object reference not set to an instance of an object

So after long research on internet, i decided to send XML request via CURL, i have created following Schema for XML request and send it via CURL request

 $soapUrl = "https://[MY_SERVER_DOMAIN]/PowerSuite/PSXMLSearch.asmx"; // asmx URL of WSDL

 // xml post structure

 $xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
                     <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                       <soap:Body>
                         <PSXML_SEARCH_SUPP xmlns="https://[MY_SERVER_DOMAIN]/PowerSuite/PSXMLSearch.asmx"> 
                           <MSGID>TRAVEK_E1</MSGID> 
                           <USERID>GCOT</USERID> 
                           <PASSWORD>CKHOSKIS6</PASSWORD> 
                           <OPTION>LIKE</OPTION> 
                           <SUPPNO>RK0048</SUPPNO> 
                         </PSXML_SEARCH_SUPP>
                       </soap:Body>
                     </soap:Envelope>'; 

    $headers = array(
                 "Content-type: text/xml;charset=\"utf-8\"",
                 "Accept: text/xml",
                 "Cache-Control: no-cache",
                 "Pragma: no-cache",
                 "SOAPAction: \"https://[MY_SERVER_DOMAIN]/PowerSuite\"",                
                 "Content-length: ".strlen($xml_post_string),
             ); 

     $url = $soapUrl;

     // PHP cURL  for https connection with auth
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_USERPWD, "GCONNECT:Connect@786");
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
     curl_setopt($ch, CURLOPT_TIMEOUT, 10);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

     // converting
     $response = curl_exec($ch); 
     curl_close($ch);

     // converting
     $response1 = str_replace("<soap:Body>","",$response);
     $response2 = str_replace("</soap:Body>","",$response1);

     // convertingc to XML
     $parser = simplexml_load_string($response2);
     // user $parser to get your data out of XML response and to display it.
echo "<pre>"; 
print_r($parser); 
echo "</pre>";
die();

But even after that i only get empty XML response as can see below

SimpleXMLElement Object
(
)

Please guide am i sending a correct XML schema, or their needs to be some thing amend into it?


Solution

  • I checked with RapidAPI, it shows the correct response, i have shown the request in the image below:

    In FORM https://gcdnb.pbrd.co/images/a9NdABt6rXQl.png

    In XML

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xh="http://www.xmlhk.com/">
       <soapenv:Header/>
       <soapenv:Body>
          <xh:PSXMLSearchSupplier>
             <!--Optional:-->
             <xh:PSXML_SEARCH_SUPP>
                <!--Optional:-->
                <xh:MSGID>TRAVEK_EBOOK001</xh:MSGID>
                <!--Optional:-->
                <xh:USERID>ABCUSER</xh:USERID>
                <!--Optional:-->
                <xh:PASSWORD>Connect@786</xh:PASSWORD>
                <!--Optional:-->
                <xh:OPTION>LIKE</xh:OPTION>
                <!--Optional:-->
                <xh:SUPPNO>ABDECS</xh:SUPPNO>
                <!--Optional:-->
                <xh:SUPPNAME>?</xh:SUPPNAME>
             </xh:PSXML_SEARCH_SUPP>
          </xh:PSXMLSearchSupplier>
       </soapenv:Body>
    </soapenv:Envelope>
    

    I tried the same above with CURL request and got that working, i found that,following to be changed in my previous CURL request , which on the raw tab in ReadyAPI

    "SOAPAction: \"http://www.xmlhk.com/PSXMLSearchSupplier\"",
    

    To work this with SOAP Client i just need to used multi dimensional array with $params['PSXML_SEARCH_SUPP'] shows below

    $wsdl = "https://ps.gerrys.com.pk/PowerSuite/PSXMLSearch.asmx?WSDL";
            $params['PSXML_SEARCH_SUPP'] = array(
              'MSGID'   =>  "TRAVEK_EBOOK001",
              'USERID'  =>  "GCONNECT",
              'PASSWORD' => "Connect@786",
              'OPTION'  =>  'LIKE',
              'SUPPNO'  =>  'RK0048'
            ); 
          try {
              $soap = new SoapClient($wsdl);
              $data = $soap->PSXMLSearchSupplier($params);
                echo "<pre>"; 
                print_r($data); 
                echo "</pre>";
              die();                    
          }
          catch(Exception $e) {
              $e->getMessage();
              var_dump($e->getMessage());
              die();
          }