3 * Humble HTTP Agent extension for SimplePie_File
5 * This class is designed to extend and override SimplePie_File
6 * in order to prevent duplicate HTTP requests being sent out.
7 * The idea is to initialise an instance of Humble HTTP Agent
8 * and attach it, to a static class variable, of this class.
9 * SimplePie will then automatically initialise this class
14 class SimplePie_HumbleHttpAgent
extends SimplePie_File
16 protected static $agent;
20 var $headers = array();
25 var $method = SIMPLEPIE_FILE_SOURCE_NONE
;
27 public static function set_agent(HumbleHttpAgent
$agent) {
28 self
::$agent = $agent;
31 public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
32 if (class_exists('idna_convert'))
34 $idn = new idna_convert();
35 $parsed = SimplePie_Misc
::parse_url($url);
36 $url = SimplePie_Misc
::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']);
39 $this->useragent
= $useragent;
40 if (preg_match('/^http(s)?:\/\//i', $url))
42 if (!is_array($headers))
46 $this->method
= SIMPLEPIE_FILE_SOURCE_REMOTE
| SIMPLEPIE_FILE_SOURCE_CURL
;
48 foreach ($headers as $key => $value) {
49 $headers2[] = "$key: $value";
51 //TODO: allow for HTTP headers
52 // curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2);
54 $response = self::$agent->get($url);
56 if ($response === false || !isset($response['status_code'])) {
57 $this->error = 'failed to fetch URL';
58 $this->success = false;
60 // The extra lines at the end are there to satisfy SimplePie's HTTP parser.
61 // The class expects a full HTTP message, whereas we're giving it only
62 // headers - the new lines indicate the start of the body.
63 $parser = new SimplePie_HTTP_Parser($response['headers']."\r\n\r\n
");
64 if ($parser->parse()) {
65 $this->headers = $parser->headers;
66 //$this->body = $parser->body;
67 $this->body = $response['body'];
68 $this->status_code = $parser->status_code;
74 $this->error = 'invalid URL';
75 $this->success = false;