]>
Commit | Line | Data |
---|---|---|
42c80841 NL |
1 | <?php\r |
2 | /**\r | |
3 | * Humble HTTP Agent extension for SimplePie_File\r | |
4 | * \r | |
5 | * This class is designed to extend and override SimplePie_File\r | |
6 | * in order to prevent duplicate HTTP requests being sent out.\r | |
7 | * The idea is to initialise an instance of Humble HTTP Agent\r | |
8 | * and attach it, to a static class variable, of this class.\r | |
9 | * SimplePie will then automatically initialise this class\r | |
10 | * \r | |
11 | * @date 2011-02-28\r | |
12 | */\r | |
13 | \r | |
14 | class SimplePie_HumbleHttpAgent extends SimplePie_File\r | |
15 | {\r | |
16 | protected static $agent;\r | |
17 | var $url;\r | |
18 | var $useragent;\r | |
19 | var $success = true;\r | |
20 | var $headers = array();\r | |
21 | var $body;\r | |
22 | var $status_code;\r | |
23 | var $redirects = 0;\r | |
24 | var $error;\r | |
25 | var $method = SIMPLEPIE_FILE_SOURCE_NONE;\r | |
26 | \r | |
27 | public static function set_agent(HumbleHttpAgent $agent) {\r | |
28 | self::$agent = $agent;\r | |
29 | }\r | |
30 | \r | |
31 | public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {\r | |
32 | if (class_exists('idna_convert'))\r | |
33 | {\r | |
34 | $idn = new idna_convert();\r | |
35 | $parsed = SimplePie_Misc::parse_url($url);\r | |
36 | $url = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']);\r | |
37 | }\r | |
38 | $this->url = $url;\r | |
39 | $this->useragent = $useragent;\r | |
40 | if (preg_match('/^http(s)?:\/\//i', $url))\r | |
41 | {\r | |
42 | if (!is_array($headers))\r | |
43 | {\r | |
44 | $headers = array();\r | |
45 | }\r | |
46 | $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;\r | |
47 | $headers2 = array();\r | |
48 | foreach ($headers as $key => $value) {\r | |
49 | $headers2[] = "$key: $value";\r | |
50 | }\r | |
51 | //TODO: allow for HTTP headers\r | |
52 | // curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2);\r | |
53 | \r | |
54 | $response = self::$agent->get($url);\r | |
55 | \r | |
56 | if ($response === false || !isset($response['status_code'])) {\r | |
57 | $this->error = 'failed to fetch URL';\r | |
58 | $this->success = false;\r | |
59 | } else {\r | |
60 | // The extra lines at the end are there to satisfy SimplePie's HTTP parser.\r | |
61 | // The class expects a full HTTP message, whereas we're giving it only\r | |
62 | // headers - the new lines indicate the start of the body.\r | |
63 | $parser = new SimplePie_HTTP_Parser($response['headers']."\r\n\r\n");\r | |
64 | if ($parser->parse()) {\r | |
65 | $this->headers = $parser->headers;\r | |
66 | //$this->body = $parser->body;\r | |
67 | $this->body = $response['body'];\r | |
68 | $this->status_code = $parser->status_code;\r | |
69 | }\r | |
70 | }\r | |
71 | }\r | |
72 | else\r | |
73 | {\r | |
74 | $this->error = 'invalid URL';\r | |
75 | $this->success = false;\r | |
76 | }\r | |
77 | }\r | |
78 | }\r | |
ec397236 | 79 | ?> |