-
Notifications
You must be signed in to change notification settings - Fork 1
/
news.php
53 lines (45 loc) · 1.32 KB
/
news.php
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
47
48
49
50
51
52
53
<?php
include "simple_html_dom.php" ;
class News {
public function getHeaders() {
$headers['Host'] = 'news.baidu.com';
$headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36';
$headerArr = array();
foreach($headers as $n => $v) {
$headerArr[] = $n .':' . $v;
}
return $headerArr;
}
public function getResponse() {
$headers = $this->getHeaders();
$ch = curl_init();
$url = 'http://news.baidu.com/';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$out = curl_exec($ch);
curl_close ($ch);
return $out;
}
public function parseHTML() {
$content = $this->getResponse();
$html = new simple_html_dom();
$html->load($content);
$result = $html->find("#pane-news .hotnews strong a");
$array = array();
foreach($result as $v) {
array_push($array, iconv('GB2312', 'UTF-8', $v->innertext));
}
$arr = array();
$i = 0;
for ($i = 0; $i < 3; $i ++) {
if ($array[$i]) {
$arr[$i] = $array[$i];
} else {
$arr[$i] = "今日有爆炸性新闻";
}
}
return $arr;
}
}