Page 1 of 1

Send HTTP Header Problem

Posted: Fri Oct 07, 2005 4:02 pm
by richid
I'm currently writing a script that will send XML formatted text to an ASP script located on a remote server. I'm having problems with it, and its returning this: "Bad Request (Invalid Hostname)" I know that the url and path are correct because I can send it a string directly through the browser and I get something back. Any help would be greatly appreciated.

Code: Select all

$server = "api-test.corporate.marketworks.com";
$port = 80;
$path = "/api/apiCall.asp";
$s_out = "test";

http_post_url($server, $port, $path, $s_out);

function http_post_url($server, $port, $path, $post_data) {

	$user_agent = $_SERVER['HTTP_USER_AGENT'];
	$content_length = strlen($post_data);


	$fp = fsockopen($server, $port, $errno, $errstr);
	if (!$fp) {
		return false;
	}

	fputs($fp, "POST $path HTTP/1.1\r\n");
	fputs($fp, "User-Agent: $user_agent");
	fputs($fp, "Host: $server\r\n");
	fputs($fp, "Content-Type: text/xml\r\n");
	fputs($fp, "Content-Length: $content_length\r\n");
	fputs($fp, "\r\n");
	fputs($fp, $post_data);
	
	$ret = "";
	while (!feof($fp))
		$ret.= fgets($fp, 1024);
        
	fclose($fp);
	
	echo $errstr;
	
	echo $ret . "\n\n";
}
Thanks in advance.

Posted: Fri Oct 07, 2005 5:40 pm
by timvw
I've found that the following works:

http://timvw.madoka.be/programming/php/client.txt

Posted: Mon Oct 10, 2005 2:00 pm
by richid
Thanks, worked like a charm. Not really sure what I was doing wrong before, but oh well.