Esteban G. Gutierrez
Esteban G. Gutierrez

Reputation: 195

how to get the xml in php response

I am trying to get a response from a server with the following code:

<?php

$url = "http://pgtest.redserfinsa.com:2027/WebPubTransactor/TransactorWS?WSDL";

$post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://webservices.serfinsa.sysdots.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:cardtransaction>
         <!--Optional:-->
         <security>{"comid":"comid","key":"$!@!@!@!@!@","comwrkstation":"comwrkstation"}</security>
         <!--Optional:-->
         <txn>MAN</txn>
         <!--Optional:-->
         <message>{"CLIENT":"9999994570392223"}
            </message>
      </web:cardtransaction>
   </soapenv:Body>
</soapenv:Envelope>';


$post_data = array('xml' => $post_string);
$stream_options = array(
    'http' => array(
        'method'  => 'POST',
        'header'  => 'Content-type: text/xml' . "\r\n",
        'content' =>  http_build_query($post_data)));


$context  = stream_context_create($stream_options);
$response = file_get_contents($url, null, $context);
?>

However, I'm still getting empty responses, does anyone have any idea?

Upvotes: 0

Views: 120

Answers (1)

Darwin Bermudez
Darwin Bermudez

Reputation: 66

I don't know why your code returns empty but you can try using curl

CODE:

$url = "http://pgtest.redserfinsa.com:2027/WebPubTransactor/TransactorWS?WSDL";

$xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://webservices.serfinsa.sysdots.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:cardtransaction>
         <security>{"comid":"comid","key":"$!@!@!@!@!@","comwrkstation":"comwrkstation"}</security>
         <txn>MAN</txn>
         <message>{"CLIENT":"9999994570392223"}
         </message>
      </web:cardtransaction>
   </soapenv:Body>
</soapenv:Envelope>';

$headers = array(
  "Content-type: text/xml",
  "Content-length: " . strlen($xml),
  "Connection: close",
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($curl);
$error = curl_error($curl);


print_r($response);
print_r($error);

Result:

Recv failure: Connection reset by peer

Upvotes: 2

Related Questions