Sending XML via php and getting back a response - php

I'm having problems sending the follwoing XML via PHP. I can't seem to get the script right it just comes back with a blank page. Any suggestions would be great. I am not coming up with any errors just nothing seems to happen.
POST /PubServices/WebServices/PublicationWebServices.asmx HTTP/1.1
Host: www.geminifund.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
POST /PubServices/WebServices/PublicationWebServices.asmx HTTP/1.1
Host: www.geminifund.com
Content-Type: application/soap+xml; charset= utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetHistroricalNav xmlns="http://www.geminifund.com/webservices/">
<startDate>dateTime</startDate>
<endDate>dateTime</endDate>
<portfolio>string</portfolio>
</GetHistroricalNav>
</soap12:Body>
</soap12:Envelope>
and my script is as follows
<?php
$url = "http://www.geminifund.com";
$post_string = '<?xml version="1.0" encoding="UTF-8"?>
<rootNode>
<innerNode>
<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>
<GetHistroricalNav xmlns="http://www.geminifund.com/webservices/">
<startDate>2011-06-30</startDate>
<endDate>2011-7-30</endDate>
<portfolio>1778/portfolio>
</GetHistroricalNav>
</soap:Body>
</soap:Envelope>
</innerNode>
</rootNode>';
$header = "POST /PubServices/WebServices/PublicationWebServices.asmx HTTP/1.1 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($post_string)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $post_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
?>

When you write "it just comes back with a blank page " are you referring to a web browser response when you enter the url? If this is what you are referring to, what web browser are you using? I recommend you try different web browsers (Safari, FireFox, Chrome) and see what results you get.

Why are you using headers to set a POST method and doing a custom request? Curl is perfectly capable of doing a POST as is:
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
custom request is for when you want to do a less-common method, such as a HEAD or PUT request.

and PHP have SoapClient class to handle SOAP protocol. Are you sure you want to do it in hand-written manner, friend? :)

Related

How to send request for "multipart/related" Content-Type using PHP-curl?

