Commit 23d3bad0 authored by tufengqi's avatar tufengqi

init

parents
/vendor
/build
.DS_Store
.idea
/composer.lock
.vscode
\ No newline at end of file
<?php
namespace tutorial\php;
error_reporting(E_ALL);
$root_path = dirname(dirname(dirname(__DIR__)));
require_once $root_path . '/vendor/autoload.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.
*/
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TJSONProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
use service\market\object\base\BaseListRequestCate;
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_dump($res);
} catch (TException $tx) {
print 'TException: ' . $tx->getMessage() . "\n";
}
<?php
namespace tutorial\php;
error_reporting(E_ALL);
$root_path = dirname(dirname(dirname(__DIR__)));
require_once $root_path . '/vendor/thrift_33cn/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
$GEN_DIR = $root_path . '/vendor/thrift_33cn/Thrift/gen-php/';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $root_path . '/vendor/thrift_33cn');
$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('tutorial', $GEN_DIR);
$loader->register();
/*
* 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.
*/
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TJSONProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
try {
$socket = new THttpClient('localhost', 80, '/classes/fpf/thrift/Server.php');
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TJSONProtocol($transport);
$client = new \tutorial\CalculatorClient($protocol);
$transport->open();
// $client->ping();
// print "ping()\n";
// $sum = $client->add(1, 1);
// print "1+1=$sum\n";
$std = new \tutorial\MemberInfoRequest();
$std->type_id = 1;
$std->id = 5;
$arr[] = $std;
$arr[] = $std;
$sum = $client->addList($arr);
print "5+5=$sum\n";
// $work = new \tutorial\Work();
// $work->op = \tutorial\Operation::DIVIDE;
// $work->num1 = 1;
// $work->num2 = 0;
// try {
// $client->calculate(1, $work);
// print "Whoa! We can divide by zero?\n";
// } catch (\tutorial\InvalidOperation $io) {
// print "InvalidOperation: $io->why\n";
// }
// $work->op = \tutorial\Operation::SUBTRACT;
// $work->num1 = 15;
// $work->num2 = 10;
// $diff = $client->calculate(1, $work);
// print "15-10=$diff\n";
// $log = $client->getStruct(1);
// print "Log: $log->value\n";
$transport->close();
} catch (TException $tx) {
print 'TException: ' . $tx->getMessage() . "\n";
}
<?php
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TPhpStream;
use Thrift\Transport\TBufferedTransport;
use Thrift\TMultiplexedProcessor;
class Launcher
{
public function run()
{
$uri = $_SERVER['REQUEST_URI'];
$pos = strpos($uri, '?');
if ($pos) {
$uri = substr($uri, 0, $pos);
}
list(, $module_name, $biz_name) = explode('/', $uri);
}
}
\ No newline at end of file
<?php
namespace tutorial\php;
error_reporting(E_ERROR);
$data = file_get_contents("php://input");
file_put_contents("./a.txt", $data, FILE_APPEND);
//echo 'from server';
$root_path = dirname(dirname(dirname(__DIR__)));
require_once $root_path . '/vendor/thrift_33cn/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
$GEN_DIR = $root_path . '/vendor/thrift_33cn/Thrift/gen-php/';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $root_path . '/vendor/thrift_33cn');
$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('tutorial', $GEN_DIR);
$loader->register();
/*
* 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.
*/
/*
* This is not a stand-alone server. It should be run as a normal
* php web script (like through Apache's mod_php) or as a cgi script
* (like with the included runserver.py). You can connect to it with
* THttpClient in any language that supports it. The PHP tutorial client
* will work if you pass it the argument "--http".
*/
if (php_sapi_name() == 'cli') {
ini_set("display_errors", "stderr");
}
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TJSONProtocol;
use Thrift\Transport\TPhpStream;
use Thrift\Transport\TBufferedTransport;
class CalculatorHandler implements \tutorial\CalculatorIf
{
protected $log = array();
public function ping()
{
error_log("ping()");
}
public function add($num1, $num2)
{
// error_log("add({$num1}, {$num2})");
return $num1 + $num2;
}
public function addList(array $arr)
{
$sum = 0;
foreach ($arr as $item){
$sum+=$item->id;
}
return $sum;
}
public function calculate($logid, \tutorial\Work $w)
{
error_log("calculate({$logid}, {{$w->op}, {$w->num1}, {$w->num2}})");
switch ($w->op) {
case \tutorial\Operation::ADD:
$val = $w->num1 + $w->num2;
break;
case \tutorial\Operation::SUBTRACT:
$val = $w->num1 - $w->num2;
break;
case \tutorial\Operation::MULTIPLY:
$val = $w->num1 * $w->num2;
break;
case \tutorial\Operation::DIVIDE:
if ($w->num2 == 0) {
$io = new \tutorial\InvalidOperation();
$io->whatOp = $w->op;
$io->why = "Cannot divide by 0";
throw $io;
}
$val = $w->num1 / $w->num2;
break;
default:
$io = new \tutorial\InvalidOperation();
$io->whatOp = $w->op;
$io->why = "Invalid Operation";
throw $io;
}
$log = new \shared\SharedStruct();
$log->key = $logid;
$log->value = (string)$val;
$this->log[$logid] = $log;
return $val;
}
public function getStruct($key)
{
error_log("getStruct({$key})");
// This actually doesn't work because the PHP interpreter is
// restarted for every request.
//return $this->log[$key];
return new \shared\SharedStruct(array("key" => $key, "value" => "PHP is stateless!"));
}
public function zip()
{
error_log("zip()");
}
}
;
header('Content-Type', 'application/x-thrift');
if (php_sapi_name() == 'cli') {
echo "\r\n";
}
$handler = new CalculatorHandler();
$processor = new \tutorial\CalculatorProcessor($handler);
$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
$protocol = new TJSONProtocol($transport, true, true);
$transport->open();
$processor->process($protocol, $protocol);
$transport->close();
[1,"addList",1,0,{"1":{"lst":["rec",2,{"1":{"i16":1},"2":{"i64":5}},{"1":{"i16":1},"2":{"i64":5}}]}}]
\ No newline at end of file
{
"name": "system",
"type": "library",
"require": {},
"require-dev": {
"thrift_33cn": "@dev",
"service/market": "@dev"
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
},
"thrift_33cn": {
"type": "git",
"url": "git@gitlab.33.cn:_site-res/thrift_33cn.git"
},
"service/market": {
"type": "git",
"url": "git@gitlab.33.cn:_site-service/market-service-intf.git"
}
}
}
\ No newline at end of file
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