Jump to content

POST or GET Programitically


spiffy577

Recommended Posts

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

 

I can also do soap but that looks way more difficult. Here are those options.

 

SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

 

POST /DataImport/DataImport.asmx HTTP/1.1

Host: MYHOST.com

Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: "http://tempuri.org/ImportLeads"

 

<?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>

<ImportLeads xmlns="http://tempuri.org">

<pLeadXmlData>string</pLeadXmlData>

</ImportLeads>

</soap:Body>

</soap:Envelope>

 

HTTP/1.1 200 OK

Content-Type: text/xml; charset=utf-8

Content-Length: length

 

<?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>

<ImportLeadsResponse xmlns="http://tempuri.org">

<ImportLeadsResult>string</ImportLeadsResult>

</ImportLeadsResponse>

</soap:Body>

</soap:Envelope>

 

SOAP 1.2

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

 

POST /DataImport/DataImport.asmx HTTP/1.1

Host: MYHOST.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>

<ImportLeads xmlns="http://tempuri.org">

<pLeadXmlData>string</pLeadXmlData>

</ImportLeads>

</soap12:Body>

</soap12:Envelope>

 

HTTP/1.1 200 OK

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>

<ImportLeadsResponse xmlns="http://tempuri.org">

<ImportLeadsResult>string</ImportLeadsResult>

</ImportLeadsResponse>

</soap12:Body>

</soap12:Envelope>

 

 

 

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 "

 

   $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/xml; 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);

Link to comment
https://forums.phpfreaks.com/topic/228305-post-or-get-programitically/
Share on other sites

So is there a way to convert the string xml to an official xml format? It supposedly needs xml. Now as for the error, is that telling me I should change the content type to html or do I have to convert the string to xml? Basically is it saying that you sent me html and I want xml or is it saying that you sent me something but I need html? Do you have any specifics on code that would help?

 

Here is the POST version. Still isn't working.

 

 $xmlpacket = '<x:LeadImport VendorID="###" xmlns:x="urn:LeadImport">
<Lead>
<VendorLeadID>'.$file_contents.'</VendorLeadID>
<FirstName>'.$firstname.'</FirstName>
<LastName>'.$lastname.'</LastName>
<StreetAddress>'.$address.'</StreetAddress>
<City>'.$city.'</City>
<State>'.$state.'</State>
<Zip>'.$zip.'</Zip>
<HomePhone>'.$phone.'</HomePhone>
<Email>'.$email.'</Email>
<Product>'.$type.'</Product>
<ContactDate>'.date("Y-d-m").'</ContactDate>
<BestTimeToCall>'.$availability.'</BestTimeToCall>
<Comments>TESTING, PLEASE DISREGARD</Comments>
<LeadSource ID="###"></LeadSource>
</Lead>
</x:LeadImport>';

$fp = fsockopen("MYHOST, 80, $errno, $errstr, 30);
if (!$fp)
{
echo 'Could not open connection.';
}
else
{
$contentlength = strlen($xmlpacket);

$out = "POST /DataImport/DataImport.asmx/ImportLeads HTTP/1.1";
$out .= "Host: MYHOST";
$out .= "Content-Type: application/x-www-form-urlencoded";
$out .= "Content-Length: $contentlength ";
$out .= "pLeadXmlData=$xmlpacket";

        fwrite($fp, $out);
while (!feof($fp))
{
	$theOutput .= fgets($fp, 128);
}
echo $theOutput;
fclose($fp);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.