summaryrefslogtreecommitdiff
path: root/providers/HtmlFeed.php
blob: 8543444d2ed8e58eb60d391d62c0ff3942f4a219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

namespace Providers;

require_once('XmlFeed.php');
require_once('../lib/querypath/src/qp.php');

abstract class HtmlFeed extends \Providers\XmlFeed {

    private $_encoding;

    protected function __getUserAgent() {
        return trim(file_get_contents('../config/user-agent'));
    }

    private function __getHttpContent($feedUrl) {
        $header = array();
        $header[] = 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
        $header[] = 'Cache-Control: max-age=0';
        $header[] = 'Connection: keep-alive';
        $header[] = 'Keep-Alive: 300';
        $header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
        $header[] = 'Pragma: ';
        $ch = curl_init($feedUrl);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->__getUserAgent());
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        curl_setopt($ch, CURLOPT_TIMEOUT, 20);
        $page = curl_exec($ch);
        curl_close ($ch);
        $this->_encoding = mb_detect_encoding($page);
        return $page;
    }

    protected function _fetchItems() {
        $page = $this->__getHttpContent($this->_feedUrl);
        $this->_feedXml = htmlqp($page, NULL, ['convert_from_encoding' => $this->_encoding, 'convert_to_encoding' => $this->_encoding]);
        return $this->_parseFeedContent($this->_feedXml);
    }

}

?>