Following are the soap envelop and header info:
POST /tf6/services/xdsrepositoryb HTTP/1.1
Content-Type: multipart/related;
boundary=MIMEBoundaryurn_uuid_566EAD10FEBB55C5A61257193478449;
type="application/xop+xml"; start="
<0.urn:uuid:566EAD10FEBB55C5A61257193478450#apache.org>"; start-
info="application/soap+xml";
action="urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b"
--MIMEBoundaryurn_uuid_566EAD10FEBB55C5A61257193478449
Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml"
Content-Transfer-Encoding: binary
Content-ID: <0.urn:uuid:566EAD10FEBB55C5A61257193478450#apache.org>
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://www.w3.org/2005/08/addressing">
<soapenv:Header>
<wsa:To>http://localhost:5000/tf6/services/xdsrepositoryb</wsa:To>
<wsa:MessageID soapenv:mustUnderstand="true">urn:uuid:566EAD10FEBB55C5A61257193478400</wsa:MessageID>
<wsa:Action>urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b</wsa:Action>
</soapenv:Header>
<soapenv:Body>
<xdsb:ProvideAndRegisterDocumentSetRequest xmlns:xdsb="urn:ihe:iti:xds-b:2007">
<lcm:SubmitObjectsRequest xmlns:lcm="urn:oasis:names:tc:ebxml-regrep:xsd:lcm:3.0">
<rim:RegistryObjectList xmlns:rim="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0">
<rim:ExtrinsicObject id="Document01" mimeType="text/plain"
objectType="urn:uuid:7edca82f-054d-47f2-a032-9b2a5b5186c1">
</lcm:SubmitObjectsRequest>
<xdsb:Document id="Document01">
<xop:Include href="cid:1.urn:uuid:566EAD10FEBB55C5A61257193478499#apache.org"
xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
</xdsb:Document>
</xdsb:ProvideAndRegisterDocumentSetRequest>
</soapenv:Body>
</soapenv:Envelope>
--MIMEBoundaryurn_uuid_566EAD10FEBB55C5A61257193478449
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-ID: <1.urn:uuid:566EAD10FEBB55C5A61257193478499#apache.org>
This is my document.
It is great!
--MIMEBoundaryurn_uuid_566EAD10FEBB55C5A61257193478449--
How can I write a PHP script using CURL to send a request?
I am getting HTTP-400 error whenever I am trying to call the service, please advise.
Thanks.
This is the code I am trying:
$body= $body = '<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b</a:Action>
<a:From>
<a:Address>urn:oid:2.16.840.1.113883.3.1259.10.1003</a:Address>
</a:From>
<a:To>https://CDACert.hawaiihie.org:20000/repository</a:To>
</s:Header>
<s:Body>
<ProvideAndRegisterDocumentSetRequest xmlns="urn:ihe:iti:xds-b:2007" xmlns:xop="http://www.w3.org/2004/08/xop/include">
<lcm3:SubmitObjectsRequest xmlns:lcm3="urn:oasis:names:tc:ebxml-regrep:xsd:lcm:3.0" xmlns:rim="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0">
<rim:RegistryObjectList>
<rim:ExtrinsicObject id="Document1" mimeType="text/xml" objectType="urn:uuid:7edca82f-054d-47f2-a032-9b2a5b5186c1">
</rim:Association>
</rim:RegistryObjectList>
</lcm3:SubmitObjectsRequest>
<Document id=\"Document1\">
<xop:Include href="cid:test_ID_123"
xmlns:xop="http://www.w3.org/2004/08/xop/include"/></Document>
</ProvideAndRegisterDocumentSetRequest>
</s:Body>
';
$text = 'dGhpcyBpcyBTaGl2J3MgZG9j';// base64 of content
$boundary = 'MIMEBoundaryurn_uuid_'.uniqid();
$postData = "--$boundary\n"
."Content-Type: application/xop+xml; charset=UTF-8; type=\"application/soap+xml\n"
."Content-Transfer-Encoding: binary\n"
."Content-length:".strlen($body)."\n"
."Content-ID:test_start\n"
.$body."\n\n"
."--$boundary\n"
."Content-Type: text/plain;\n"
."Content-Transfer-Encoding: binary\n"
."Content-length:".strlen($text)."\n"
."Content-ID:test_ID_123\n"
.$text."\n"
."--$boundary--";
$header1 = array (
"Content-Type: multipart/related; type=\"application/xop+xml\"; start=\"test_start\"; start-info=\"application/soap+xml\"; action=\"urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b\"",
"Content-length:".strlen($postData),
"Transfer-Encoding:chunked",
"User-Agent:" .$_SERVER ['HTTP_USER_AGENT'],
"Host:" .$_SERVER['HTTP_HOST']
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $soapUrl);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLINFO_, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array($postData)); // the SOAP request
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch); // 400 HTTP error from curl_info

PHP & XML - How to generate a soap request error 415 [duplicate]

