Parsing response from the WSDL using PHP - php

I'm very sorry if I made a wrong title, I'm not familiar with SOAP response and types of it. But I guess it's a WSDL response, at least I got it from WSDL link...
I have a following url
http://somedomain.com/j.svc?wsdl
And after I made a request using curl_multi I got the following response. The response was shortened to two results so it would be easier to read
The response is as following:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetJourneyListResponse xmlns="http://tempuri.org/">
<GetJourneyListResult xmlns:a="http://schemas.datacontract.org/2004/07/DreamFlightWCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Journey>
<a:FromAirport>LHR</a:FromAirport>
<a:TotalPrice>146</a:TotalPrice>
</a:Journey>
<a:Journey>
<a:FromAirport>LHR</a:FromAirport>
<a:TotalPrice>155</a:TotalPrice>
</a:Journey>
</GetJourneyListResult>
</GetJourneyListResponse>
</s:Body>
</s:Envelope>
Is there any chance to parse the result using PHP? I made lots of searches including StackOverflow and here what I managed to find.
To parse the above response I can use following code:
$xml = simplexml_load_string($result);
$xml->registerXPathNamespace('flight','http://schemas.datacontract.org/2004/07/DreamFlightWCF');
foreach ($xml->xpath('//flight:Journey') as $item){
print_r($item);
}
It seems that the above PHP code piece is correct by partially. I get the correct amount of "Journey"s but the $item by its own is empty.
Any solutions? Please don't advise to use SoapClient to retrieve the result. I can't move from curl_multi. I already have the result and I need to parse it. Thank you in advance

$soap_request = "<?xml version=\"1.0\"?>\n";
$soap_request .= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">\n";
$soap_request .= " <soap:Body xmlns:m=\"http://www.example.org/stock\">\n";
$soap_request .= " <m:GetStockPrice>\n";
$soap_request .= " <m:StockName>IBM</m:StockName>\n";
$soap_request .= " </m:GetStockPrice>\n";
$soap_request .= " </soap:Body>\n";
$soap_request .= "</soap:Envelope>";
$header = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\"",
"Content-length: ".strlen($soap_request),
);
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, "http://ecc6unitst.kaisa.com:8000/sap/bc/srt/wsdl/bndg_386D2B5BD851F337E1000000AC1264E4/wsdl11/allinone/standard/document?sap-client=400" );
curl_setopt($soap_do, CURLOPT_USERPWD, "EBALOBORRP:welcome1");
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
$str = curl_exec($soap_do);
if(curl_exec($soap_do) === false) {
$err = 'Curl error: ' . curl_error($soap_do);
curl_close($soap_do);
print $err;
} else {
curl_close($soap_do);
var_dump($str);
print 'Operation completed without any errors';
}

First try Parsing SOAP response then try Google.

Related

PHP cURL submit to WSDL SOAP environment

