Skip to content

Instantly share code, notes, and snippets.

@TechplexEngineer
Last active August 29, 2015 14:03
Show Gist options
  • Save TechplexEngineer/2ce2b838e6715c7c0c2e to your computer and use it in GitHub Desktop.
Save TechplexEngineer/2ce2b838e6715c7c0c2e to your computer and use it in GitHub Desktop.
This Gist adds f4stdev and rudimentary POST request capability. -- Simple GET & POST requests in PHP.
<?php
/*
* Simple HttpRequest example using PHP
* tom slankard
* POST capability: Techplex Engineer
* https://gist.github.com/twslankard/989974
* http://stackoverflow.com/a/20621965/429544
*/
class HttpRequest {
public $url = null;
public $method = 'GET';
public $body = null;
public $headers = Array();
public $allow_redirect = true;
private $url_info = null;
private $host_name = null;
private $host_ip = null;
private $response_body = null;
private $response_headers = Array();
private $response_code = null;
private $response_message = null;
private $port = null;
private $verbose = true;
private $raw_post_data = "";
public function __construct($url, $method = 'GET') {
$this->url = $url;
$this->method = $method;
// parse url
$this->url_info = parse_url($url);
$this->host_name = $this->url_info['host'];
$this->host_ip = gethostbyname($this->host_name);
// get port number given the scheme
if(!isset($this->url_info['port'])) {
if($this->url_info['scheme'] == "http")
$this->port = 80;
else if($this->url_info['scheme'] == "https")
$this->port = 443;
} else {
$this->port = $this->url_info['port'];
}
// add default headers
$this->headers["Host"] = "$this->host_name";
$this->headers["Connection"] = "close";
}
public function setRawPostData($data)
{
if ($this->method != "POST") {
return false;
}
$this->raw_post_data = $data;
}
private function constructRequest() {
$path = "/";
if(isset($this->url_info['path']))
$path = $this->url_info['path'];
$req = "$this->method $path HTTP/1.1\r\n";
foreach($this->headers as $header => $value) {
$req .= "$header: $value\r\n";
}
$req .= "Content-Length: ".strlen($this->raw_post_data)."\r\n";
$req .= "\r\n";
$req .= $this->raw_post_data;
return "$req\r\n";
}
/// reads a line from a file
function readLine($fp)
{
$line = "";
while (!feof($fp)) {
$line .= fgets($fp, 2048);
if (substr($line, -1) == "\n") {
return rtrim($line, "\r\n");
}
}
return $line;
}
public function send() {
$fp = fsockopen($this->host_ip, $this->port);
if (! $fp) return False;
// construct request
$request = $this->constructRequest();
// write request to socket
fwrite($fp, $request);
// read the status line
$line = $this->readline($fp);
$status = explode(" ", $line);
// make sure the HTTP version is valid
if(!isset($status[0]) || !preg_match("/^HTTP\/\d+\.?\d*/", $status[0]))
die("Couldn't get HTTP version from response.");
// get the response code
if(!isset($status[1]))
die("Couldn't get HTTP response code from response.");
else $this->response_code = $status[1];
// get the reason, e.g. "not found"
if(!isset($status[2]))
die("Couldn't get HTTP response reason from response.");
else $this->response_reason = $status[2];
// read the headers
do {
$line = $this->readLine($fp);
if($line != "") {
$header = explode(":", $line);
$this->response_headers[$header[0]] = ltrim($header[1]);
}
} while(!feof($fp) && $line != "");
// read the body
$this->response_body = "";
do {
$line = $this->readLine($fp); {
if($line)
$this->response_body .= "$line\n";
}
} while(!feof($fp));
// close the connection
fclose($fp);
return TRUE;
}
public function getStatus() {
return $this->response_code;
}
public function getHeaders() {
return $this->response_headers;
}
public function getResponseBody() {
return $this->response_body;
}
}
<?php
include_once("http.php");
//Sample GET Request
$req = new HttpRequest("http://httpbin.org/get", "GET");
$req->headers["Connection"] = "close";
$req->send() or die("Couldn't send!");
echo( $req->getResponseBody() );
//Sample POST Request
$data = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
);
$request = new HTTPRequest("http://httpbin.org/post", 'POST');
$request->setRawPostData(http_build_query($data)); //ke1=value1&key2=value2&key3=value3
$req->send() or die("Couldn't send!");
echo( $req->getResponseBody() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment