Skip to content


XML POST Proxy Script using PHP and libcurl

This is the simplest answer I could find for a client needing to POST an XML SOAP message to a server in another domain.

Not sure on the security because the POST isn’t validated so it just passes on whatever the client sends.

libcurl has a lot of options

 
<?php
 
#
#
// Make sure the user is posting
#
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
 
	// Read the POST input from stdin
	$postText = trim(file_get_contents('php://input'));
 
} else {
 
    exit("We only offer POST");
 
};
 
# The client needs to set a SOAPTarget header pointing to the URL of the Webservice you need
# in jquery you use the beforeSend: ajax option and create a function such as
# function(xhr) { xhr.setRequestHeader( "SOAPTarget", "http://yourserver.com/your/webservice/endpoint") };
#
	$url=$_SERVER['HTTP_SOAPTARGET'];
 
# start your curl session
	$session = curl_init($url);
 
# set the libcurl options
    curl_setopt ($session, CURLOPT_POST, true);
    curl_setopt ($session, CURLOPT_POSTFIELDS, $postText);
 
# soapaction  header _must_ be set
	curl_setopt ($session, CURLOPT_HTTPHEADER, array('SOAPAction: ""',
                                                                                "Content-Type: text/xml"));
	curl_setopt($session, CURLOPT_HEADER, false);
	curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($session, CURLOPT_FORBID_REUSE, 1);
	curl_setopt($session, CURLOPT_FRESH_CONNECT, 1);
 
// Make the call and store the reply in $xml
	$xml = curl_exec($session);
 
 /* foreach($_SERVER as $h=>$v)
    if(ereg('HTTP_(.+)',$h,$hp))
      echo "<li>$h = $v</li>\n";;
*/
 
# tell the client to expect an xml reply
	header("Content-Type: text/xml");
 
# then send the the xml reply from the webservice
	echo $xml;
 
# close the session to free up resources
	curl_close($session);
 
/* This dumps all the session variables from PHP woo hoo! var_dump(get_defined_vars());  */
 
?>

Posted in IT Tips.

Tagged with , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.



Page optimized by WP Minify WordPress Plugin