Jamie Bull Jamie Bull - 26 days ago 5x
PHP Question

Opening connection to HTTP server with PHP

I'm trying to open a connection to a webserver I have hosted on an Amazon EC2 instance. I can access the port with a GET request from my browser which gives me the following:

Started HTTP server...
- <IP redacted> [19/Jul/2013 13:49:24] code 501, message Unsupported method ('GET')
- <IP redacted> [19/Jul/2013 13:49:24] "GET / HTTP/1.1" 501 -

This is as expected as I haven't implemented GET on the webserver - but I get no response at all using the following snippet to POST to the same port.

$req = 'verify=true';

$header = "POST / HTTP/1.1\r\n";
$header .= "Host: ec2-55-555-555-555.compute-1.amazonaws.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ("ec2-55-555-555-555.compute-1.amazonaws.com", 8080, $errno, $errstr, 30);

fputs ($fp, $header . $req);

fclose ($fp);

It definitely runs and reaches the end but the
call is timing out. What's missing or is there but shouldn't be?


The easiest way to interact with an HTTP server from PHP would be to use curl.

It's a good library with some great features. It works well.

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "Your_url_here");

$resp = curl_exec($curl);