Jump to content
  • Advertisement
Sign in to follow this  
spraff

[web] PHP can't perform HTTP requests within my own site

This topic is 3334 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there. I am trying to set up a web site to respond to e-mail events. I have receive_email.php as the intended target and at the moment it's a stub:
<?php
$fd = fopen("php://stdin", "r");
$m = stream_get_contents($fd);
if (FALSE===$m) $m="ERROR";
fclose($fd);
file_put_contents ("/tmp/x", $m);
echo "FINISHED";
?>

I am testing this with the following dummy_send_email.php:
<?php
$m = $_POST['message'];
if (strlen($m)) {
	$s=fsockopen('localhost',80,$error_n,$error_s,30);
	if (!$s) {
		echo "<h1>HTTP ERROR</h1><p>$error_n</p><p>$error_s</p>";
		exit;
	}
	$header = "POST /receive_email.php HTTP/1.0\r\n";
	$header.= "Content-Type: text/plain\r\n";
	$header.= "Content-Length: ".strlen($m)."\r\n";
	$header.= "\r\n";
	fputs ($s, $header.$m);
	$res = fgets ($s, 1024);
	fclose ($s);
	echo "<p>$res</p><p>$m</p>"; exit;
}
?>
<form method="post" action="dummy_send_email.php">
<textarea rows="25" cols="80" style="width:100%" name="message"></textarea>
<input type="submit">
</form>

When dummy_sent_email.php submits, the output is
Quote:
HTTP/1.1 200 OK {the text from the textarea}
and the file /tmp/x is empty. Problems
  • the "FINISHED" string is not read out by fgets in dummy_send_email.php
  • stdin reads zero bytes with stream_get_contents, I've tried a fgets loop with the same results -- interestingly feof is false despite this
Other PHP pages in the rest of the site are working perfectly. Any idea what's going wrong? Ubuntu/Apache Thanks for reading

Share this post


Link to post
Share on other sites
Advertisement
This looks horribly complicated. The way this is done in practice is to define a receive_email function and call it, since it's much easier AND requires less server resources than an HTTP request.

Even assuming that you really do insist on doing an HTTP request, the one you construct is flawed (you're missing a host header which is going to be expected by apache in some circumstances, and you have an unusual content-type), plus you don't really need to use sockets. Use an existing library, like cURL, which does this for you easier.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!