By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

My code gets a HTTP 400 error when I post to IIS 6 servers but works on IIS 5 servers. ??? Something wrong in the header. ?

P: n/a
<?php

// pullpage function by Nick bouton http://www.nickbouton.com/.

$CustomerID = "IDHERE";
$method = "POST";
$host = "xml.mydata.com";
$usepath = "/xml.asp";

//print all vars in an array
function printa($variable) {
echo '<pre>';
//echo "<b>".strtoupper("$variable").":</b>";
print_r($variable);
echo "</pre>";
}

function pullpage ($method, $host, $usepath, $postdata) {
# open socket to filehandle
$fp = fsockopen( $host, 80, &$errno, &$errstr, 120 );

# user-agent name
$ua = "Mozilla/1.0";

if( !$fp ) {
print "$errstr ($errno)<br>\n";
} else {
if ($method == "GET") {
fputs($fp, "GET $usepath HTTP/1.0\n");
} elseif ($method == "POST") {
fputs( $fp, "POST $usepath HTTP/1.0\n");
}
fputs($fp, "User-Agent: ".$ua."\n");
fputs($fp, "Accept: */*\n");

if($method == "POST") {
$strlength = strlen($postdata);
fputs($fp, "Content-type: text/xml\n");
fputs($fp, "Content-length:
".$strlength."\n\n");
fputs($fp, $postdata."\n");
}

fputs( $fp, "\n");
$output = "";

# while content exists, keep retrieving document in 1K
chunks
while (!feof($fp)) {
$output .= fgets( $fp, 1024 );
}

fclose($fp);
}
return $output;
}

//define all vars
$xml_array["Latitude"]="";
$xml_array["Longitude"]="";
$xml_array["ParsedAddressRange"]="";
$xml_array["ParsedPreDirection"]="";
$xml_array["ParsedStreetName"]="";
$xml_array["ParsedSuffix"]="";
$xml_array["ParsedPostDirection"]="";
$xml_array["ParsedSuiteName"]="";
$xml_array["ParsedSuiteRange"]="";
$xml_array["ParsedGarbage"]="";
$xml_array["Company"]="";
$xml_array["Address"]="";
$xml_array["Address2"]="";
$xml_array["Suite"]="";
$xml_array["City"]="";
$xml_array["CityAbbreviation"]="";
$xml_array["State"]="";
$xml_array["Zip"]="";
$xml_array["Plus4"]="";
$xml_array["AreaCodeOfZip"]="";
$xml_array["CarrierRoute"]="";
$xml_array["DeliveryPointCode"]="";
$xml_array["DeliveryPointCheckDigit"]="";
$xml_array["CountyFips"]="";
$xml_array["CountyName"]="";
$xml_array["TimeZone"]="";
$xml_array["TimeZoneCode"]="";
$xml_array["AddressErrorCode"]="";
$xml_array["AddressErrorString"]="";
$xml_array["Msa"]="";
$xml_array["Pmsa"]="";
$xml_array["AddressTypeCode"]="";
$xml_array["AddressTypeString"]="";
$xml_array["Urbanization"]="";
$xml_array["CongressionalDistrict"]="";
$xml_array["PrivateMailBox"]="";
$xml_array["Lacs"]="";
$xml_array["AddressStatusCode"]="";
$xml_array["AddressDatabaseDate"]="";

//set fields passed from form
foreach ($xml_array as $key => $value) {
if(isset($_POST[$key])) { $xml_array[$key]=$_POST[$key]; }
}

//check for custom_xml and override
if (isset($_POST["custom_xml"])) { if(strlen($_POST["custom_xml"])) {
$custom_xml = $_POST["custom_xml"]; } }

// create first part of xml post data
$postdata = "<?xml version=\"1.0\" ?><RecordSet><CustomerID>".
$CustomerID ."</CustomerID><Record>";

//create middle record data of xml post
while (list($key, $value) = each (${"xml_array"})) {
$postdata .= "<". $key.">" .$value."</".$key .">";
$output_array[$key] = "";
}

// append last part of xml post data
$postdata .= "</Record></RecordSet>";

// if custom xml disregard above and use custom xml instead
// also, you can comment out the form and post directly to this php
script
// setting the posted xml variable name to custom_xml

if(isset($custom_xml)) {
$postdata = "<?xml version=\"1.0\" ?><RecordSet><CustomerID>".
$CustomerID ."</CustomerID>". $custom_xml . "</RecordSet>";
}

//do the HTTP POST
$return_content = pullpage( "POST", $host, $usepath , $postdata);
$server_response = $return_content;

//create loop and extract single record for multiple record return

while (ereg('<Record>',$return_content)) {
if(is_array($output_array)) { reset($output_array); }
$start = "<Record>";
$end = "</Record>";
$startpos = strpos($return_content, $start);
$endpos = strpos($return_content, $end) + strlen($end);
$record_content = trim(substr($return_content, $startpos,
$endpos-$startpos));

//remove record content from xml output
$return_content = trim(substr($return_content, $startpos +
strlen($record_content), strlen($return_content)));

if(is_array($output_array)) {
while (list($key, $value) = each (${"output_array"}))
{
$start ="<". $key.">";
$end = "</".$key .">";
$startpos = strpos($record_content, $start) +
strlen($start);
$endpos = strpos($record_content, $end);
$output_array[$key] =
trim(substr($record_content, $startpos, $endpos-$startpos));
if (!strpos($record_content, $start)){
$output_array[$key]=""; }
}
}
}

// note - there is no error checking of the input xml and not a fancy
xml parser or anything so that this will work with
// most php versions
include('mydata_form.php');

?>
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.