I am trying to send an API request using a XML body and get a response back in xml.
When I test this in Postman the respone is as XML and I am able to see the element names. I used the postman code function to get the code below for PHP.
However the response in PHP curl is a string with element values. There are no element names and the response is one long string with no separators
Any help with this will be greatly appreciated
My code:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://myUrl.com',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="http://myApiService.webservice.gbs">
<soapenv:Body>
<api:getNameOptions>
<api:name>36534367</api:name>
<api:surname>5000033</api:surname>
</api:getNameOptions>
</soapenv:Body>
</soapenv:Envelope>',
CURLOPT_HTTPHEADER => array(
'Content-Type: text/xml; charset=utf-8',
'SOAPAction: urn:getNameOptions'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
PHP Response
500000466280AGI/B1671025282958passedCGI_BTT_02 nop 1/1/1/7/6/10/1CGI/B167/B131/2/2ABCXSDARDCGI_BEST_02:1-1-1-7-6(SP4A) AGB/B167/B131/2, OUT 2 Tray 1(2)
CodePudding user response:
SOAP in PHP is a bit difficult but it's much better to use the build in SOAP class and read the WSDL file to get the complete structure and work with the SOAP api.
When you look at the documentation: https://www.php.net/manual/en/book.soap.php
There is an example that could match your problem.
$soapClient = new SoapClient("https://soapserver.example.com/blahblah.asmx?wsdl");
// Prepare SoapHeader parameters
$sh_param = array(
'Username' => 'username',
'Password' => 'password'
);
$headers = new SoapHeader('http://soapserver.example.com/webservices', 'UserCredentials', $sh_param);
// Prepare Soap Client
$soapClient->__setSoapHeaders(array($headers));
With this example you can login and set the credentials and set it as soap header.
CodePudding user response:
line below fixed the issue
$oXML = new SimpleXMLElement( $response );
header( 'Content-type: text/xml' );
echo $oXML->asXML();