I've never had the opportunity to submit to a WSDL SOAP web service and am running into some issues. Im using PHP cURL to submit the form to a known back end fist, then secondly to the WSDL SOAP service. The first part is working fine, so I will skip over that. I have spent the better part of 3 days trying different solutions I've found on the web, and my own after reading SOAP documentation, with no luck.
Here is what I'm using to submit to the WSDL
<?php
//first cURL POST HERE - works fine
//second cURL POST BELOW
$FName = $_POST['FirstName'];
$Lname = $_POST['LastName'];
$Email = $_POST['Email'];
$Phone = $_POST['Phone1'];
$soapURL = "https://something.com/IBWeb/IBDemoManager/IBDemoManager.asmx?wsdl";
$soapUser = "USR";
$soapPassword = "PWD";
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$xml_post_string = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservices.htdocs.openecry">
<soapenv:Header/>
<soapenv:Body>
<web:demosetup soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<AccessCode xsi:type="xsd:string">G0!=#%fut40</AccessCode>
<NewUserCategoryName xsi:type="xsd:string">OFLDemo</NewUserCategoryName>
<TemplateUserName xsi:type="xsd:string">OFLUser</TemplateUserName>
<CusType xsi:type="xsd:string">Indirect</CusType>
<WLabelID xsi:type="xsd:string">276</WLabelID>
<SCodeID xsi:type="xsd:string"></SCodeID>
<SoftID xsi:type="xsd:string">1</SoftID>
<FName xsi:type="xsd:string">'.$FName.'</FName>
<LName xsi:type="xsd:string">'.$LName.'</LName>
<Email xsi:type="xsd:string">'.$Email.'</Email>
<Phone xsi:type="xsd:string">'.$Phone.'</Phone>
<Address xsi:type="xsd:string"></Address>
<City xsi:type="xsd:string"></City>
<Zip xsi:type="xsd:string"></Zip>
<State xsi:type="xsd:string"></State>
<Country xsi:type="xsd:string"></Country>
<CountryName xsi:type="xsd:string"></CountryName>
<AssetTypes xsi:type="xsd:string">Futures</AssetTypes>
<How xsi:type="xsd:string">OFL webservice</How>
<MoreEmail xsi:type="xsd:string"></MoreEmail>
<RemoteAddr xsi:type="xsd:string">'.$hostname.'</RemoteAddr>
<CampaignID xsi:type="xsd:string"></CampaignID>
</web:demosetup>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
//IS SOAPAction the same as the endpoint "$soapURL"?//
"SOAPAction: https://something.com/IBWeb/IBDemoManager/IBDemoManager.asmx?wsdl",
"Content-length: ".strlen($xml_post_string),
);
$url2 = $soapURL;
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url2 );
curl_setopt($soap_do, CURLOPT_HEADER, false);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 100);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
if(curl_exec($soap_do) === false) {
$err = 'Curl error: ' . curl_error($soap_do);
curl_close($soap_do);
print $err;
} else {
$result = curl_exec($soap_do);
echo '<pre>';
print_r($result);
curl_close($soap_do);
//print 'Operation completed without any errors';
}
Here are just some comments:
Try to disable the SSL check (just for testing):
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
You should call curl_close($ch2); as last. Example:
$output2 = curl_exec($ch2);
if(curl_errno($ch2))
echo curl_error($ch2);
} else {
echo $output2;
}
curl_close($ch2); // <--- close here
You could also try the Zend SOAP library.
If you don't like CURL try Guzzle to make an HTTP request.

SOAP XML call using PHP curl?

I have one SOAP WSDL api and i have a test api access.
My SOAP Url : https://development.avinode.com/avinode/AvinodeIntegrationWeb/ws/EmptyLegFlightDemand.ws?wsdl
i need to call and get the details from this url. i must send my username and password into this api call. so i tried to build the code. the code is given below.
<?php
$soapUrl='https://development.avinode.com:443/avinode/AvinodeIntegrationWeb/ws/EmptyLegDownload.ws?wsdl';
$xml_post_string='<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<S:Header>
<To xmlns="http://www.w3.org/2005/08/addressing">https://development.avinode.com/avinode/AvinodeIntegrationWeb/ws/EmptyLegDownload.ws</To>
<Action xmlns="http://www.w3.org/2005/08/addressing">http://www.avinode.com/integration/EmptyLegDownload#request</Action>
<ReplyTo xmlns="http://www.w3.org/2005/08/addressing">
<Address>http://www.w3.org/2005/08/addressing/anonymous</Address>
</ReplyTo>
<FaultTo xmlns="http://www.w3.org/2005/08/addressing">
<Address>http://www.w3.org/2005/08/addressing/anonymous</Address>
</FaultTo>
<MessageID xmlns="http://www.w3.org/2005/08/addressing">uuid:6a456287-923e-458e-9ccb-f900307f2b0f</MessageID>
<wsse:Security S:mustUnderstand="1">
<wsu:Timestamp xmlns:ns13="http://www.w3.org/2003/05/soap-envelope" xmlns:ns14="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512" wsu:Id="_1">
<wsu:Created>2014-10-17T15:45:42Z</wsu:Created>
<wsu:Expires>2014-10-17T15:50:42Z</wsu:Expires>
</wsu:Timestamp>
<wsse:UsernameToken xmlns:ns13="http://www.w3.org/2003/05/soap-envelope" xmlns:ns14="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512" wsu:Id="uuid_2da8da35-1c69-4f38-9899-ba6950c825f5">
<wsse:Username>MY_USERNAME</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">PASSWORD</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</S:Header>
<S:Body>
<ns3:request xmlns="http://www.avinode.com/core/CommonTypes" xmlns:ns2="http://www.avinode.com/services/EmptyLegDownload" xmlns:ns3="http://www.avinode.com/integration/EmptyLegDownload">
<ns2:product>
<name>osiz</name>
<version>1.0</version>
</ns2:product>
<ns2:domain>http://flightcomparision.osiztechnologies.com</ns2:domain>
<ns2:locale>en_US</ns2:locale>
<ns2:currency>USD</ns2:currency>
<ns2:region>AMERICA</ns2:region>
<ns2:after>2014-11-01T00:00:00Z</ns2:after>
<ns2:before>2014-12-01T00:00:00Z</ns2:before>
<ns2:pax>1</ns2:pax>
<ns2:excludeBrokers>false</ns2:excludeBrokers>
<ns2:requireTailNumber>false</ns2:requireTailNumber>
</ns3:request>
</S:Body>
</S:Envelope>';
$headers = array(
"Content-Type: text/xml;charset=UTF-8",
"Accept: gzip,deflate",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"\"",
"Authorization: Basic $auth",
"Content-length: ".strlen($xml_post_string),
);
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $soapUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
curl_setopt($ch, CURLOPT_MAXREDIRS, 12);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ss = curl_getinfo($ch);
//print_r($ss);
// exit;
$response = curl_exec($ch);
print_r($response);
exit;
curl_close($ch);
?>
I got only Empty response only please give me any idea highly appreciated.
I would start by adding:
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
That will allow you to see the response http code of the server.
Usually when I work on SOAP / REST services I make sure to have Fiddler running in the background, that makes debugging a lot easier.
You can find how to use Fiddler with cURL here: Configure PHP cURL

