spiffy577 Posted February 20, 2011 Share Posted February 20, 2011 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 More sharing options...
BlueSkyIS Posted February 20, 2011 Share Posted February 20, 2011 the error is pretty specific: Bad Request Content-Type: text/html; charset=us-ascii Link to comment https://forums.phpfreaks.com/topic/228305-post-or-get-programitically/#findComment-1177329 Share on other sites More sharing options...
spiffy577 Posted February 21, 2011 Author Share Posted February 21, 2011 I understand the error. I am trying to give it exactly what it is asking for and it is still giving me a bad request error. Is there something specific I am missing? Link to comment https://forums.phpfreaks.com/topic/228305-post-or-get-programitically/#findComment-1177368 Share on other sites More sharing options...
BlueSkyIS Posted February 21, 2011 Share Posted February 21, 2011 it seems to me that you are giving it what it is specifically telling you not to give it: $contenttype = "text/xml; charset=us-ascii"; Bad Request Content-Type: text/html; charset=us-ascii Link to comment https://forums.phpfreaks.com/topic/228305-post-or-get-programitically/#findComment-1177625 Share on other sites More sharing options...
spiffy577 Posted February 21, 2011 Author Share Posted February 21, 2011 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); Link to comment https://forums.phpfreaks.com/topic/228305-post-or-get-programitically/#findComment-1177726 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.