This question already has answers here:
PHP Bad Request in Curl on SOAP interface of http://www.cyberlogic.gr/webservices/PlaceSearch
(5 answers)
Closed 8 years ago.
Why am I getting error 415, if anyone can help me
I do not understand why the error occurs
link to error
http://agency.lastminute-hr.com/stranice/upisi_destinacije_unico.php
This is how the xml
POST /services/WebService.asmx HTTP/1.1
Host: wl.filos.com.gr
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<PlaceSearch xmlns="http://www.cyberlogic.gr/webservices/">
<xml>string</xml>
</PlaceSearch>
</soap12:Body>
</soap12:Envelope>
my code is:
$soapUrl = "http://wl.filos.com.gr/services/WebService.asmx?op=PlaceSearch";
$soap_request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$soap_request .= "<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/\">\n";
$soap_request .= " <soap:Body>\n";
$soap_request .= " <PlaceSearch xmlns=\"http://www.cyberlogic.gr/webservices/\">\n";
$soap_request .= " <xml><PlaceSearchRequest><Username>******</Username><Password>******</Password><PlaceType>Cities</PlaceType><Language>en</Language></PlaceSearchRequest></xml>\n";
$soap_request .= " </PlaceSearch>\n";
$soap_request .= " </soap:Body>\n";
$soap_request .= "</soap:Envelope>";
$xml_post_string = $soap_request;
$headers = array(
"POST /services/WebService.asmx HTTP/1.1",
"Host: wl.filos.com.gr",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string)
);
$url = $soapUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($xml_post_string) );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
// echo results
echo "The server responded: <br />";
echo " " . $info['http_code']. " <br />";
curl_close($ch);
$response1 = str_replace("<soap12:Body>","",$response);
$response2 = str_replace("</soap12:Body>","",$response1);
$parser = simplexml_load_string($response2);
415 is the HTTP Status Code you obtain from the response of the HTTP request you fire with curl.
These codes are standardized and documented, in your case of 415:
415 Unsupported Media Type
The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.
The entity of the request means the request-body, which is the POST method's request body. In short this means, that the data you've send to the server does not fit it's needs.
You have to fix the data you send to the server first, otherwise all later operations (like loading the response string into simplexml) will fail as well.
Alternatively if you're sure you followed the specs of the webservice correctly, the only thing you can do is proper error handling, that is, if the server returns an error for your request (codes 400 to 499) to not process further the return value but just signal the error condition.
This XML is invalid for webservice you use.
Paste it here: http://wl.filos.com.gr/services/WebService.asmx?op=PlaceSearch and check what is wrong

PHP & XML - How to generate a soap request in PHP from this XML?

I am completly new to SOAP operations.
I have been provided with an XML document (SOAP) to get some collection points for a shipping method.
From the manual located here:
http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint
I can see that I need to use the following SOAP request:
POST /package/package_1.3/packageservices.asmx HTTP/1.1
Host: privpakservices.schenker.nu
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://privpakservices.schenker.nu/SearchCollectionPoint"
<?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>
<SearchCollectionPoint xmlns="http://privpakservices.schenker.nu/">
<customerID>long</customerID>
<key>string</key>
<serviceID>string</serviceID>
<paramID>int</paramID>
<address>string</address>
<postcode>string</postcode>
<city>string</city>
<maxhits>int</maxhits>
</SearchCollectionPoint>
</soap:Body>
</soap:Envelope>
The thing is that i don't know how to send this as a request using PHP, and how to get the response.
Any help to pinpoint me in the right direction, is much appreciated.
UPDATE
I can read the response data with var_dump. However, I am not able to read individual element data.
I need to read data as below
foreach($parser as $row) {
echo $row->customerID;
echo $row->key;
echo $row->serviceID;
}
If anyone should be interested, i have provided the correct answer:
$soapUrl = "http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint";
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><SearchCollectionPoint xmlns="http://privpakservices.schenker.nu/"><customerID>XXX</customerID><key>XXXXXX-XXXXXX</key><serviceID></serviceID><paramID>0</paramID><address>RiksvÅ gen 5</address><postcode>59018</postcode><city>Mantorp</city><maxhits>10</maxhits></SearchCollectionPoint></soap12:Body></soap12:Envelope>';
$headers = array(
"POST /package/package_1.3/packageservices.asmx HTTP/1.1",
"Host: privpakservices.schenker.nu",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string)
);
$url = $soapUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
$parser = simplexml_load_string($response2);
following example might help you.
SOAP XML schema
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.dataaccess.com/webservicesserver/">
<x:Header/>
<x:Body>
<web:NumberToDollars>
<web:dNum>10</web:dNum>
</web:NumberToDollars>
</x:Body>
</x:Envelope>
PHP code
$wsdl = 'http://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL';
try{
$clinet=new SoapClient($wsdl);
$ver =array("dNum"=>"2002");
$quates=$clinet->NumberToDollars($ver);
var_dump($quates);
}
catch(SoapFault $e)
{
echo $e->getMessage();
}

soap request issue