Using XML Data as Array

I have a problem, I'm trying to get data from http://www.acquaintcrm.co.uk/propertyfunctions/propertysearch.asmx?op=GetDataV8
I have made the request and output the data to a file, properties.xml
The file gets filled with data, but I just dont know how to use that data, I would like to convert the xml data into an array, but I honestly don't know where to start.
My code
$xml_data = '<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>
<GetDataV8 xmlns="http://www.AcquaintCRM.co.uk/propertyfunctions/">
<strSitePrefix>STDZ</strSitePrefix>
<intSiteID>-1</intSiteID>
<intPropertyID>0</intPropertyID>
<intPropertyDevelopmentID>0</intPropertyDevelopmentID>
<bytBedrooms>0</bytBedrooms>
<decMinPrice>0</decMinPrice>
<decMaxPrice>0</decMaxPrice>
<bytTenure>1</bytTenure>
<intCommercial>-1</intCommercial>
<bytPropertyAge>0</bytPropertyAge>
<intRentalTerms>0</intRentalTerms>
<bytFeaturedCount>0</bytFeaturedCount>
<strAreas></strAreas>
<strTypes></strTypes>
<bytSortOrder>0</bytSortOrder>
<bytUseCDataTags>0</bytUseCDataTags>
</GetDataV8>
</soap:Body>
</soap:Envelope>
';
$url = "http://www.acquaintcrm.co.uk/propertyfunctions/propertysearch.asmx?op=GetDataV8";
$headers = array(
"POST * HTTP/1.1",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Content-length: ".strlen($xml_data),
"SOAPAction: http://www.AcquaintCRM.co.uk/propertyfunctions/GetDataV8",
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$response = curl_exec($ch);
if (curl_errno($ch))
{
echo 'Error: ';
echo curl_error($ch);
}
else
{
curl_close($ch);
$xml = file_put_contents('properties.xml', $response);
$xml = simplexml_load_file('properties.xml');
print_r($xml);
}
The XML file can be viewed here
http://manchester.studentdigz.com/properties.xml
Thank you all in advance, I appreciate all help!
You can use php simplexml (http://php.net/manual/en/book.simplexml.php) to load the string in an object.
Note that if you really want an array you could as well do :
$xml = simplexml_load_string($xmlstring)->children('http://schemas.xmlsoap.org/soap/envelope/')->children('Body');
$json = json_encode($xml);
$array = json_decode($json,TRUE);
echo "<pre>";
echo "xml object : <br/>";
var_dump($xml);
echo "<hr/>";
echo "json string : <br/>";
var_dump($json);
echo "<hr/>";
echo "array : <br/>";
var_dump($array);

Soap request using cURL and PHP

I'm trying to send a SOAP post via Curl in PHP but I always get a couldn't connect to host problem.
But when i trie to use the same URL in a web based client like hurl, a got the correct response :
Hurl Test :http://www.hurl.it
My code :
$url = "https://gateway.monster.com/bgwBroker";
$soapMessage = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">' .
'<SOAP-ENV:Header>'.
'<mh:MonsterHeader xmlns:mh="http://schemas.monster.com/MonsterHeader">'.
'<mh:MessageData>'.
'<mh:MessageId>PresenceMedia SARL Jobs</mh:MessageId>'.
'<mh:Timestamp>2004-06-09T14:41:44Z</mh:Timestamp>'.
'</mh:MessageData>'.
'</mh:MonsterHeader>'.
'<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/04/secext">'.
'<wsse:UsernameToken>'.
'<wsse:Username>xrtpjobsx01</wsse:Username>'.
'<wsse:Password>rtp987654</wsse:Password>'.
'</wsse:UsernameToken>'.
'</wsse:Security>'.
'</SOAP-ENV:Header>'.
'<SOAP-ENV:Body>'.
'<Job jobRefCode="Job - minimal fields" jobAction="addOrUpdate" jobComplete="true" xmlns="http://schemas.monster.com/Monster" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.monster.com/Monster http://schemas.monster.com/Current/xsd/Monster.xsd">'.
'<RecruiterReference>'.
'<UserName>xrtpjobsx01</UserName>'.
'</RecruiterReference>'.
'<JobInformation>'.
'<JobTitle><![CDATA[PresenceMedia SARL its a simple test from morocco blablablablablablablablablablablabla]]></JobTitle>'.
'<JobStatus monsterId="4">JobTypeFullTime</JobStatus>'.
'<PhysicalAddress>'.
'<City>Rabat Shore</City>'.
'<State>NY</State>'.
'<CountryCode>US</CountryCode>'.
'<PostalCode>11220</PostalCode>'.
'</PhysicalAddress>'.
'<JobBody><![CDATA[PresenceMedia SARL Body blablabla blablabla blablabla blablabla blablabla blablabla blablabla]]></JobBody>'.
'</JobInformation>'.
'<JobPostings>'.
'<JobPosting>'.
'<Location>'.
'<City>London East</City>'.
'<State>London</State>'.
'<CountryCode>UK</CountryCode>'.
'</Location>'.
'<JobCategory monsterId="47" />'.
'<JobOccupations>'.
'<JobOccupation monsterId="11909" />'.
'</JobOccupations>'.
'<BoardName monsterId="1" />'.
'<Industries>'.
'<Industry>'.
'<IndustryName monsterId="1" />'.
'</Industry>'.
'</Industries>'.
'</JobPosting>'.
'</JobPostings>'.
'</Job>'.
'</SOAP-ENV:Body>'.
'</SOAP-ENV:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\"",
"Content-length: ".strlen($soapMessage),
);
$soapUser = "xrtpjobsx01";
$soapPassword = "rtp987654";
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url);
curl_setopt($soap_do, CURLOPT_PORT, 8443);
curl_setopt($soap_do, CURLOPT_USERPWD, $soapUser.":".$soapPassword);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do,CURLOPT_TIMEOUT,9000);
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soapMessage);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($soap_do);
$err = curl_error($soap_do);
echo "Result:" . $result;
echo "<br>Error:" . $err ;
echo "<b><pre>";
var_dump(curl_getinfo($soap_do));
echo "</pre>";
?>
In php use simple Soapclient to post reponse. and check he response also check if the port used for connection is free.
$client = new SoapClient($Url, $options);
$data = $client->functionName($params);

