SOAP XML Request Problems

Firstly, hello to everyone it’s my first time using this site.

Now to the problem!

I’m trying to send an XML SOAP request to a remote server and then gather and use the response. This is the XML file I’m trying to send:


<?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:Header>
    	 <AuthHeader xmlns="http://webservices.website.net/">
      		 <sGroupGUID>GROUPID</sGroupGUID>
      		 <sPassword>PASSWORD</sPassword>
      		 <sAdminGUID></sAdminGUID>
      		 <sAdminPassword></sAdminPassword>
      		 <sCompanyGUID></sCompanyGUID>
    	 </AuthHeader>
     </soap:Header>
    <soap:Body>
    		<AddUser xmlns="http://webservices.website.net/">
      			 <User>
        			 <sGUID></sGUID>
        			 <sUserName>TestLind</sUserName>
        			 <sEmail>Test@test.com</sEmail>
        			 <sFullName>Test</sFullName>
        			 <sPin></sPin>
        			 <iWeight>150</iWeight>
        			 <iTimeZoneOffset>UK</iTimeZoneOffset>
        			 <UnitPreference>METRIC</UnitPreference>
        			 <iDailyStepGoal></iDailyStepGoal>
        			 <iDailyCalorieGoal></iDailyCalorieGoal>
        			 <iDailyDistanceGoal></iDailyDistanceGoal>
        			 <iDailyTimeGoal></iDailyTimeGoal>
        			 <iHeight>160</iHeight>
        			 <cGender>M</cGender>
      			 </User>
   		 </AddUser>
  	 </soap:Body>
 </soap:Envelope>

I’ve tried two different approaches, and am getting two different errors with each, if any one could shine some light on either of these errors.

Method 1: cURL ($xmlDocument being the above)


public function cURL($xmlDocument)
 		{
 			$URL = "http://sub.website.net/";
 		
 			
				$header[] = "POST: /Webservices/EmbeddedManagement.asmx HTTP/1.1 \\r\
";
				$header[] = "Host: embedded.website.com \\r\
";
				$header[] = "Content-Type: text/xml; charset=utf-8 \\r\
";
				$header[] = "Content-Length: ".strlen($xmlDocument)." \\r\
";
				$header[] = "SOAPAction: 'http://webservices.website.net/AddUser' \\r\
";
				
 				
 			$ch = curl_init($URL);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlDocument);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$output = curl_exec($ch);
			
			if(curl_errno($ch))
    			print curl_error($ch);
			else
    		curl_close($ch);
    		
			return $output;
 		}

The response header for this approach returns “HTTP/1.1 200 OK” but also shows a “Bad Request (Invalid Hostname)”. I don’t think my header values are being taken by the cURL request because when assessing the request with HttpFox, they read: “HOST: localhost:8888”,“Content-Length: 108”,etc.

Method 2: fsockopen


public function postXMLToURL ($server, $path, $xmlDocument) {
		
			$contentLength = strlen($xmlDocument);
			
			$fp = fsockopen($server, 80, $errno, $errstr, 30);
			fwrite($fp, "POST: $path HTTP/1.1 \\r\
");
			fwrite($fp, "Host: $server \\r\
");
			fwrite($fp, "Content-Type: text/xml; charset=utf-8 \\r\
");
			fwrite($fp, "Content-Length: $contentLength \\r\
");
			fwrite($fp, "SOAPAction: 'http://webservices.website.net/AddUser' \\r\
");
			fwrite($fp, "Connection: close\\r\
\\r\
");
			fwrite($fp, $xmlDocument);
			
			$result = '';
		
			while (!feof($fp)) {
				$result .= fgets($fp, 128);
				echo "File pointer line returned <br />"; //Debug message
			}
			
			return $result;
		}

This method returns the error: Bad Request (Invalid Verb)

Any ideas would be much appreciated. Also if you know of any better ways to do what I’ve done there please do let me know as I’ve only just started developing with Web Services.

Thanks in advance

$

Colon after the POST needs removing

Hello frnds,

my header contains following data,

$header = array(
	  "POST /XXXX.asmx HTTP/1.1\\r\
",
	  "Host: demo.XXXX.in\\r\
",
	  "Content-Type: text/xml; charset=utf-8\\r\
",
	  "Content-length: ".$soap_request_length."\\r\
", 
	  "SOAPAction: \\"http://tempuri.org/VerifyLogin\\"\\r\
",
	 
  );

and i am geting error like,

Length Required

HTTP Error 411. The request must be chunked or have a content length.

Please help me to solve this problem.
Thank you.

Why not simply use PHP’s buit-in SoapClient class?

The request says 1.1, so you’d have to “chunk” your SOAP request body. Try HTTP/1.0

Perhaps supplying the content-length will be enough:


$header = array(
	  "POST /XXXX.asmx HTTP/1.1\\r\
",
	  "Host: demo.XXXX.in\\r\
",
	  "Content-Type: text/xml; charset=utf-8\\r\
",
	  "Content-length: ".$soap_request_length."\\r\
", 
	  "SOAPAction: \\"http://tempuri.org/VerifyLogin\\"\\r\
",
	 
  );

Can you test the value for $soap_request_length and make sure it actually contains something?