POST /accountapi/service.asmx HTTP/1.1
Host: staging.prepaidfinancialservices.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://accountapi.internal/CardInquiry"
<?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>
<CardInquiry xmlns="http://accountapi.internal/">
<sClientLogin>string</sClientLogin>
<sClientPassword>string</sClientPassword>
<CardNumber>string</CardNumber>
</CardInquiry>
</soap:Body>
</soap:Envelope>
Above is the SOAP request but when i try to post it via my php code i am getting an error that "Couldn't resolve host 'accountapi.internal"...help me plzz
<?php
ini_set('display_errors', "1");
$a=fopen("test.xml",'r');
$mxml=fread($a,65536);
$url='http://accountapi.internal/CardInquiry';
$yt =curl_init($url);
$header = "POST /accountapi/service.asmx HTTP/1.1 <br>";
$header .= "Host: staging.prepaidfinancialservices.com <br>";
$header .= "Content-Type: application/soap+xml; charset=utf-8 <br>";
$header .= "Content-Length: ".strlen($mxml)."\r\n <br>";
$header .= "SOAPAction: 'http://accountapi.internal/CardInquiry' <br>";
echo $header."<br><br>";
//exit;
$header .= $mxml;
curl_setopt($yt, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($yt, CURLOPT_URL, $url);
curl_setopt($yt, CURLOPT_CUSTOMREQUEST, $header);
curl_setopt($yt, CURLOPT_RETURNTRANSFER, true);
$rxml=curl_exec($yt);
echo "sent\n$mxml\n<br>";
echo "received\n$rxml\n";
echo curl_error($yt);
?>
Here is my php code ....
I think the url http://accountapi.internal/CardInquiry belongs to some LAN & is publicly not available to use. So when ever you are calling this URL it's not getting any name server register for this address. so you are getting the message "Couldn't resolve host". Try putting IP Address i think it will work.

sending xml and headers via curl

wondering how I can set all this data in a curl session, via php:
POST /feeds/api/users/default/uploads HTTP/1.1
Host: uploads.gdata.youtube.com
Authorization: AuthSub token="DXAA...sdb8"
GData-Version: 2
X-GData-Key: key=adf15ee97731bca89da876c...a8dc
Slug: video-test.mp4
Content-Type: multipart/related; boundary="f93dcbA3"
Content-Length: 1941255
Connection: close
--f93dcbA3
Content-Type: application/atom+xml; charset=UTF-8
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<media:group>
<media:title type="plain">Bad Wedding Toast</media:title>
<media:description type="plain">
I gave a bad toast at my friend's wedding.
</media:description>
<media:category
scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
</media:category>
<media:keywords>toast, wedding</media:keywords>
</media:group>
</entry>
--f93dcbA3
Content-Type: video/mp4
Content-Transfer-Encoding: binary
<Binary File Data>
--f93dcbA3--
I don't understand why have some headers, then the --f93dcbA3 more headers (what's the boundary?), some xml (why here ?), more headers and the content of a file.
I know how to make the request without the xml part and the 'boundary'.
Any help will be appreciated :D
The boundary is required because the form enctype is multipart/form-data, rather in this case multipart/related. The boundary is a unique string that cannot appear anywhere else in the request, and it is used to separate each element from the form, whether it is the value of a text input, or a file upload. Each boundary has its own content-type.
Curl cannot do multipart/related for you, so you will need to use a workaround, see this message from the curl mailing list for suggestions. Basically, you will have to construct most of the message yourself.
Note, the last boundary has an additional -- at the end.
This code should hopefully help get you started:
<?php
$url = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads';
$authToken = 'DXAA...sdb8'; // token you got from google auth
$boundary = uniqid(); // generate uniqe boundary
$headers = array("Content-Type: multipart/related; boundary=\"$boundary\"",
"Authorization: AuthSub token=\"$authToken\"",
'GData-Version: 2',
'X-GData-Key: key=adf15....a8dc',
'Slug: video-test.mp4');
$postData = "--$boundary\r\n"
."Content-Type: application/atom+xml; charset=UTF-8\r\n\r\n"
.$xmlString . "\r\n" // this is the xml atom data
."--$boundary\r\n"
."Content-Type: video/mp4\r\n"
."Content-Transfer-Encoding: binary\r\n\r\n"
.$videoData . "\r\n" // this is the content of the mp4
."--$boundary--";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
Hope that helps.

Categories