-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.php
152 lines (109 loc) · 3.99 KB
/
response.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
if (empty($_POST) || empty($_POST['search_term'])) die('Please enter the URL');
require_once ('../libs/simplehtmldom/simple_html_dom.php');
// googleのsitemap xmlを使ってurlを抽出する
//$google_xml_url = 'http://nob-log.info/sitemap.xml';
$google_xml_url = $_POST['search_term'];
$limit = 20;
if (!isValidURL($google_xml_url)) die ('Please enter the URL');
//if (!isValidXml($google_xml_url)) die ('Please supply the xml');
$hash_key = md5($google_xml_url);
if (($ranking = apc_fetch($hash_key)) == false) {
// urlのリストを作成する
$xml = simplexml_load_file($google_xml_url);
foreach($xml->url as $data){
if (is_object($data->loc)) {
$url = (string) $data->loc;
} else {
$url = $data->loc;
}
// fqlを作成する
$fql = 'SELECT url, like_count FROM link_stat WHERE url IN " ' . $url .'"';
$fql_query_url = "https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);
$urls[] = $fql_query_url;
}
$fql_query_obj = getMultiContents($urls, 20);
//$fql_query_obj = json_decode($fql_query_result, true);
if (empty($fql_query_obj)) die('not found');
foreach ($fql_query_obj as $key => $row) {
$like_count[$key] = $row['like_count'];
}
array_multisort($like_count, SORT_DESC, $fql_query_obj);
$ranking = array_slice($fql_query_obj, 0, $limit);
apc_store($hash_key, $ranking);
}
$string = "<tr>\n";
$string .= "<th>title</th>\n";
$string .= "<th>liked</th>\n";
$string .= "</tr>\n";
foreach ($ranking as &$val) {
//if ($val['like_count'] == 0) continue;
$val['title'] = (getPageTitle($val['url']));
$string .= "<tr>";
$string .= '<td><a href="'.$val['url'].'" target="_blank">'.$val['title']."</a></td>\n";
$string .= "<td>".$val['like_count']."</td>\n";
$string .= "</tr>\n";
}
echo $string;
exit;
function getPageTitle($url) {
// なぜか file_get_contentsが動かないのでとりあえず…
return $url;
$html = file_get_contents($url);
$html = mb_convert_encoding($html, mb_internal_encoding(), "auto" );
if ( preg_match( "/<title>(.*?)<\/title>/i", $html, $matches) ) {
return $matches[1];
} else {
return $url;
}
}
function isValidURL($url) {
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}
function isValidXml($url) {
$parse_url = parse_url($url);
return (pathinfo($parse_url['path'], PATHINFO_EXTENSION) == 'xml');
}
/**
* 複数URLのコンテンツ、及び通信ステータスを一括取得する。
* サンプル:
* $urls = array( "http://〜", "http://〜", "http://〜" );
* $results = getMultiContents($urls);
* print_r($results);
*/
function getMultiContents( $url_lists, $max ) {
$results = array();
foreach(array_chunk($url_lists, $max) as $url_list) {
// マルチハンドルの用意
$mh = curl_multi_init();
// URLをキーとして、複数のCurlハンドルを入れて保持する配列
$ch_list = array();
// Curlハンドルの用意と、マルチハンドルへの登録
foreach( $url_list as $url ) {
$ch_list[$url] = curl_init($url);
curl_setopt($ch_list[$url], CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch_list[$url], CURLOPT_TIMEOUT, 1); // タイムアウト秒数を指定
curl_multi_add_handle($mh, $ch_list[$url]);
}
// 一括で通信実行、全て終わるのを待つ
$running = null;
do { curl_multi_exec($mh, $running); } while ( $running );
// 実行結果の取得
foreach( $url_list as $url ) {
// ステータスとコンテンツ内容の取得
//$results[$url] = curl_getinfo($ch_list[$url]);
//$results[] = reset(json_decode(curl_multi_getcontent($ch_list[$url]), true));
$res = json_decode(curl_multi_getcontent($ch_list[$url]), true);
if (!empty($res)) {
$results[] = $res[0];
}
// Curlハンドルの後始末
curl_multi_remove_handle($mh, $ch_list[$url]);
curl_close($ch_list[$url]);
}
// マルチハンドルの後始末
curl_multi_close($mh);
}
// 結果返却
return $results;
}