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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace common\service\bishijie;
const BISHIJIE_LANGUAGE_CN = 1;
const BISHIJIE_LANGUAGE_EN = 3;
const BISHIJIE_LANGUAGE_KO = 4;
class BishijieService
{
// CURL requests the relevant parameters
public $useragent = 'BiShiJie PHPSDK v1.x';
public $connecttimeout = 2;
public $timeout = 30;
public $ssl_verifypeer = FALSE;
// CURL requests state-related data
public $http_header = array();
public $http_code;
public $http_info;
public $curl_error;
public $curl_response;
public $url;
protected $app_id, $app_sercet;
public static $base_url = 'https://iapi.bishijie.com/';
public function __construct($app_id, $app_sercet)
{
$this->app_id = $app_id;
$this->app_sercet = $app_sercet;
}
/**
* Get Newsflash List
*
* @param int $language BISHIJIE_LANGUAGE_*
* @param number $start_time
* @param number $end_time
* @param number $size count of newsflash, max is 500
* @return
*/
public function getNewsflashList($language, $start_time = 0, $end_time = 0, $size = 150)
{
$params = array('language' => $language);
$start_time && $params['start_time'] = $start_time;
$end_time && $params['end_time'] = $end_time;
$size && $params['size'] = $size;
return $this->get('newsflash/list', $params);
}
/**
* Gets the last 8 hours of messages that have been modified or deleted
*
* @param int $language BISHIJIE_LANGUAGE_*
* @return mixed
*/
public function getNewsflashUpdated($language)
{
$params = array('language' => $language);
return $this->get('newsflash/update_list', $params);
}
/**
* Gets the Article List ,Without Content field.
*
* @param number $page
* @param number $page_size
* @param string $language
* @return mixed
*/
public function getArticleList($page = 1, $page_size = 20, $language = BISHIJIE_LANGUAGE_CN)
{
$params = array('language' => $language, 'page' => $page, 'size' => $page_size);
return $this->get('article/list', $params);
}
/**
* Get Article detail(with content field)
*
* @param number $news_id
* @param string $language
* @return mixed
*/
public function getArticleDetail($article_id, $language = BISHIJIE_LANGUAGE_CN)
{
$params = array('language' => $language, 'id' => $article_id);
return $this->get('article/detail', $params);
}
protected function get($method, $params)
{
$params = $this->buildRequest($params);
$url = self::$base_url . $method . '?' . http_build_query($params);
$data = $this->http($url, 'GET');
if ($this->http_info['http_code'] == 405)
throw new BishijieException('This interface does not support GET method requests', 1003);
return $data;
}
protected function post($method, $params)
{
$request = $this->buildRequest($params);
$url = self::$base_url . $method;
$data = $this->http($url, 'POST', http_build_query($request));
if ($this->http_info['http_code'] == 405)
throw new BishijieException('This interface does not support POST method requests', 1004);
return $data;
}
protected function buildRequest(array $params)
{
$params['app_id'] = $this->app_id;
$params['timestamp'] = time();
ksort($params);
$params['sign'] = md5(http_build_query($params) . $this->app_sercet);
return $params;
}
/**
*
* @param string $url
* @param string $method
* @param string $postfields
* @return mixed
*/
protected function http($url, $method, $postfields = NULL)
{
$this->http_info = array();
$ci = curl_init();
curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
curl_setopt($ci, CURLOPT_HEADER, FALSE);
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
$method = strtoupper($method);
switch ($method) {
case 'POST':
curl_setopt($ci, CURLOPT_POST, TRUE);
if (!empty($postfields))
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
break;
case 'DELETE':
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
if (!empty($postfields))
$url = "{$url}?{$postfields}";
}
curl_setopt($ci, CURLOPT_URL, $url);
$response = curl_exec($ci);
$this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
$this->http_info = array_merge($this->http_info, curl_getinfo($ci));
$this->url = $url;
if ($response == false)
$this->curl_error = curl_error($ci);
$this->curl_response = $response;
curl_close($ci);
$response = json_decode($response, true);
return $response;
}
/**
* Get the header info to store.
*/
public function getHeader($ch, $header)
{
$i = strpos($header, ':');
if (!empty($i)) {
$key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
$value = trim(substr($header, $i + 2));
$this->http_header[$key] = $value;
}
return strlen($header);
}
}
class BishijieException extends \Exception
{
}