Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
token
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wallet
token
Commits
bd210511
Commit
bd210511
authored
Sep 09, 2019
by
shajiaiming
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
调整请求时间
parent
176cbc54
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
697 additions
and
649 deletions
+697
-649
Curl.php
common/helpers/Curl.php
+652
-607
Chain33Service.php
common/service/chain33/Chain33Service.php
+45
-41
CrossChainController.php
console/controllers/CrossChainController.php
+0
-1
No files found.
common/helpers/Curl.php
View file @
bd210511
...
...
@@ -8,612 +8,656 @@ use yii\helpers\Json;
class
Curl
{
/**
* @var string
* * Holds response data right after sending a request.
*/
public
$httpType
=
'http'
;
/**
* @var string|boolean
* Holds response data right after sending a request.
*/
public
$response
=
null
;
/**
* @var null|integer
* Error code holder: https://curl.haxx.se/libcurl/c/libcurl-errors.html
*/
public
$errorCode
=
null
;
/**
* @var null|string
* Error text holder: http://php.net/manual/en/function.curl-strerror.php
*/
public
$errorText
=
null
;
/**
* @var integer HTTP-Status Code
* This value will hold HTTP-Status Code. False if request was not successful.
*/
public
$responseCode
=
null
;
/**
* @var string|null HTTP Response Charset
* (taken from Content-type header)
*/
public
$responseCharset
=
null
;
/**
* @var int HTTP Response Length
* (taken from Content-length header, or strlen() of downloaded content)
*/
public
$responseLength
=
-
1
;
/**
* @var string|null HTTP Response Content Type
* (taken from Content-type header)
*/
public
$responseType
=
null
;
/**
* @var array|null HTTP Response headers
* Lists response header in an array if CURLOPT_HEADER is set to true.
*/
public
$responseHeaders
=
null
;
/**
* @var array HTTP-Status Code
* Custom options holder
*/
protected
$_options
=
[];
/**
* @var array
* Hold array of get params to send with the request
*/
protected
$_getParams
=
[];
/**
* @var array
* Hold array of post params to send with the request
*/
protected
$_postParams
=
[];
/**
* @var resource|null
* Holds cURL-Handler
*/
public
$curl
=
null
;
/**
* @var string
* hold base URL
*/
protected
$_baseUrl
=
''
;
/**
* @var array default curl options
* Default curl options
*/
protected
$_defaultOptions
=
[
CURLOPT_USERAGENT
=>
'Yii2-Curl-Agent'
,
CURLOPT_TIMEOUT
=>
10
,
CURLOPT_CONNECTTIMEOUT
=>
10
,
CURLOPT_RETURNTRANSFER
=>
true
,
CURLOPT_HEADER
=>
true
,
CURLOPT_SSL_VERIFYPEER
=>
false
,
CURLOPT_SSL_VERIFYHOST
=>
2
,
];
// ############################################### class methods // ##############################################
/**
* Start performing GET-HTTP-Request
*
* @param string $url
* @param boolean $raw if response body contains JSON and should be decoded
*
* @return mixed response
*/
public
function
get
(
$url
,
$raw
=
true
)
{
$this
->
_baseUrl
=
$url
;
return
$this
->
_httpRequest
(
'GET'
,
$raw
);
}
/**
* Start performing HEAD-HTTP-Request
*
* @param string $url
*
* @return mixed response
*/
public
function
head
(
$url
)
{
$this
->
_baseUrl
=
$url
;
return
$this
->
_httpRequest
(
'HEAD'
);
}
/**
* Start performing POST-HTTP-Request
*
* @param string $url
* @param boolean $raw if response body contains JSON and should be decoded
*
* @return mixed response
*/
public
function
post
(
$url
,
$raw
=
true
)
{
$this
->
_baseUrl
=
$url
;
return
$this
->
_httpRequest
(
'POST'
,
$raw
);
}
/**
* Start performing PUT-HTTP-Request
*
* @param string $url
* @param boolean $raw if response body contains JSON and should be decoded
*
* @return mixed response
*/
public
function
put
(
$url
,
$raw
=
true
)
{
$this
->
_baseUrl
=
$url
;
return
$this
->
_httpRequest
(
'PUT'
,
$raw
);
}
/**
* Start performing PATCH-HTTP-Request
*
* @param string $url
* @param boolean $raw if response body contains JSON and should be decoded
*
* @return mixed response
*/
public
function
patch
(
$url
,
$raw
=
true
)
{
$this
->
_baseUrl
=
$url
;
$this
->
setHeaders
([
'X-HTTP-Method-Override'
=>
'PATCH'
]);
return
$this
->
_httpRequest
(
'POST'
,
$raw
);
}
/**
* Start performing DELETE-HTTP-Request
*
* @param string $url
* @param boolean $raw if response body contains JSON and should be decoded
*
* @return mixed response
*/
public
function
delete
(
$url
,
$raw
=
true
)
{
$this
->
_baseUrl
=
$url
;
return
$this
->
_httpRequest
(
'DELETE'
,
$raw
);
}
/**
* Set curl option
*
* @param string $key
* @param mixed $value
*
* @return $this
*/
public
function
setOption
(
$key
,
$value
)
{
//set value
if
(
array_key_exists
(
$key
,
$this
->
_defaultOptions
)
&&
$key
!==
CURLOPT_WRITEFUNCTION
)
{
$this
->
_defaultOptions
[
$key
]
=
$value
;
}
else
{
$this
->
_options
[
$key
]
=
$value
;
}
//return self
return
$this
;
}
/**
* Set get params
*
* @param array $params
* @return $this
*/
public
function
setGetParams
(
$params
)
{
if
(
is_array
(
$params
))
{
foreach
(
$params
as
$key
=>
$value
)
{
$this
->
_getParams
[
$key
]
=
$value
;
}
}
//return self
return
$this
;
}
/**
* Set get params
*
* @param array $params
* @return $this
*/
public
function
setPostParams
(
$params
)
{
if
(
is_array
(
$params
))
{
$this
->
setOption
(
CURLOPT_POSTFIELDS
,
http_build_query
(
$params
)
);
}
//return self
return
$this
;
}
/**
* Set raw post data allows you to post any data format.
*
* @param mixed $data
* @return $this
*/
public
function
setRawPostData
(
$data
)
{
$this
->
setOption
(
CURLOPT_POSTFIELDS
,
$data
);
//return self
return
$this
;
}
/**
* Set get params
*
* @param string $data
* @return $this
*/
public
function
setRequestBody
(
$data
)
{
if
(
is_string
(
$data
))
{
$this
->
setOption
(
CURLOPT_POSTFIELDS
,
$data
);
}
//return self
return
$this
;
}
/**
* Get URL - return URL parsed with given params
*
* @return string The full URL with parsed get params
*/
public
function
getUrl
()
{
if
(
Count
(
$this
->
_getParams
)
>
0
)
{
return
$this
->
_baseUrl
.
'?'
.
http_build_query
(
$this
->
_getParams
);
}
else
{
return
$this
->
_baseUrl
;
}
}
/**
* Set curl options
*
* @param array $options
*
* @return $this
*/
public
function
setOptions
(
$options
)
{
$this
->
_options
=
$options
+
$this
->
_options
;
return
$this
;
}
/**
* Set multiple headers for request.
*
* @param array $headers
*
* @return $this
*/
public
function
setHeaders
(
$headers
)
{
if
(
is_array
(
$headers
))
{
//init
$parsedHeader
=
[];
//collect currently set headers
foreach
(
$this
->
getRequestHeaders
()
as
$header
=>
$value
)
{
array_push
(
$parsedHeader
,
$header
.
':'
.
$value
);
}
//parse header into right format key:value
foreach
(
$headers
as
$header
=>
$value
)
{
array_push
(
$parsedHeader
,
$header
.
':'
.
$value
);
}
//set headers
$this
->
setOption
(
CURLOPT_HTTPHEADER
,
$parsedHeader
);
}
return
$this
;
}
/**
* Set a single header for request.
*
* @param string $header
* @param string $value
*
* @return $this
*/
public
function
setHeader
(
$header
,
$value
)
{
//init
$parsedHeader
=
[];
//collect currently set headers
foreach
(
$this
->
getRequestHeaders
()
as
$headerToSet
=>
$valueToSet
)
{
array_push
(
$parsedHeader
,
$headerToSet
.
':'
.
$valueToSet
);
}
//add override new header
if
(
strlen
(
$header
)
>
0
)
{
array_push
(
$parsedHeader
,
$header
.
':'
.
$value
);
}
//set headers
$this
->
setOption
(
CURLOPT_HTTPHEADER
,
$parsedHeader
);
return
$this
;
}
/**
* Unset a single header.
*
* @param string $header
*
* @return $this
*/
public
function
unsetHeader
(
$header
)
{
//init
$parsedHeader
=
[];
//collect currently set headers and filter "unset" header param.
foreach
(
$this
->
getRequestHeaders
()
as
$headerToSet
=>
$valueToSet
)
{
if
(
$header
!==
$headerToSet
)
{
array_push
(
$parsedHeader
,
$headerToSet
.
':'
.
$valueToSet
);
}
}
//set headers
$this
->
setOption
(
CURLOPT_HTTPHEADER
,
$parsedHeader
);
return
$this
;
}
/**
* Get all request headers as key:value array
*
* @return array
*/
public
function
getRequestHeaders
()
{
//Init
$requestHeaders
=
$this
->
getOption
(
CURLOPT_HTTPHEADER
);
$parsedRequestHeaders
=
[];
if
(
is_array
(
$requestHeaders
))
{
foreach
(
$requestHeaders
as
$headerValue
)
{
list
(
$key
,
$value
)
=
explode
(
':'
,
$headerValue
,
2
);
$parsedRequestHeaders
[
$key
]
=
$value
;
}
}
return
$parsedRequestHeaders
;
}
/**
* Get specific request header as key:value array
*
* @param string $headerKey
*
* @return string|null
*/
public
function
getRequestHeader
(
$headerKey
)
{
//Init
$parsedRequestHeaders
=
$this
->
getRequestHeaders
();
return
isset
(
$parsedRequestHeaders
[
$headerKey
])
?
$parsedRequestHeaders
[
$headerKey
]
:
null
;
}
/**
* Unset a single curl option
*
* @param string $key
*
* @return $this
*/
public
function
unsetOption
(
$key
)
{
//reset a single option if its set already
if
(
isset
(
$this
->
_options
[
$key
]))
{
unset
(
$this
->
_options
[
$key
]);
}
return
$this
;
}
/**
* Unset all curl option, excluding default options.
*
* @return $this
*/
public
function
unsetOptions
()
{
//reset all options
if
(
isset
(
$this
->
_options
))
{
$this
->
_options
=
[];
}
return
$this
;
}
/**
* Total reset of options, responses, etc.
*
* @return $this
*/
public
function
reset
()
{
if
(
$this
->
curl
!==
null
)
{
curl_close
(
$this
->
curl
);
//stop curl
}
//reset all options
if
(
isset
(
$this
->
_options
))
{
$this
->
_options
=
[];
}
//reset response & status params
$this
->
curl
=
null
;
$this
->
errorCode
=
null
;
$this
->
response
=
null
;
$this
->
responseCode
=
null
;
$this
->
responseCharset
=
null
;
$this
->
responseLength
=
-
1
;
$this
->
responseType
=
null
;
$this
->
errorText
=
null
;
$this
->
_postParams
=
[];
$this
->
_getParams
=
[];
return
$this
;
}
/**
* Return a single option
*
* @param string|integer $key
* @return mixed|boolean
*/
public
function
getOption
(
$key
)
{
//get merged options depends on default and user options
$mergesOptions
=
$this
->
getOptions
();
//return value or false if key is not set.
return
isset
(
$mergesOptions
[
$key
])
?
$mergesOptions
[
$key
]
:
false
;
}
/**
* Return merged curl options and keep keys!
*
* @return array
*/
public
function
getOptions
()
{
return
$this
->
_options
+
$this
->
_defaultOptions
;
}
/**
* Get curl info according to http://php.net/manual/de/function.curl-getinfo.php
*
* @param null $opt
* @return array|mixed
*/
public
function
getInfo
(
$opt
=
null
)
{
if
(
$this
->
curl
!==
null
&&
$opt
===
null
)
{
return
curl_getinfo
(
$this
->
curl
);
}
elseif
(
$this
->
curl
!==
null
&&
$opt
!==
null
)
{
return
curl_getinfo
(
$this
->
curl
,
$opt
);
}
else
{
return
[];
}
}
/**
* Performs HTTP request
*
* @param string $method
* @param boolean $raw if response body contains JSON and should be decoded -> helper.
*
* @throws Exception if request failed
*
* @return mixed
*/
protected
function
_httpRequest
(
$method
,
$raw
=
false
)
{
//set request type and writer function
$this
->
setOption
(
CURLOPT_CUSTOMREQUEST
,
strtoupper
(
$method
));
//check if method is head and set no body
if
(
$method
===
'HEAD'
)
{
$this
->
setOption
(
CURLOPT_NOBODY
,
true
);
$this
->
unsetOption
(
CURLOPT_WRITEFUNCTION
);
}
//setup error reporting and profiling
if
(
YII_DEBUG
)
{
Yii
::
trace
(
'Start sending cURL-Request: '
.
$this
->
getUrl
()
.
'\n'
,
__METHOD__
);
Yii
::
beginProfile
(
$method
.
' '
.
$this
->
_baseUrl
.
'#'
.
md5
(
serialize
(
$this
->
getOption
(
CURLOPT_POSTFIELDS
))),
__METHOD__
);
}
/**
* proceed curl
*/
$curlOptions
=
$this
->
getOptions
();
$this
->
curl
=
curl_init
(
$this
->
getUrl
());
curl_setopt_array
(
$this
->
curl
,
$curlOptions
);
/**
* @var string
* * Holds response data right after sending a request.
*/
public
$httpType
=
'http'
;
/**
* @var string|boolean
* Holds response data right after sending a request.
*/
public
$response
=
null
;
/**
* @var null|integer
* Error code holder: https://curl.haxx.se/libcurl/c/libcurl-errors.html
*/
public
$errorCode
=
null
;
/**
* @var null|string
* Error text holder: http://php.net/manual/en/function.curl-strerror.php
*/
public
$errorText
=
null
;
/**
* @var integer HTTP-Status Code
* This value will hold HTTP-Status Code. False if request was not successful.
*/
public
$responseCode
=
null
;
/**
* @var string|null HTTP Response Charset
* (taken from Content-type header)
*/
public
$responseCharset
=
null
;
/**
* @var int HTTP Response Length
* (taken from Content-length header, or strlen() of downloaded content)
*/
public
$responseLength
=
-
1
;
/**
* @var string|null HTTP Response Content Type
* (taken from Content-type header)
*/
public
$responseType
=
null
;
/**
* @var array|null HTTP Response headers
* Lists response header in an array if CURLOPT_HEADER is set to true.
*/
public
$responseHeaders
=
null
;
/**
* @var array HTTP-Status Code
* Custom options holder
*/
protected
$_options
=
[];
/**
* @var array
* Hold array of get params to send with the request
*/
protected
$_getParams
=
[];
/**
* @var array
* Hold array of post params to send with the request
*/
protected
$_postParams
=
[];
/**
* @var resource|null
* Holds cURL-Handler
*/
public
$curl
=
null
;
/**
* @var string
* hold base URL
*/
protected
$_baseUrl
=
''
;
/**
* @var array default curl options
* Default curl options
*/
protected
$_defaultOptions
=
[
CURLOPT_USERAGENT
=>
'Yii2-Curl-Agent'
,
CURLOPT_TIMEOUT
=>
10
,
CURLOPT_CONNECTTIMEOUT
=>
10
,
CURLOPT_RETURNTRANSFER
=>
true
,
CURLOPT_HEADER
=>
true
,
CURLOPT_SSL_VERIFYPEER
=>
false
,
CURLOPT_SSL_VERIFYHOST
=>
2
,
];
// ############################################### class methods // ##############################################
/**
* Start performing GET-HTTP-Request
*
* @param string $url
* @param boolean $raw if response body contains JSON and should be decoded
*
* @return mixed response
*/
protected
$connect_timeout_ms
;
protected
$timeout_ms
;
public
function
__construct
(
$config
=
[])
{
if
(
!
empty
(
$config
))
{
$this
->
connect_timeout_ms
=
isset
(
$config
[
'connect_timeout'
])
?
$config
[
'connect_timeout'
]
:
10
;
$this
->
timeout_ms
=
isset
(
$config
[
'timeout_ms'
])
?
$config
[
'timeout_ms'
]
:
100
;
}
}
public
function
get
(
$url
,
$raw
=
true
)
{
$this
->
_baseUrl
=
$url
;
return
$this
->
_httpRequest
(
'GET'
,
$raw
);
}
/**
* Start performing HEAD-HTTP-Request
*
* @param string $url
*
* @return mixed response
*/
public
function
head
(
$url
)
{
$this
->
_baseUrl
=
$url
;
return
$this
->
_httpRequest
(
'HEAD'
);
}
/**
* Start performing POST-HTTP-Request
*
* @param string $url
* @param boolean $raw if response body contains JSON and should be decoded
*
* @return mixed response
*/
public
function
post
(
$url
,
$raw
=
true
)
{
$this
->
_baseUrl
=
$url
;
return
$this
->
_httpRequest
(
'POST'
,
$raw
);
}
/**
* Start performing PUT-HTTP-Request
*
* @param string $url
* @param boolean $raw if response body contains JSON and should be decoded
*
* @return mixed response
*/
public
function
put
(
$url
,
$raw
=
true
)
{
$this
->
_baseUrl
=
$url
;
return
$this
->
_httpRequest
(
'PUT'
,
$raw
);
}
/**
* Start performing PATCH-HTTP-Request
*
* @param string $url
* @param boolean $raw if response body contains JSON and should be decoded
*
* @return mixed response
*/
public
function
patch
(
$url
,
$raw
=
true
)
{
$this
->
_baseUrl
=
$url
;
$this
->
setHeaders
([
'X-HTTP-Method-Override'
=>
'PATCH'
]);
return
$this
->
_httpRequest
(
'POST'
,
$raw
);
}
/**
* Start performing DELETE-HTTP-Request
*
* @param string $url
* @param boolean $raw if response body contains JSON and should be decoded
*
* @return mixed response
*/
public
function
delete
(
$url
,
$raw
=
true
)
{
$this
->
_baseUrl
=
$url
;
return
$this
->
_httpRequest
(
'DELETE'
,
$raw
);
}
/**
* Set curl option
*
* @param string $key
* @param mixed $value
*
* @return $this
*/
public
function
setOption
(
$key
,
$value
)
{
//set value
if
(
array_key_exists
(
$key
,
$this
->
_defaultOptions
)
&&
$key
!==
CURLOPT_WRITEFUNCTION
)
{
$this
->
_defaultOptions
[
$key
]
=
$value
;
}
else
{
$this
->
_options
[
$key
]
=
$value
;
}
//return self
return
$this
;
}
/**
* Set get params
*
* @param array $params
* @return $this
*/
public
function
setGetParams
(
$params
)
{
if
(
is_array
(
$params
))
{
foreach
(
$params
as
$key
=>
$value
)
{
$this
->
_getParams
[
$key
]
=
$value
;
}
}
//return self
return
$this
;
}
/**
* Set get params
*
* @param array $params
* @return $this
*/
public
function
setPostParams
(
$params
)
{
if
(
is_array
(
$params
))
{
$this
->
setOption
(
CURLOPT_POSTFIELDS
,
http_build_query
(
$params
)
);
}
//return self
return
$this
;
}
/**
* Set raw post data allows you to post any data format.
*
* @param mixed $data
* @return $this
*/
public
function
setRawPostData
(
$data
)
{
$this
->
setOption
(
CURLOPT_POSTFIELDS
,
$data
);
//return self
return
$this
;
}
/**
* Set get params
*
* @param string $data
* @return $this
*/
public
function
setRequestBody
(
$data
)
{
if
(
is_string
(
$data
))
{
$this
->
setOption
(
CURLOPT_POSTFIELDS
,
$data
);
}
//return self
return
$this
;
}
/**
* Get URL - return URL parsed with given params
*
* @return string The full URL with parsed get params
*/
public
function
getUrl
()
{
if
(
Count
(
$this
->
_getParams
)
>
0
)
{
return
$this
->
_baseUrl
.
'?'
.
http_build_query
(
$this
->
_getParams
);
}
else
{
return
$this
->
_baseUrl
;
}
}
/**
* Set curl options
*
* @param array $options
*
* @return $this
*/
public
function
setOptions
(
$options
)
{
$this
->
_options
=
$options
+
$this
->
_options
;
return
$this
;
}
/**
* Set multiple headers for request.
*
* @param array $headers
*
* @return $this
*/
public
function
setHeaders
(
$headers
)
{
if
(
is_array
(
$headers
))
{
//init
$parsedHeader
=
[];
//collect currently set headers
foreach
(
$this
->
getRequestHeaders
()
as
$header
=>
$value
)
{
array_push
(
$parsedHeader
,
$header
.
':'
.
$value
);
}
//parse header into right format key:value
foreach
(
$headers
as
$header
=>
$value
)
{
array_push
(
$parsedHeader
,
$header
.
':'
.
$value
);
}
//set headers
$this
->
setOption
(
CURLOPT_HTTPHEADER
,
$parsedHeader
);
}
return
$this
;
}
/**
* Set a single header for request.
*
* @param string $header
* @param string $value
*
* @return $this
*/
public
function
setHeader
(
$header
,
$value
)
{
//init
$parsedHeader
=
[];
//collect currently set headers
foreach
(
$this
->
getRequestHeaders
()
as
$headerToSet
=>
$valueToSet
)
{
array_push
(
$parsedHeader
,
$headerToSet
.
':'
.
$valueToSet
);
}
//add override new header
if
(
strlen
(
$header
)
>
0
)
{
array_push
(
$parsedHeader
,
$header
.
':'
.
$value
);
}
//set headers
$this
->
setOption
(
CURLOPT_HTTPHEADER
,
$parsedHeader
);
return
$this
;
}
/**
* Unset a single header.
*
* @param string $header
*
* @return $this
*/
public
function
unsetHeader
(
$header
)
{
//init
$parsedHeader
=
[];
//collect currently set headers and filter "unset" header param.
foreach
(
$this
->
getRequestHeaders
()
as
$headerToSet
=>
$valueToSet
)
{
if
(
$header
!==
$headerToSet
)
{
array_push
(
$parsedHeader
,
$headerToSet
.
':'
.
$valueToSet
);
}
}
//set headers
$this
->
setOption
(
CURLOPT_HTTPHEADER
,
$parsedHeader
);
return
$this
;
}
/**
* Get all request headers as key:value array
*
* @return array
*/
public
function
getRequestHeaders
()
{
//Init
$requestHeaders
=
$this
->
getOption
(
CURLOPT_HTTPHEADER
);
$parsedRequestHeaders
=
[];
if
(
is_array
(
$requestHeaders
))
{
foreach
(
$requestHeaders
as
$headerValue
)
{
list
(
$key
,
$value
)
=
explode
(
':'
,
$headerValue
,
2
);
$parsedRequestHeaders
[
$key
]
=
$value
;
}
}
return
$parsedRequestHeaders
;
}
/**
* Get specific request header as key:value array
*
* @param string $headerKey
*
* @return string|null
*/
public
function
getRequestHeader
(
$headerKey
)
{
//Init
$parsedRequestHeaders
=
$this
->
getRequestHeaders
();
return
isset
(
$parsedRequestHeaders
[
$headerKey
])
?
$parsedRequestHeaders
[
$headerKey
]
:
null
;
}
/**
* Unset a single curl option
*
* @param string $key
*
* @return $this
*/
public
function
unsetOption
(
$key
)
{
//reset a single option if its set already
if
(
isset
(
$this
->
_options
[
$key
]))
{
unset
(
$this
->
_options
[
$key
]);
}
return
$this
;
}
/**
* Unset all curl option, excluding default options.
*
* @return $this
*/
public
function
unsetOptions
()
{
//reset all options
if
(
isset
(
$this
->
_options
))
{
$this
->
_options
=
[];
}
return
$this
;
}
/**
* Total reset of options, responses, etc.
*
* @return $this
*/
public
function
reset
()
{
if
(
$this
->
curl
!==
null
)
{
curl_close
(
$this
->
curl
);
//stop curl
}
//reset all options
if
(
isset
(
$this
->
_options
))
{
$this
->
_options
=
[];
}
//reset response & status params
$this
->
curl
=
null
;
$this
->
errorCode
=
null
;
$this
->
response
=
null
;
$this
->
responseCode
=
null
;
$this
->
responseCharset
=
null
;
$this
->
responseLength
=
-
1
;
$this
->
responseType
=
null
;
$this
->
errorText
=
null
;
$this
->
_postParams
=
[];
$this
->
_getParams
=
[];
return
$this
;
}
/**
* Return a single option
*
* @param string|integer $key
* @return mixed|boolean
*/
public
function
getOption
(
$key
)
{
//get merged options depends on default and user options
$mergesOptions
=
$this
->
getOptions
();
//return value or false if key is not set.
return
isset
(
$mergesOptions
[
$key
])
?
$mergesOptions
[
$key
]
:
false
;
}
/**
* Return merged curl options and keep keys!
*
* @return array
*/
public
function
getOptions
()
{
return
$this
->
_options
+
$this
->
_defaultOptions
;
}
/**
* Get curl info according to http://php.net/manual/de/function.curl-getinfo.php
*
* @param null $opt
* @return array|mixed
*/
public
function
getInfo
(
$opt
=
null
)
{
if
(
$this
->
curl
!==
null
&&
$opt
===
null
)
{
return
curl_getinfo
(
$this
->
curl
);
}
elseif
(
$this
->
curl
!==
null
&&
$opt
!==
null
)
{
return
curl_getinfo
(
$this
->
curl
,
$opt
);
}
else
{
return
[];
}
}
/**
* Performs HTTP request
*
* @param string $method
* @param boolean $raw if response body contains JSON and should be decoded -> helper.
*
* @throws Exception if request failed
*
* @return mixed
*/
protected
function
_httpRequest
(
$method
,
$raw
=
false
)
{
//set request type and writer function
$this
->
setOption
(
CURLOPT_CUSTOMREQUEST
,
strtoupper
(
$method
));
//check if method is head and set no body
if
(
$method
===
'HEAD'
)
{
$this
->
setOption
(
CURLOPT_NOBODY
,
true
);
$this
->
unsetOption
(
CURLOPT_WRITEFUNCTION
);
}
//setup error reporting and profiling
if
(
YII_DEBUG
)
{
Yii
::
trace
(
'Start sending cURL-Request: '
.
$this
->
getUrl
()
.
'\n'
,
__METHOD__
);
Yii
::
beginProfile
(
$method
.
' '
.
$this
->
_baseUrl
.
'#'
.
md5
(
serialize
(
$this
->
getOption
(
CURLOPT_POSTFIELDS
))),
__METHOD__
);
}
/**
* proceed curl
*/
$curlOptions
=
$this
->
getOptions
();
$this
->
curl
=
curl_init
(
$this
->
getUrl
());
curl_setopt_array
(
$this
->
curl
,
$curlOptions
);
if
(
true
==
$this
->
timeout_ms
&&
true
==
$this
->
connect_timeout_ms
)
{
curl_setopt
(
$this
->
curl
,
CURLOPT_TIMEOUT_MS
,
$this
->
timeout_ms
);
curl_setopt
(
$this
->
curl
,
CURLOPT_CONNECTTIMEOUT
,
$this
->
timeout_ms
);
curl_setopt
(
$this
->
curl
,
CURLOPT_CONNECTTIMEOUT_MS
,
$this
->
connect_timeout_ms
);
}
//curl_setopt($this->curl, CURLOPT_TIMEOUT,40);
$response
=
curl_exec
(
$this
->
curl
);
//check if curl was successful
if
(
$response
===
false
)
{
//set error code
$this
->
errorCode
=
curl_errno
(
$this
->
curl
);
$this
->
errorText
=
curl_strerror
(
$this
->
errorCode
);
switch
(
$this
->
errorCode
)
{
// 7, 28 = timeout
case
7
:
case
28
:
$this
->
responseCode
=
'timeout'
;
return
false
;
break
;
default
:
return
false
;
break
;
}
}
//extract header / body data if CURLOPT_HEADER are set to true
if
(
isset
(
$curlOptions
[
CURLOPT_HEADER
])
&&
$curlOptions
[
CURLOPT_HEADER
])
{
$this
->
response
=
$this
->
_extractCurlBody
(
$response
);
$this
->
responseHeaders
=
$this
->
_extractCurlHeaders
(
$response
);
}
else
{
$this
->
response
=
$response
;
}
// Extract additional curl params
$this
->
_extractAdditionalCurlParameter
();
//end yii debug profile
if
(
YII_DEBUG
)
{
Yii
::
endProfile
(
$method
.
' '
.
$this
->
getUrl
()
.
'#'
.
md5
(
serialize
(
$this
->
getOption
(
CURLOPT_POSTFIELDS
))),
__METHOD__
);
}
//check responseCode and return data/status
if
(
$this
->
getOption
(
CURLOPT_CUSTOMREQUEST
)
===
'HEAD'
)
{
return
true
;
}
else
{
$this
->
response
=
$raw
?
$this
->
response
:
Json
::
decode
(
$this
->
response
);
return
$this
->
response
;
}
}
/**
* Extract additional curl params protected class helper
*/
protected
function
_extractAdditionalCurlParameter
()
{
/**
* retrieve response code
*/
$this
->
responseCode
=
curl_getinfo
(
$this
->
curl
,
CURLINFO_HTTP_CODE
);
/**
* try extract response type & charset.
*/
$this
->
responseType
=
curl_getinfo
(
$this
->
curl
,
CURLINFO_CONTENT_TYPE
);
if
(
!
is_null
(
$this
->
responseType
)
&&
count
(
explode
(
';'
,
$this
->
responseType
))
>
1
)
{
list
(
$this
->
responseType
,
$possibleCharset
)
=
explode
(
';'
,
$this
->
responseType
);
//extract charset
if
(
preg_match
(
'~^charset=(.+?)$~'
,
trim
(
$possibleCharset
),
$matches
)
&&
isset
(
$matches
[
1
]))
{
$this
->
responseCharset
=
strtolower
(
$matches
[
1
]);
}
}
/**
* try extract response length
*/
$this
->
responseLength
=
curl_getinfo
(
$this
->
curl
,
CURLINFO_CONTENT_LENGTH_DOWNLOAD
);
if
((
int
)
$this
->
responseLength
==
-
1
)
{
$this
->
responseLength
=
strlen
(
$this
->
response
);
}
}
/**
* Extract body curl data from response
*
* @param string $response
* @return string
*/
protected
function
_extractCurlBody
(
$response
)
{
return
substr
(
$response
,
$this
->
getInfo
(
CURLINFO_HEADER_SIZE
));
}
/**
* Extract header curl data from response
*
* @param string $response
* @return array
*/
protected
function
_extractCurlHeaders
(
$response
)
{
//Init
$headers
=
[];
$headerText
=
substr
(
$response
,
0
,
strpos
(
$response
,
"
\r\n\r\n
"
));
foreach
(
explode
(
"
\r\n
"
,
$headerText
)
as
$i
=>
$line
)
{
if
(
$i
===
0
)
{
$headers
[
'http_code'
]
=
$line
;
}
else
{
list
(
$key
,
$value
)
=
explode
(
':'
,
$line
,
2
);
$headers
[
$key
]
=
ltrim
(
$value
);
}
}
return
$headers
;
}
$response
=
curl_exec
(
$this
->
curl
);
//check if curl was successful
if
(
$response
===
false
)
{
//set error code
$this
->
errorCode
=
curl_errno
(
$this
->
curl
);
$this
->
errorText
=
curl_strerror
(
$this
->
errorCode
);
switch
(
$this
->
errorCode
)
{
// 7, 28 = timeout
case
7
:
case
28
:
$this
->
responseCode
=
'timeout'
;
return
false
;
break
;
default
:
return
false
;
break
;
}
}
//extract header / body data if CURLOPT_HEADER are set to true
if
(
isset
(
$curlOptions
[
CURLOPT_HEADER
])
&&
$curlOptions
[
CURLOPT_HEADER
])
{
$this
->
response
=
$this
->
_extractCurlBody
(
$response
);
$this
->
responseHeaders
=
$this
->
_extractCurlHeaders
(
$response
);
}
else
{
$this
->
response
=
$response
;
}
// Extract additional curl params
$this
->
_extractAdditionalCurlParameter
();
//end yii debug profile
if
(
YII_DEBUG
)
{
Yii
::
endProfile
(
$method
.
' '
.
$this
->
getUrl
()
.
'#'
.
md5
(
serialize
(
$this
->
getOption
(
CURLOPT_POSTFIELDS
))),
__METHOD__
);
}
//check responseCode and return data/status
if
(
$this
->
getOption
(
CURLOPT_CUSTOMREQUEST
)
===
'HEAD'
)
{
return
true
;
}
else
{
$this
->
response
=
$raw
?
$this
->
response
:
Json
::
decode
(
$this
->
response
);
return
$this
->
response
;
}
}
/**
* Extract additional curl params protected class helper
*/
protected
function
_extractAdditionalCurlParameter
()
{
/**
* retrieve response code
*/
$this
->
responseCode
=
curl_getinfo
(
$this
->
curl
,
CURLINFO_HTTP_CODE
);
/**
* try extract response type & charset.
*/
$this
->
responseType
=
curl_getinfo
(
$this
->
curl
,
CURLINFO_CONTENT_TYPE
);
if
(
!
is_null
(
$this
->
responseType
)
&&
count
(
explode
(
';'
,
$this
->
responseType
))
>
1
)
{
list
(
$this
->
responseType
,
$possibleCharset
)
=
explode
(
';'
,
$this
->
responseType
);
//extract charset
if
(
preg_match
(
'~^charset=(.+?)$~'
,
trim
(
$possibleCharset
),
$matches
)
&&
isset
(
$matches
[
1
]))
{
$this
->
responseCharset
=
strtolower
(
$matches
[
1
]);
}
}
/**
* try extract response length
*/
$this
->
responseLength
=
curl_getinfo
(
$this
->
curl
,
CURLINFO_CONTENT_LENGTH_DOWNLOAD
);
if
((
int
)
$this
->
responseLength
==
-
1
)
{
$this
->
responseLength
=
strlen
(
$this
->
response
);
}
}
/**
* Extract body curl data from response
*
* @param string $response
* @return string
*/
protected
function
_extractCurlBody
(
$response
)
{
return
substr
(
$response
,
$this
->
getInfo
(
CURLINFO_HEADER_SIZE
));
}
/**
* Extract header curl data from response
*
* @param string $response
* @return array
*/
protected
function
_extractCurlHeaders
(
$response
)
{
//Init
$headers
=
[];
$headerText
=
substr
(
$response
,
0
,
strpos
(
$response
,
"
\r\n\r\n
"
));
foreach
(
explode
(
"
\r\n
"
,
$headerText
)
as
$i
=>
$line
)
{
if
(
$i
===
0
)
{
$headers
[
'http_code'
]
=
$line
;
}
else
{
list
(
$key
,
$value
)
=
explode
(
':'
,
$line
,
2
);
$headers
[
$key
]
=
ltrim
(
$value
);
}
}
return
$headers
;
}
}
\ No newline at end of file
common/service/chain33/Chain33Service.php
View file @
bd210511
...
...
@@ -20,7 +20,7 @@ class Chain33Service
public
function
__construct
(
$parameter
=
[])
{
if
(
empty
(
$parameter
))
{
if
(
empty
(
$parameter
))
{
$this
->
node_params
=
Yii
::
$app
->
params
[
'chain33'
];
}
else
{
$this
->
node_params
=
$parameter
;
...
...
@@ -31,8 +31,8 @@ class Chain33Service
{
$config
=
$this
->
node_params
;
$scheme
=
$config
[
'scheme'
]
??
'http'
;
$host
=
$config
[
'host'
]
??
'127.0.0.1'
;
$port
=
(
string
)
$config
[
'port'
]
??
''
;
$host
=
$config
[
'host'
]
??
'127.0.0.1'
;
$port
=
(
string
)
$config
[
'port'
]
??
''
;
if
(
$port
)
{
return
$scheme
.
'://'
.
$host
.
':'
.
$port
;
}
else
{
...
...
@@ -44,9 +44,9 @@ class Chain33Service
{
$data
=
[
'jsonrpc'
=>
'2.0'
,
'id'
=>
0
,
'method'
=>
$method
,
'params'
=>
[],
'id'
=>
0
,
'method'
=>
$method
,
'params'
=>
[],
];
if
(
!
empty
(
$params
))
{
$data
[
'params'
][]
=
$params
;
...
...
@@ -56,7 +56,11 @@ class Chain33Service
public
function
send
(
$params
=
[],
$method
=
'Chain33.Query'
)
{
$ch
=
new
Curl
();
$timeout
=
[
'connect_timeout'
=>
50000
,
'timeout_ms'
=>
40000
];
$ch
=
new
Curl
(
$timeout
);
$jsonrpc
=
self
::
jsonRpcBuild
(
$params
,
$method
);
$ch
->
setHeader
(
'Content-Type'
,
'application/json'
);
$ch
->
setRawPostData
(
$jsonrpc
);
...
...
@@ -124,7 +128,7 @@ class Chain33Service
* 获取地址下的所有token资产
*
* @param string|array $address
* @param string
$symbol
* @param string $symbol
* @return array
*/
public
function
getAccountTokenAssets
(
$address
,
$symbol
)
...
...
@@ -133,8 +137,8 @@ class Chain33Service
$address
=
[
$address
];
}
$params
=
[
"addresses"
=>
$address
,
"execer"
=>
"user.p.guodun.token"
,
"addresses"
=>
$address
,
"execer"
=>
"user.p.guodun.token"
,
"tokenSymbol"
=>
$symbol
];
return
$this
->
send
(
$params
,
'token.GetTokenBalance'
);
...
...
@@ -147,18 +151,18 @@ class Chain33Service
* @param $to
* @param $amount
* @param string $note
* @param bool
$isToken
* @param bool $isToken
* @param string $tokenSymbol
* @return array
*/
public
function
extractToken
(
$from
,
$to
,
$amount
,
$note
=
''
,
$isToken
=
true
,
$tokenSymbol
=
''
)
{
$params
=
[
"from"
=>
$to
,
"to"
=>
$from
,
"amount"
=>
-
$amount
,
"note"
=>
$note
,
"isToken"
=>
$isToken
?
true
:
false
,
"from"
=>
$to
,
"to"
=>
$from
,
"amount"
=>
-
$amount
,
"note"
=>
$note
,
"isToken"
=>
$isToken
?
true
:
false
,
"tokenSymbol"
=>
strtoupper
(
$tokenSymbol
)
];
return
$this
->
send
(
$params
,
'Chain33.SendToAddress'
);
...
...
@@ -171,18 +175,18 @@ class Chain33Service
* @param $to
* @param $amount
* @param string $note
* @param bool
$isToken
* @param bool $isToken
* @param string $tokenSymbol
* @return array
*/
public
function
transToken
(
$from
,
$to
,
$amount
,
$note
=
''
,
$isToken
=
true
,
$tokenSymbol
=
''
)
{
$params
=
[
"from"
=>
$from
,
"to"
=>
$to
,
"amount"
=>
$amount
,
"note"
=>
$note
,
"isToken"
=>
$isToken
?
true
:
false
,
"from"
=>
$from
,
"to"
=>
$to
,
"amount"
=>
$amount
,
"note"
=>
$note
,
"isToken"
=>
$isToken
?
true
:
false
,
"tokenSymbol"
=>
strtoupper
(
$tokenSymbol
)
];
return
$this
->
send
(
$params
,
'Chain33.SendToAddress'
);
...
...
@@ -191,25 +195,25 @@ class Chain33Service
public
function
createTokenRawTransaction
(
$to
,
$amount
,
$isToken
,
$tokenSymbol
,
$fee
,
$note
,
$execer
)
{
$params
=
[
"to"
=>
$to
,
"to"
=>
$to
,
"amount"
=>
$amount
,
"isToken"
=>
$isToken
,
"tokenSymbol"
=>
$tokenSymbol
,
"fee"
=>
$fee
,
"note"
=>
$note
,
"note"
=>
$note
,
"execer"
=>
$execer
,
];
return
$this
->
send
(
$params
,
'Chain33.CreateRawTransaction'
);
}
public
function
createRawTransaction
(
$to
,
$amount
,
$fee
,
$note
,
$execer
)
public
function
createRawTransaction
(
$to
,
$amount
,
$fee
,
$note
,
$execer
)
{
$params
=
[
"to"
=>
$to
,
"to"
=>
$to
,
"amount"
=>
$amount
,
"fee"
=>
$fee
,
"note"
=>
$note
,
"note"
=>
$note
,
"execer"
=>
$execer
,
];
...
...
@@ -274,11 +278,11 @@ class Chain33Service
public
function
structureTrade
(
$amount
,
$execName
,
$fee
,
$isToken
=
True
,
$to
,
$tokenSymbol
=
''
)
{
$params
=
[
"amount"
=>
$amount
,
"amount"
=>
$amount
,
"execName"
=>
$execName
,
"fee"
=>
$fee
,
"isToken"
=>
$isToken
?
true
:
false
,
"to"
=>
$to
,
"fee"
=>
$fee
,
"isToken"
=>
$isToken
?
true
:
false
,
"to"
=>
$to
,
"tokenSymbol"
=>
strtoupper
(
$tokenSymbol
)
];
return
$this
->
send
(
$params
,
'Chain33.CreateRawTransaction'
);
...
...
@@ -287,9 +291,9 @@ class Chain33Service
public
function
signTrade
(
$addr
,
$expire
=
'2h'
,
$txHex
)
{
$params
=
[
"addr"
=>
$addr
,
"addr"
=>
$addr
,
"expire"
=>
$expire
,
"txHex"
=>
$txHex
"txHex"
=>
$txHex
];
return
$this
->
send
(
$params
,
'Chain33.SignRawTx'
);
}
...
...
@@ -297,7 +301,7 @@ class Chain33Service
public
function
sendTrade
(
$data
)
{
$params
=
[
"data"
=>
$data
,
"data"
=>
$data
,
];
return
$this
->
send
(
$params
,
'Chain33.SendTransaction'
);
}
...
...
@@ -329,12 +333,12 @@ class Chain33Service
public
function
getTxByAddr
(
$addr
,
$flag
,
$count
,
$direction
,
$height
,
$index
)
{
$params
=
[
'addr'
=>
$addr
,
'flag'
=>
$flag
,
'count'
=>
$count
,
'addr'
=>
$addr
,
'flag'
=>
$flag
,
'count'
=>
$count
,
'direction'
=>
$direction
,
'height'
=>
$height
,
'index'
=>
$index
'height'
=>
$height
,
'index'
=>
$index
];
return
$this
->
send
(
$params
,
'Chain33.GetTxByAddr'
);
}
...
...
@@ -359,9 +363,9 @@ class Chain33Service
public
function
unLockWallet
(
$password
)
{
$params
=
[
'passwd'
=>
$password
,
'passwd'
=>
$password
,
'walletorticket'
=>
false
,
'timeout'
=>
30
'timeout'
=>
30
];
return
$this
->
send
(
$params
,
'Chain33.UnLock'
);
}
...
...
console/controllers/CrossChainController.php
View file @
bd210511
...
...
@@ -51,7 +51,6 @@ class CrossChainController extends Controller
'scheme'
=>
$transfer_url
[
0
],
'host'
=>
str_replace
(
'//'
,
''
,
$transfer_url
[
1
]),
'port'
=>
isset
(
$transfer_url
[
2
])
?
$transfer_url
[
2
]
:
''
,
'timeout'
=>
100
];
$service
=
new
Chain33Service
(
$node_params
);
...
...
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