Send HTTP Header Problem

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
richid
Forum Newbie
Posts: 2
Joined: Fri Oct 07, 2005 2:32 pm

Send HTTP Header Problem

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I've found that the following works:

http://timvw.madoka.be/programming/php/client.txt
richid
Forum Newbie
Posts: 2
Joined: Fri Oct 07, 2005 2:32 pm

Post by richid »

Thanks, worked like a charm. Not really sure what I was doing wrong before, but oh well.
Post Reply