POST or GET programatically?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spiffy577
Forum Newbie
Posts: 3
Joined: Tue Dec 25, 2007 12:09 am

POST or GET programatically?

Post by spiffy577 »

Hello-

I have a project in where I am trying to post some xml code to a webservice but I keep getting a bad request error.

Here is the possible options for POST and GET. I cannot get either to work.
HTTP GET
The following is a sample HTTP GET request and response. The placeholders shown need to be replaced with actual values.
GET /DataImport/DataImport.asmx/ImportLeads?pLeadXmlData=string HTTP/1.1
Host: MYHOST.com

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org">string</string>

HTTP POST
The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.
POST /DataImport/DataImport.asmx/ImportLeads HTTP/1.1
Host: MYHOST.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length

pLeadXmlData=string

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org">string</string

Here is my code... Can you help? What am I missing? I keep getting a Bad Request error.
"HTTP/1.1 400 Bad Request Content-Type: text/html; charset=us-ascii Server: Microsoft-HTTPAPI/2.0 Date: Fri, 18 Feb 2011 21:01:53 GMT Connection: close Content-Length: 311 "

Code: Select all

$xmldata = '<?xml version="1.0" encoding="utf-8" ?>';
$xmldata .= '<x:LeadImport VendorID="###" xmlns:x="urn:LeadImport">';
$xmldata .= '<Lead>'."\n";
$xmldata .= '<FirstName>'.$firstname.'</FirstName>';
$xmldata .= '<LastName>'.$lastname.'</LastName>';
$xmldata .= '</Lead>';
$xmldata .= '</x:LeadImport>';

$port = 80; // Tried 443 for the https, didn't work
$host = "MYHOST.com";  // an alias
$method = "GET";  // Tried POST also
$contenttype = "text/html; charset=us-ascii";
$path = "/DataImport/DataImport.asmx";
$data = $xmldata;

// script
if($port == 443)
      $sslhost = "ssl://".$host;
else
      $sslhost = $host;
$fp = fsockopen($sslhost, $port);
fwrite($fp, "$method  /DataImport/DataImport.asmx/ImportLeads?pLeadXmlData=".$data." HTTP/1.1\r\n");
fwrite($fp, "Host: $host\r\n");
fwrite($fp, "Content-type: $contenttype\r\n");
fwrite($fp, "Content-length: ".strlen($data)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");

$response = '';
while (!feof($fp))
{
$response .= fread($fp, 1024);
}

print $response . "<BR>\n";
fclose($fp);
Post Reply