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

## 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 on other sites
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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627697
• Total Posts
2978680

• 20
• 14
• 12
• 10
• 12