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
2585769e
Commit
2585769e
authored
Jun 28, 2017
by
pader
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化了 Client 的请求,优化了调试模式的输出信息
parent
d4a960fa
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
26 deletions
+65
-26
Client.php
src/base/Client.php
+65
-26
No files found.
src/base/Client.php
View file @
2585769e
...
@@ -143,47 +143,86 @@ class Client
...
@@ -143,47 +143,86 @@ class Client
}
}
protected
function
request
(
$url
,
$post
=
''
,
$headers
=
[])
{
protected
function
request
(
$url
,
$post
=
''
,
$headers
=
[])
{
$ci
=
curl_init
();
$ci
=
curl_init
(
$url
);
/* Curl settings */
curl_setopt
(
$ci
,
CURLOPT_USERAGENT
,
'PHP/'
.
PHP_VERSION
.
' Client/'
.
$this
->
clientId
);
curl_setopt
(
$ci
,
CURLOPT_CONNECTTIMEOUT
,
5
);
curl_setopt
(
$ci
,
CURLOPT_TIMEOUT
,
8
);
curl_setopt
(
$ci
,
CURLOPT_RETURNTRANSFER
,
TRUE
);
curl_setopt
(
$ci
,
CURLOPT_SSL_VERIFYPEER
,
false
);
curl_setopt
(
$ci
,
CURLOPT_HEADER
,
FALSE
);
//添加客户端信息以辨服务端识别和调试
$userAgent
=
'PHP/'
.
PHP_VERSION
.
' ('
.
PHP_OS
.
') Client/'
.
$this
->
clientId
;
if
(
isset
(
$_SERVER
[
'HTTP_HOST'
]))
{
$userAgent
.=
' ('
.
$_SERVER
[
'HTTP_HOST'
]
.
')'
;
}
$curl
=
curl_version
();
$userAgent
.=
" cURL/
{
$curl
[
'version'
]
}
(
{
$curl
[
'ssl_version'
]
}
)"
;
//基础设置
curl_setopt_array
(
$ci
,
[
CURLOPT_CONNECTTIMEOUT
=>
5
,
CURLOPT_TIMEOUT
=>
8
,
CURLOPT_RETURNTRANSFER
=>
true
,
CURLOPT_SSL_VERIFYPEER
=>
false
,
CURLOPT_HEADER
=>
false
,
CURLOPT_USERAGENT
=>
$userAgent
]);
//POST 请求
if
(
$post
)
{
if
(
$post
)
{
curl_setopt
(
$ci
,
CURLOPT_POST
,
TRUE
);
$body
=
is_array
(
$post
)
?
http_build_query
(
$post
)
:
$post
;
$post
=
is_array
(
$post
)
?
http_build_query
(
$post
)
:
$post
;
curl_setopt_array
(
$ci
,
[
$post
&&
curl_setopt
(
$ci
,
CURLOPT_POSTFIELDS
,
$post
);
CURLOPT_POST
=>
true
,
CURLOPT_POSTFIELDS
=>
$body
]);
}
}
$header
[]
=
'Expect:'
;
//优化
$headers
[]
=
'Expect:'
;
curl_setopt
(
$ci
,
CURLOPT_URL
,
$url
);
curl_setopt_array
(
$ci
,
[
curl_setopt
(
$ci
,
CURLOPT_HTTPHEADER
,
$headers
);
CURLOPT_HTTPHEADER
=>
$headers
,
//curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
CURLOPT_HTTP_VERSION
=>
CURL_HTTP_VERSION_1_0
,
//curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
CURLOPT_IPRESOLVE
=>
CURL_IPRESOLVE_V4
]);
$response
=
curl_exec
(
$ci
);
//调试
if
(
$this
->
debug
)
{
curl_setopt
(
$ci
,
CURLINFO_HEADER_OUT
,
true
);
$responseHeaders
=
''
;
curl_setopt
(
$ci
,
CURLOPT_HEADERFUNCTION
,
function
(
$ci
,
$header
)
use
(
&
$responseHeaders
)
{
$responseHeaders
.=
$header
;
return
strlen
(
$header
);
});
}
$response
=
curl_exec
(
$ci
);
$httpCode
=
curl_getinfo
(
$ci
,
CURLINFO_HTTP_CODE
);
$httpCode
=
curl_getinfo
(
$ci
,
CURLINFO_HTTP_CODE
);
$this
->
debug
&&
$httpInfo
=
curl_getinfo
(
$ci
);
//获取调试信息
if
(
$this
->
debug
)
{
$httpInfo
=
curl_getinfo
(
$ci
);
}
curl_close
(
$ci
);
curl_close
(
$ci
);
if
(
$this
->
debug
)
{
if
(
$this
->
debug
)
{
echo
'<pre>'
;
echo
'<pre>'
;
echo
'=====info====='
.
"
\r\n
"
;
echo
"=====INFO=====
\r\n
"
;
print_r
(
$httpInfo
);
echo
"Remote Address:
{
$httpInfo
[
'primary_ip'
]
}
:
{
$httpInfo
[
'primary_port'
]
}
\r\n
"
,
"Local Address:
{
$httpInfo
[
'local_ip'
]
}
:
{
$httpInfo
[
'local_port'
]
}
\r\n
"
,
"Namelookup Time:
{
$httpInfo
[
'namelookup_time'
]
}
\r\n
"
,
"Connect Time:
{
$httpInfo
[
'connect_time'
]
}
\r\n
"
,
"Pre Transfer Time:
{
$httpInfo
[
'pretransfer_time'
]
}
\r\n
"
,
"Start Transfer Time:
{
$httpInfo
[
'starttransfer_time'
]
}
\r\n
"
,
"File Time:
{
$httpInfo
[
'filetime'
]
}
\r\n
"
,
"Total Time:
{
$httpInfo
[
'total_time'
]
}
\r\n
"
;
echo
"
\r\n
=====REQUEST=====
\r\n
"
,
$httpInfo
[
'request_header'
];
echo
"
\r\n
=====post data======
\r\n
"
;
if
(
isset
(
$body
))
{
var_dump
(
$post
);
echo
$body
.
"
\r\n
"
;
}
echo
"
\r\n
=====response=====
\r\n
"
;
echo
"
\r\n
=====RESPONSE=====
\r\n
"
,
$responseHeaders
,
$response
;
print_r
(
$response
);
echo
'</pre>'
;
echo
'</pre>'
;
exit
;
}
}
return
[
$httpCode
,
$response
];
return
[
$httpCode
,
$response
];
...
...
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