Commit 87f42ce4 authored by tufengqi's avatar tufengqi

客户端新增guzzle

parent a4ec1553
<?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 TGuzzleClient extends \Thrift\Transport\TTransport
{
private static $curlHandle;
/**
* 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 $request_;
/**
* Buffer for the HTTP response data.
*
* @var binary string
*/
protected $response_;
/**
* 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->request_ = '';
$this->response_ = 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()
{
$this->request_ = '';
$this->response_ = 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)
{
if ($len >= strlen($this->response_)) {
return $this->response_;
} else {
$ret = substr($this->response_, 0, $len);
$this->response_ = substr($this->response_, $len);
return $ret;
}
}
/**
* Writes some data into the pending buffer
*
* @param string $buf The data to write
* @throws TTransportException if writing fails
*/
public function write($buf)
{
$this->request_ .= $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_ : '');
$fullUrl = $this->scheme_ . "://" . $host . $this->uri_;
$headers = [];
$defaultHeaders = [
'Host' => $host,
'User-Agent' => 'PHP/THttpClient',
'Accept' => 'application/x-thrift',
'Content-Type' => 'application/x-thrift',
'Content-Length' => TStringFuncFactory::create()->strlen($this->request_)
];
foreach (array_merge($defaultHeaders, $this->headers_) as $key => $value) {
$headers[] = "$key: $value";
}
// Send an asynchronous request.
$request = new \GuzzleHttp\Psr7\Request('POST', $fullUrl, $headers, $this->request_);
$this->request_ = '';
$options = [
'allow_redirects' => [
'max' => 5,
],
'timeout' => number_format($this->timeout_ / 1000, 1) // s
];
$promise = $client->sendAsync($request, $options)->then(function ($response) {
$this->response_ = $response->getBody();
echo 'I completed! ' . $this->response_;
});
$promise->wait();
}
public static function closeCurlHandle()
{
try {
if (self::$curlHandle) {
curl_close(self::$curlHandle);
self::$curlHandle = null;
}
} catch (\Exception $x) {
error_log('There was an error closing the curl handle: ' . $x->getMessage());
}
}
public function addHeaders($headers)
{
$this->headers_ = array_merge($this->headers_, $headers);
}
}
...@@ -48,7 +48,7 @@ class ThriftServiceFactoryProxy ...@@ -48,7 +48,7 @@ class ThriftServiceFactoryProxy
self::BINARY => 'application/thrift-binary', self::BINARY => 'application/thrift-binary',
self::JSON => 'application/thrift-json', self::JSON => 'application/thrift-json',
]; ];
$this->socket = new TCurlClient($service_real['host'], $service_real['port'], '/' . $service_name_space_str . '/' . $service_str . '/'); $this->socket = new TGuzzleClient($service_real['host'], $service_real['port'], '/' . $service_name_space_str . '/' . $service_str . '/');
$this->socket->addHeaders(['Content-type' => $content_type_mapping[$content_type]]); $this->socket->addHeaders(['Content-type' => $content_type_mapping[$content_type]]);
$this->transport = new \Thrift\Transport\TBufferedTransport($this->socket, 1024, 1024); $this->transport = new \Thrift\Transport\TBufferedTransport($this->socket, 1024, 1024);
if (self::BINARY === $content_type) { if (self::BINARY === $content_type) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment