Thriftphp server-side development in the previous article (Thrift's java and php Data interaction) only introduced java as the server side, but "php is the best language ", naturally, thrift server developed by php is missing.
The business example used is the previous logon and registration example.
The server side is written by php code, and the client side is written by php and java.
PhpMulServer. php (note that apache or other web servers are required for running. because thrift does not provide server load balancer itself, you can use LVS, HAProxy, Nginx, and so on to perform server load balancer for HTTP requests)
registerNamespace('Thrift', __DIR__ . '/../lib');$loader->registerDefinition('com', $GEN_DIR);$loader->register();if (php_sapi_name() == 'cli') { ini_set("display_errors", "stderr");}use Thrift\Protocol\TBinaryProtocol;use Thrift\Transport\TPhpStream;use Thrift\Transport\TBufferedTransport;use Thrift\TMultiplexedProcessor;use com\penngo\User;class RegisterServiceHandler implements \com\penngo\RegisterServiceIf { public function createUser($name, $psw){ $user = new User(); $user->id = 2; $user->name = $name; $user->password = $psw; return $user; }};class LoginServiceHandler implements \com\penngo\LoginServiceIf { public function login($name, $psw){ $user = new User(); if($name == 'penngo' && $psw == '123'){ $user->id = 1; $user->name = 'penngo'; } return $user; }};header('Content-Type', 'application/x-thrift');if (php_sapi_name() == 'cli') { echo "\r\n";}$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));$protocol = new TBinaryProtocol($transport, true, true);$tMultiplexedProcessor = new TMultiplexedProcessor();$handler = new LoginServiceHandler();$loginServiceProcessor = new LoginServiceProcessor($handler);$tMultiplexedProcessor->registerProcessor("LoginService", $loginServiceProcessor);$registerService = new RegisterServiceHandler();$registerServiceProcessor = new RegisterServiceProcessor($registerService);$tMultiplexedProcessor->registerProcessor("RegisterService", $registerServiceProcessor);$transport->open();$tMultiplexedProcessor->process($protocol, $protocol);$transport->close();
Php client call
PhpMulClient. php
registerNamespace('Thrift', __DIR__ . '/../lib');$loader->registerDefinition('com', $GEN_DIR);$loader->register();use Thrift\Protocol\TBinaryProtocol;use Thrift\Protocol\TMultiplexedProtocol;use Thrift\Transport\THttpClient;use Thrift\Transport\TBufferedTransport;use Thrift\Exception\TException;try {$socket = new THttpClient('localhost', 80, '/thrift/penngo/PhpMulServer.php');// $socket = new THttpClient('localhost', 8090, '/thrift/penngo/PhpMulServer.php'); $transport = new TBufferedTransport($socket); $protocol = new TBinaryProtocol($transport); $loginProtocol = new TMultiplexedProtocol($protocol, 'LoginService'); $loginService = new LoginServiceClient($loginProtocol); $user = $loginService->login('penngo', '123'); var_dump($user); $registerProtocol = new TMultiplexedProtocol($protocol, 'RegisterService'); $registerService = new RegisterServiceClient($registerProtocol); $user = $registerService->createUser('penngo', '123'); var_dump($user);// $transport->close();} catch (TException $tx) { print 'TException: '.$tx->getMessage()."\n";}?>
Java client
HttpClient. java
Package com. penngo. main; import org. apache. thrift. *; import org. apache. thrift. protocol. *; import org. apache. thrift. transport. *; import com. penngo. loginService; import com. penngo. registerService; import com. penngo. user; public class HttpClient {public static void main (String [] args) {try {THttpClient transport = new THttpClient (" http://localhost:80/thrift/penngo/PhpMulServer.php "); TProtocol protocol = new TBinaryProtocol (transport); TMultiplexedProtocol mp1 = new TMultiplexedProtocol (protocol," LoginService "); LoginService. client loginClient = new LoginService. client (mp1); TMultiplexedProtocol mp2 = new TMultiplexedProtocol (protocol, "RegisterService"); RegisterService. client registerClient = new RegisterService. client (mp2); // transport. open (); User user = loginClient. login ("penn Go "," 123 "); if (user! = Null) {System. out. println ("logon successful:" + user. getId () + "" + user. getName ();} else {System. out. println ("logon failed");} User user2 = registerClient. createUser ("test", "123"); if (user2! = Null) {System. out. println ("User created successfully:" + user2.getId () + "" + user2.getName ();} else {System. out. println ("failed to create user");} // transport. close ();} catch (TException x) {x. printStackTrace ();}}}