HTTP Request API
MetaPhilter includes a class to execute HTTP requests.
The constructor of the httpRequest
class:
httpRequest(string $url [, string $method [, string $data [, array $headers] ] ])
The $headers
parameter should be an array of strings with standard HTTP headers (e.g. 'Content-Type: text/xml')
If $data
is specified, a 'Content-Length' header is automatically sent to the server in the header block.
The resulting object will have four member variables:
response
The response code returned by the server (200 for 'OK', 404 for 'Not found', etc.)
headers
An associate array of the parameters sent in the header block by the server, where each parameter name is a lower-case key.
url
The resulting url of the fetched object after any redirection.
content
The entity data returned from the server.
To make a simple GET request:
$response=new httpRequest('http://google.com/'); print $response->content;
To POST some data:
$response=new httpRequest('http://non-existant.com/post.php', POST, '<?xml version="1.0"?> <cms>MetaPhilter</cms>', array('Content-Type: text/xml')); if ($response->headers['content-type']!='text/xml') print 'Incorrect data return!'; else print $response->content;