My favorites
▼
|
Sign in
p2-php-framework
This is an MVC framework and library on PHP.
Project Home
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
Service
/
Base.php
‹r238
r253
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
<?php
/**
* P2_Service_Base
*
* require
* * libxml 2.6.21+ (PHP 5.1.2+)
* * P2_PublicVars
*
* @version 2.2.7a
* @see http://code.google.com/p/p2-php-framework/
* @license The MIT license (http://www.opensource.org/licenses/mit-license.php)
*/
class P2_Service_Base extends P2_PublicVars {
const SATUS_INIT = 0;
const SATUS_IDVALID_XML = 1000; //XML不整合
const SATUS_LOCK_ERROR = 1100; //ロック取得失敗
const SATUS_SERVICE_ERROR = 2000; //サービス固有のエラー
public $status = self::SATUS_INIT;
public $responseCode; //エラー解析用
public $response; //エラー解析用
public $url; //エラー解析用
protected $_params = array();
private $_workDir;
private $_interval;
/**
* コンストラクタ
*/
public function __construct() {
}
/**
* @param string $workDir /path/to/workdir
* @param integer $intervalSecond (optional)
*/
public function setRequestInterval($workDir, $intervalSecond = 1) {
$this->_workDir = $workDir;
$this->_interval = $intervalSecond;
}
public function addParam($key, $value) {
$this->_params[$key] = $value;
}
public function clearParams() {
$this->_params = array();
}
public function request($url, $xmlFlg = true, $method = 'get', $toEncoding = 'UTF-8') {
$this->url = '';
$method = strToLower($method);
//パラメータをQueryStringに変換
$qs = $this->_arr2qs($toEncoding);
//ロック取得
$fp = $this->_getLockFp();
if (!$fp) {
$this->status = self::SATUS_LOCK_ERROR;
throw new Exception('ロック取得失敗');
}
//ファイル取得
$resp = $this->_getFile($method, $url, $qs);
//最終更新時刻更新してロックを開放
if ($this->_workDir) {
touch($this->_workDir . '/timestamp');
flock($fp, LOCK_UN);
fclose($fp);
}
//ファイル取得失敗の場合
if ($resp === false || $this->status >= 400) {
throw new Exception('リクエスト失敗 (' . $this->responseCode . ')');
}
$this->response = $resp;
if ($xmlFlg) {
//XMLをパース
//@see http://php.net/manual/ja/libxml.constants.php
$xml = simplexml_load_string(
$resp,
'SimpleXMLElement',
LIBXML_COMPACT + LIBXML_NOERROR
);
if ($xml === false) {
$this->status = self::SATUS_IDVALID_XML;
throw new Exception('XML取得失敗');
}
return $xml;
} else {
return $resp;
}
}
protected function _arr2qs($to) {
$from = mb_internal_encoding();
$arr = $this->_params;
if ($from !== $to) {
mb_convert_variables($to, $from, $arr);
}
return http_build_query($arr);
}
private function _getLockFp() {
if (!$this->_workDir) { //インターバル指定無し
return true;
}
$fp = fopen($this->_workDir . '/lock', 'w');
if (!$fp || !flock($fp, LOCK_EX)) {
return false;
}
//アクセス間隔を遵守
$path = $this->_workDir . '/timestamp';
if (file_exists($path)) {
$sleep = filemtime($path) + $this->_interval - time();
if ($sleep > 0) {
sleep($sleep);
}
}
return $fp;
}
private function _getFile($method, $url, $qs) {
//PHP 5.2.10+で有効。それ以前はHTTPエラー時にはPHPのWarningが発生
$http = array('ignore_errors' => true);
switch ($method) {
case 'get':
if ($qs) {
$url .= '?' . $qs;
}
break;
case 'post':
$http['method'] = 'POST';
$http['header'] = 'Content-type: application/x-www-form-urlencoded';
$http['content'] = $qs;
break;
default:
throw new Exception($method . 'には未対応です');
}
$this->url = $url;
$resp = file_get_contents(
$url,
false,
stream_context_create(array('http' => $http))
);
$this->_setResponseCode($http_response_header);
return $resp;
}
private function _setResponseCode($responseHeader) {
if (!$responseHeader) {
return;
}
preg_match('@^HTTP/1\\.. ([0-9]{3}) @i', $responseHeader[0], $matches);
$this->status = $matches[1];
$this->responseCode = $responseHeader[0];
}
public function getDetail($paramsFlg = false) {
$msg = '[status] ' . $this->status . "\n";
$msg .= '[url] ' . $this->url . "\n";
if ($paramsFlg) {
$msg .= '[params] ' . http_build_query($this->_params) . "\n";
$msg .= '[params (decoded)] ' . urldecode(http_build_query($this->_params)) . "\n";
}
if ($this->responseCode) {
$msg .= '[response code] ' . $this->responseCode . "\n";
}
$msg .= '[response body] ' . $this->response;
return $msg;
}
}
Show details
Hide details
Change log
r249
by andozz on Sep 28, 2011
Diff
[No log message]
Go to:
/trunk/Cache.php
/trunk/Controller.php
/trunk/Data.php
/trunk/Db.php
/trunk/Feeder.php
/trunk/Feeder2.php
/trunk/Filter.php
/trunk/Form.php
/trunk/Html.php
/trunk/Log.php
/trunk/Pager.php
/trunk/Service/Base.php
/trunk/globalFunctions.php
Project members,
sign in
to write a code review
Older revisions
r238
by andozz on Apr 15, 2011
Diff
[No log message]
r234
by andozz on Mar 31, 2011
Diff
[No log message]
r232
by andozz on Mar 14, 2011
Diff
@によるWarning抑制をやめて別の方法でWarningを回避
All revisions of this file
File info
Size: 4704 bytes, 188 lines
View raw file
Powered by
Google Project Hosting