Make a POST request

I would like to know how to make a HTTP POST request like it's described there http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#UploadingMetadata (Creating an empty document).
My code looks like this:
<?php
$headers = array(
"POST /feeds/default/private/full HTTP/1.1",
"Host: docs.google.com",
"GData-Version: 3.0",
"Content-Length: 287",
"Content-Type: application/atom+xml"
);
$data = "<?xml version='1.0' encoding='UTF-8'?>";
$data .= "<entry xmlns='http://www.w3.org/2005/Atom'>";
$data .= "<category scheme='http://schemas.google.com/g/2005#kind'";
$data .= "term='http://schemas.google.com/docs/2007#document'/>";
$data .= "<title>new document</title>";
$data .= "</entry>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://google.com/docs/feeds/default/private/full");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
print_r($result);
?>
What's wrong there? Am I doing request correctly?
$data = "<?xml version='1.0' encoding='UTF-8'?>";
Replace with:
$data = "<"."?xml version='1.0' encoding='UTF-8'?".">";
And...
$data .= "term='http://schemas.google.com/docs/2007#document'/>";
With:
$data .= " term='http://schemas.google.com/docs/2007#document'/>";
Oh and finally, you shouldn't be print_ring the result; print_r is for arrays and objects, not strings (curl_exec returns a string or null/false), instead use var_dump($result);
Further, your custom headers look weird:
POST is not a header at all so that's plain wrong.
Host: is added by curl itself, no point in setting that.
Content-Length: is done by curl itself, you mostly risk confusing curl if you get it wrong.

Categories