Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
wxh_internal_api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
杨培培
wxh_internal_api
Commits
08b13b33
Commit
08b13b33
authored
Jul 07, 2017
by
夏彬
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
b79baded
dad94e48
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
95 additions
and
47 deletions
+95
-47
ucenter_basic.php
example/ucenter_basic.php
+3
-3
ucenter_register.php
example/ucenter_register.php
+1
-1
ApiException.php
src/base/ApiException.php
+21
-0
Client.php
src/base/Client.php
+38
-25
ClientException.php
src/base/ClientException.php
+9
-2
Server.php
src/base/Server.php
+23
-1
ServerException.php
src/base/ServerException.php
+0
-15
No files found.
example/ucenter_basic.php
View file @
08b13b33
...
@@ -12,7 +12,7 @@ require __DIR__.'/_init.php';
...
@@ -12,7 +12,7 @@ require __DIR__.'/_init.php';
Member
::
setDebug
();
Member
::
setDebug
();
//$info = Member::getMemberInfo(1
2);
$info
=
Member
::
getMemberInfo
(
2
);
$info
=
Member
::
getSyncByMobile
(
'18019580609
'
);
//$info = Member::getSyncByMobile('13000000002
');
//$info = Member::getSyncByUid(
40
);
//$info = Member::getSyncByUid(
514
);
print_r
(
$info
);
print_r
(
$info
);
example/ucenter_register.php
View file @
08b13b33
...
@@ -12,5 +12,5 @@ require __DIR__.'/_init.php';
...
@@ -12,5 +12,5 @@ require __DIR__.'/_init.php';
Member
::
setDebug
();
Member
::
setDebug
();
$name
=
uniqid
(
'dev_'
);
$name
=
uniqid
(
'dev_'
);
$info
=
Member
::
register
(
$name
,
'123456'
,
$name
,
[
'mobile'
=>
'1600000000
0
'
]);
$info
=
Member
::
register
(
$name
,
'123456'
,
$name
,
[
'mobile'
=>
'1600000000
1
'
]);
print_r
(
$info
);
print_r
(
$info
);
src/base/ApiException.php
0 → 100644
View file @
08b13b33
<?php
/**
* Created by PhpStorm.
* User: pader
* Date: 2017/7/6
* Time: 11:46
*/
namespace
internalApi\base
;
/**
* 接口异常
*
* 本异常为目标接口业务异常
* 所的目标接口抛出的异常到客户端后均抛出为本异常
*
* @package internalApi\base
*/
class
ApiException
extends
\Exception
{
}
\ No newline at end of file
src/base/Client.php
View file @
08b13b33
...
@@ -97,7 +97,7 @@ class Client
...
@@ -97,7 +97,7 @@ class Client
* @param string $api
* @param string $api
* @param array $params
* @param array $params
* @return mixed
* @return mixed
* @throws ClientException
API 请求失败时,会抛出此异常
* @throws ClientException
|ApiException 请求过程出错会抛出ClientException,若源API抛异常,则会抛出ApiException
*/
*/
protected
static
function
call
(
$api
,
$params
=
[])
protected
static
function
call
(
$api
,
$params
=
[])
{
{
...
@@ -109,22 +109,30 @@ class Client
...
@@ -109,22 +109,30 @@ class Client
list
(
$httpCode
,
$response
)
=
$instance
->
request
(
$url
,
$params
);
list
(
$httpCode
,
$response
)
=
$instance
->
request
(
$url
,
$params
);
if
(
$httpCode
==
200
&&
$response
!==
false
)
{
$data
=
@
json_decode
(
$response
,
true
);
if
(
json_last_error
()
!==
JSON_ERROR_NONE
)
{
throw
new
ClientException
(
"接口
$api
返回数据异常: "
.
$response
);
}
return
$data
;
}
if
(
$httpCode
==
403
&&
substr
(
$response
,
0
,
6
)
===
'error:'
)
{
if
(
$httpCode
==
403
&&
substr
(
$response
,
0
,
6
)
===
'error:'
)
{
$message
=
substr
(
$response
,
6
);
$message
=
substr
(
$response
,
6
);
throw
new
ClientException
(
"接口
$api
调用失败: "
.
$message
);
throw
new
ClientException
(
"接口
$api
调用失败: "
.
$message
);
}
elseif
(
$httpCode
===
0
)
{
throw
new
ClientException
(
'接口请求异常: '
.
$response
,
$httpCode
);
}
elseif
(
$httpCode
!=
200
||
$response
===
false
)
{
throw
new
ClientException
(
'接口响应异常: '
.
$response
,
$httpCode
);
}
}
$data
=
@
json_decode
(
$response
,
true
);
if
(
$httpCode
==
500
&&
substr
(
$response
,
0
,
10
)
===
'exception:'
)
{
$e
=
substr
(
$response
,
10
);
if
(
json_last_error
()
!==
JSON_ERROR_NONE
)
{
$e
=
json_decode
(
$e
,
true
);
throw
new
ClientException
(
'接口返回数据异常: '
.
$response
);
throw
new
ApiException
(
"
{
$e
[
'class'
]
}
:
{
$e
[
'message'
]
}
"
,
$e
[
'code'
]
);
}
}
return
$data
;
if
(
$httpCode
==
0
)
{
throw
new
ClientException
(
"接口
$api
请求异常: "
.
$response
);
}
else
{
throw
new
ClientException
(
"接口
$api
响应异常: "
.
$response
,
$httpCode
);
}
}
}
/**
/**
...
@@ -222,29 +230,34 @@ class Client
...
@@ -222,29 +230,34 @@ class Client
curl_close
(
$ci
);
curl_close
(
$ci
);
if
(
$this
->
debug
)
{
if
(
$this
->
debug
)
{
echo
"<pre>
\r\n
=====INFO=====
\r\n
"
;
$info
=
"=====INFO=====
\r\n
"
echo
"Request URL:
$url
\r\n
"
,
.
"Request URL:
$url
\r\n
"
"Remote Address:
{
$httpInfo
[
'primary_ip'
]
}
:
{
$httpInfo
[
'primary_port'
]
}
\r\n
"
,
.
"Remote Address:
{
$httpInfo
[
'primary_ip'
]
}
:
{
$httpInfo
[
'primary_port'
]
}
\r\n
"
"Local Address:
{
$httpInfo
[
'local_ip'
]
}
:
{
$httpInfo
[
'local_port'
]
}
\r\n
"
,
.
"Local Address:
{
$httpInfo
[
'local_ip'
]
}
:
{
$httpInfo
[
'local_port'
]
}
\r\n
"
"Namelookup Time:
{
$httpInfo
[
'namelookup_time'
]
}
\r\n
"
,
.
"Namelookup Time:
{
$httpInfo
[
'namelookup_time'
]
}
\r\n
"
"Connect Time:
{
$httpInfo
[
'connect_time'
]
}
\r\n
"
,
.
"Connect Time:
{
$httpInfo
[
'connect_time'
]
}
\r\n
"
"Pre Transfer Time:
{
$httpInfo
[
'pretransfer_time'
]
}
\r\n
"
,
.
"Pre Transfer Time:
{
$httpInfo
[
'pretransfer_time'
]
}
\r\n
"
"Start Transfer Time:
{
$httpInfo
[
'starttransfer_time'
]
}
\r\n
"
,
.
"Start Transfer Time:
{
$httpInfo
[
'starttransfer_time'
]
}
\r\n
"
"File Time:
{
$httpInfo
[
'filetime'
]
}
\r\n
"
,
.
"File Time:
{
$httpInfo
[
'filetime'
]
}
\r\n
"
"Total Time:
{
$httpInfo
[
'total_time'
]
}
\r\n
"
;
.
"Total Time:
{
$httpInfo
[
'total_time'
]
}
\r\n
"
;
if
(
isset
(
$errno
))
{
if
(
isset
(
$errno
))
{
echo
"
\r\n
=====ERROR=====
\r\n
ErrorNo:
$errno
\r\n
ErrorMsg:
$errmsg
\r\n
"
;
$info
.=
"
\r\n
=====ERROR=====
\r\n
ErrorNo:
$errno
\r\n
ErrorMsg:
$errmsg
\r\n
"
;
}
}
echo
"
\r\n
=====REQUEST=====
\r\n
"
,
$httpInfo
[
'request_header'
]
;
$info
.=
"
\r\n
=====REQUEST=====
\r\n
{
$httpInfo
[
'request_header'
]
}
"
;
if
(
isset
(
$body
))
{
if
(
isset
(
$body
))
{
echo
$body
.
"
\r\n
"
;
$info
.=
$body
.
"
\r\n
"
;
}
}
echo
"
\r\n
=====RESPONSE=====
\r\n
"
,
$responseHeaders
,
$response
;
$info
.=
"
\r\n
=====RESPONSE=====
\r\n
{
$responseHeaders
}{
$response
}
"
;
echo
"
\r\n
</pre>"
;
if
(
PHP_SAPI
==
'cli'
||
(
isset
(
$_SERVER
[
'HTTP_X_REQUESTED_WITH'
])
&&
$_SERVER
[
'HTTP_X_REQUESTED_WITH'
]
==
'XMLHttpRequest'
)
||
empty
(
$_SERVER
[
'HTTP_USER_AGENT'
]))
{
echo
$info
.
"
\r\n
"
;
}
else
{
echo
'<pre>'
.
htmlspecialchars
(
$info
)
.
'</pre>'
;
}
}
}
return
[
$httpCode
,
$response
];
return
[
$httpCode
,
$response
];
...
...
src/base/ClientException.php
View file @
08b13b33
...
@@ -8,7 +8,14 @@
...
@@ -8,7 +8,14 @@
namespace
internalApi\base
;
namespace
internalApi\base
;
/**
class
ClientException
extends
\ErrorException
* 客户端异常
*
* 本异常为非业务异常
* 如调用请求发送失败,数据异常,网络异常,接口参数不一致等程序和服务器环境导致的无法正常发送和解析接口的请求问题
*
* @package internalApi\base
*/
class
ClientException
extends
\Exception
{
{
}
}
\ No newline at end of file
src/base/Server.php
View file @
08b13b33
...
@@ -94,7 +94,11 @@ class Server
...
@@ -94,7 +94,11 @@ class Server
}
}
}
}
$data
=
call_user_func_array
(
$callable
,
$params
);
try
{
$data
=
call_user_func_array
(
$callable
,
$params
);
}
catch
(
\Exception
$e
)
{
$this
->
responseException
(
$e
);
}
if
(
$return
)
{
if
(
$return
)
{
return
$data
;
return
$data
;
...
@@ -104,6 +108,24 @@ class Server
...
@@ -104,6 +108,24 @@ class Server
}
}
}
}
/**
* @param \Exception $e
*/
public
function
responseException
(
$e
)
{
header
(
'HTTP/1.0 500 Internal Server Error'
);
$exception
=
[
'class'
=>
get_class
(
$e
),
'code'
=>
$e
->
getCode
(),
'message'
=>
$e
->
getMessage
(),
'line'
=>
$e
->
getFile
(),
'file'
=>
$e
->
getFile
(),
'traceString'
=>
$e
->
getTraceAsString
()
];
echo
'exception:'
.
json_encode
(
$exception
,
JSON_UNESCAPED_UNICODE
);
exit
;
}
public
function
responseError
(
$message
)
public
function
responseError
(
$message
)
{
{
header
(
'HTTP/1.0 403 Forbidden'
);
header
(
'HTTP/1.0 403 Forbidden'
);
...
...
src/base/ServerException.php
deleted
100644 → 0
View file @
b79baded
<?php
/**
* Created by PhpStorm.
* User: pader
* Date: 2017/6/27
* Time: 16:44
*/
namespace
internalApi\base
;
class
ServerException
extends
\Exception
{
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment