Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
system
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
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
_site-res
system
Commits
50ad8055
Commit
50ad8055
authored
May 02, 2018
by
tufengqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
00a09135
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
343 additions
and
21 deletions
+343
-21
Client.php
classes/fpf/thrift/Client.php
+30
-21
THttpClient.php
classes/fpf/thrift/THttpClient.php
+256
-0
ThriftServiceFactory.php
classes/fpf/thrift/ThriftServiceFactory.php
+19
-0
ThriftServiceFactoryProxy.php
classes/fpf/thrift/ThriftServiceFactoryProxy.php
+38
-0
No files found.
classes/fpf/thrift/Client.php
View file @
50ad8055
<?php
namespace
tutorial\php
;
namespace
fpf
;
error_reporting
(
E_ALL
);
$root_path
=
dirname
(
dirname
(
dirname
(
__DIR__
)));
require_once
$root_path
.
'/vendor/autoload.php'
;
...
...
@@ -23,6 +23,15 @@ require_once $root_path . '/vendor/autoload.php';
* specific language governing permissions and limitations
* under the License.
*/
$root_path
=
dirname
(
dirname
(
dirname
(
__DIR__
)));
require_once
$root_path
.
'/vendor/thrift_33cn/ClassLoader/ThriftClassLoader.php'
;
use
Thrift\ClassLoader\ThriftClassLoader
;
$loader
=
new
ThriftClassLoader
();
echo
$root_path
.
'/classes/'
;
$loader
->
registerNamespace
(
'fpf\\thrift'
,
$root_path
.
'/classes'
);
$loader
->
register
();
use
Thrift\Protocol\TBinaryProtocol
;
use
Thrift\Protocol\TJSONProtocol
;
...
...
@@ -30,28 +39,28 @@ use Thrift\Transport\TSocket;
use
Thrift\Transport\THttpClient
;
use
Thrift\Transport\TBufferedTransport
;
use
Thrift\Exception\TException
;
use
service\market\object\base\BaseListRequestCate
;
use
service\market\constant\CoinCate
;
use
service\market\constant\DataSource
;
use
fpf\thrift\ThriftServiceFactory
;
try
{
$socket
=
new
THttpClient
(
'service.local.zhaobi.com'
,
80
,
'/market/BaseMarketService/getList'
);
$content_type
=
1
;
$content_type_mapping
=
[
0
=>
'application/thrift-binary'
,
1
=>
'application/thrift-json'
,
];
$socket
->
addHeaders
([
'Content-type'
=>
$content_type_mapping
[
$content_type
]]);
$transport
=
new
TBufferedTransport
(
$socket
,
1024
,
1024
);
if
(
0
===
$content_type
)
{
$protocol
=
new
TBinaryProtocol
(
$transport
);
}
elseif
(
1
===
$content_type
)
{
$protocol
=
new
TJsonProtocol
(
$transport
);
}
$client
=
new
\service\market\BaseMarketServiceClient
(
$protocol
);
$transport
->
open
();
$requests
=
new
\service\market\object\base\BaseListRequest
();
$requests
->
cate
=
BaseListRequestCate
::
ALL
;
$res
=
$client
->
getList
(
$requests
);
$transport
->
close
();
/**
* @var $base_market_service_client \service\market\BaseMarketServiceClient
*/
$base_market_service_client
=
ThriftServiceFactory
::
getService
(
'\service\market\BaseMarketServiceClient'
);
// $request = new \service\market\object\base\BaseListRequest();
// $request->cate = CoinCate::USDT;
// $request->data_source = DataSource::CACHE;
$request
=
new
\service\market\object\base\BaseOneRequest
();
// $request->cate = CoinCate::BTC;
$request
->
data_source
=
DataSource
::
CACHE
;
$request
->
coin_name
=
'BCCBTC'
;
$res
=
$base_market_service_client
->
getOne
(
$request
);
var_dump
(
$res
);
}
catch
(
TException
$tx
)
{
print
'TException: '
.
$tx
->
getMessage
()
.
"
\n
"
;
...
...
classes/fpf/thrift/THttpClient.php
0 → 100644
View file @
50ad8055
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.transport
*/
namespace
fpf\thrift
;
use
Thrift\Exception\TTransportException
;
use
Thrift\Factory\TStringFuncFactory
;
/**
* HTTP client for Thrift
*
* @package thrift.transport
*/
class
THttpClient
extends
\Thrift\Transport\TTransport
{
/**
* The host to connect to
*
* @var string
*/
protected
$host_
;
/**
* The port to connect on
*
* @var int
*/
protected
$port_
;
/**
* The URI to request
*
* @var string
*/
protected
$uri_
;
/**
* The scheme to use for the request, i.e. http, https
*
* @var string
*/
protected
$scheme_
;
/**
* Buffer for the HTTP request data
*
* @var string
*/
protected
$buf_
;
/**
* Input socket stream.
*
* @var resource
*/
protected
$handle_
;
/**
* Read timeout
*
* @var float
*/
protected
$timeout_
;
/**
* http headers
*
* @var array
*/
protected
$headers_
;
protected
$uri_no_func
;
/**
* Make a new HTTP client.
*
* @param string $host
* @param int $port
* @param string $uri_no_func
*/
public
function
__construct
(
$host
,
$port
=
80
,
$uri_no_func
=
''
,
$scheme
=
'http'
)
{
if
((
TStringFuncFactory
::
create
()
->
strlen
(
$uri_no_func
)
>
0
)
&&
(
$uri_no_func
{
0
}
!=
'/'
))
{
$uri_no_func
=
'/'
.
$uri_no_func
;
}
$this
->
scheme_
=
$scheme
;
$this
->
host_
=
$host
;
$this
->
port_
=
$port
;
$this
->
uri_no_func
=
$uri_no_func
;
$this
->
uri_
=
$uri_no_func
;
$this
->
buf_
=
''
;
$this
->
handle_
=
null
;
$this
->
timeout_
=
null
;
$this
->
headers_
=
array
();
}
public
function
setFuncUri
(
$uri_func
)
{
$this
->
uri_
=
rtrim
(
$this
->
uri_no_func
,
'/'
)
.
'/'
.
$uri_func
;
}
/**
* Set read timeout
*
* @param float $timeout
*/
public
function
setTimeoutSecs
(
$timeout
)
{
$this
->
timeout_
=
$timeout
;
}
/**
* Whether this transport is open.
*
* @return boolean true if open
*/
public
function
isOpen
()
{
return
true
;
}
/**
* Open the transport for reading/writing
*
* @throws TTransportException if cannot open
*/
public
function
open
()
{
}
/**
* Close the transport.
*/
public
function
close
()
{
if
(
$this
->
handle_
)
{
@
fclose
(
$this
->
handle_
);
$this
->
handle_
=
null
;
}
}
/**
* Read some data into the array.
*
* @param int $len How much to read
* @return string The data that has been read
* @throws TTransportException if cannot read any more data
*/
public
function
read
(
$len
)
{
$data
=
@
fread
(
$this
->
handle_
,
$len
);
if
(
$data
===
false
||
$data
===
' '
)
{
$md
=
stream_get_meta_data
(
$this
->
handle_
);
if
(
$md
[
' timed_out '
])
{
throw
new
TTransportException
(
' THttpClient : timed out reading '
.
$len
.
' bytes from '
.
$this
->
host_
.
' :'
.
$this
->
port_
.
$this
->
uri_
,
TTransportException
::
TIMED_OUT
);
}
else
{
throw
new
TTransportException
(
' THttpClient : Could not read '
.
$len
.
' bytes from '
.
$this
->
host_
.
' : '
.
$this
->
port_
.
$this
->
uri_
,
TTransportException
::
UNKNOWN
);
}
}
return
$data
;
}
/**
* Writes some data into the pending buffer
*
* @param string $buf The data to write
* @throws TTransportException if writing fails
*/
public
function
write
(
$buf
)
{
$this
->
buf_
.=
$buf
;
}
/**
* Opens and sends the actual request over the HTTP connection
*
* @throws TTransportException if a writing error occurs
*/
public
function
flush
()
{
// God, PHP really has some esoteric ways of doing simple things.
$host
=
$this
->
host_
.
(
$this
->
port_
!=
80
?
' :'
.
$this
->
port_
:
' '
);
$headers
=
array
();
$defaultHeaders
=
array
(
' Host '
=>
$host
,
' Accept '
=>
' application / x - thrift '
,
' User - Agent '
=>
' PHP / THttpClient '
,
' Content - Type '
=>
' application / x - thrift '
,
' Content - Length '
=>
TStringFuncFactory
::
create
()
->
strlen
(
$this
->
buf_
)
);
foreach
(
array_merge
(
$defaultHeaders
,
$this
->
headers_
)
as
$key
=>
$value
)
{
$headers
[]
=
"
$key
:
$value
"
;
}
$options
=
array
(
' method '
=>
' POST '
,
' header '
=>
implode
(
"
\r\n
"
,
$headers
),
' max_redirects '
=>
1
,
' content '
=>
$this
->
buf_
);
if
(
$this
->
timeout_
>
0
)
{
$options
[
' timeout '
]
=
$this
->
timeout_
;
}
$this
->
buf_
=
' '
;
$contextid
=
stream_context_create
(
array
(
' http '
=>
$options
));
$this
->
handle_
=
@
fopen
(
$this
->
scheme_
.
' ://'
.
$host
.
$this
->
uri_
,
'r'
,
false
,
$contextid
);
// Connect failed?
if
(
$this
->
handle_
===
false
)
{
$this
->
handle_
=
null
;
$error
=
'THttpClient: Could not connect to '
.
$host
.
$this
->
uri_
;
throw
new
TTransportException
(
$error
,
TTransportException
::
NOT_OPEN
);
}
}
public
function
addHeaders
(
$headers
)
{
$this
->
headers_
=
array_merge
(
$this
->
headers_
,
$headers
);
}
}
classes/fpf/thrift/ThriftServiceFactory.php
0 → 100644
View file @
50ad8055
<?php
namespace
fpf\thrift
;
class
ThriftServiceFactory
{
private
static
$sync_services
=
[];
public
static
function
getService
(
$class_name
)
{
if
(
!
empty
(
self
::
$sync_services
[
$class_name
]))
{
return
self
::
$sync_services
[
$class_name
];
}
self
::
$sync_services
[
$class_name
]
=
new
ThriftServiceFactoryProxy
(
$class_name
);
return
self
::
$sync_services
[
$class_name
];
}
}
\ No newline at end of file
classes/fpf/thrift/ThriftServiceFactoryProxy.php
0 → 100644
View file @
50ad8055
<?php
namespace
fpf\thrift
;
class
ThriftServiceFactoryProxy
{
private
$instance
=
null
;
private
$transport
=
null
;
private
$socket
=
null
;
public
function
__construct
(
$class_name
)
{
$content_type
=
1
;
$content_type_mapping
=
[
0
=>
'application/thrift-binary'
,
1
=>
'application/thrift-json'
,
];
$this
->
socket
=
new
THttpClient
(
'service.local.zhaobi.com'
,
80
,
'/market/BaseMarketService/'
);
$this
->
socket
->
addHeaders
([
'Content-type'
=>
$content_type_mapping
[
$content_type
]]);
$this
->
transport
=
new
\Thrift\Transport\TBufferedTransport
(
$this
->
socket
,
1024
,
1024
);
if
(
0
===
$content_type
)
{
$protocol
=
new
\Thrift\Protocol\TBinaryProtocol
(
$this
->
transport
);
}
elseif
(
1
===
$content_type
)
{
$protocol
=
new
\Thrift\Protocol\TJSONProtocol
(
$this
->
transport
);
}
$this
->
instance
=
new
$class_name
(
$protocol
);
}
public
function
__call
(
$name
,
$arguments
)
{
$this
->
transport
->
open
();
$this
->
socket
->
setFuncUri
(
$name
);
$ret
=
call_user_func_array
([
$this
->
instance
,
$name
],
$arguments
);
$this
->
transport
->
close
();
return
$ret
;
}
}
\ 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