Thrift PHP Server-side development

Source: Internet
Author: User
Tags php server

In the previous article (Thrift Java and PHP Data interaction http://my.oschina.net/penngo/blog/489311) only introduced Java as the server side, but "PHP is the best language", Naturally less PHP developed by the thrift server side.

Business example used, or login and registration as an example of the previous article


The server driven by PHP code, and the client is written by PHP and Java.

phpmulserver.php (note the need to use Apache or other Web server to run, because the thrift itself does not provide load balancing, you can consider using LVS, HAProxy, Nginx and so on to the HTTP request Load Balancing processing)

<?phpnamespace com\penngo;error_reporting (E_all); require_once __dir__. ' /.. /lib/thrift/classloader/thriftclassloader.php ';use thrift\classloader\thriftclassloader; $GEN _dir =  realpath (DirName (__file__). ' /..‘).‘ /gen-php '; $loader  = new thriftclassloader (); $loader->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&nbsp, $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 Calls

phpmulclient.php

<?phpnamespace com\penngo;error_reporting (E_all); require_once __dir__. ' /.. /lib/thrift/classloader/thriftclassloader.php ';use thrift\classloader\thriftclassloader; $GEN _dir =  realpath (DirName (__file__). ' /..‘).‘ /gen-php '; $loader  = new thriftclassloader (); $loader->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 ("Penngo",  "123 ");if  (user != null)  {system.out.println (" Login Successful: " + user.getid ()  + "   "+ user.getname ());} &NBSP;ELSE&NBSP;{SYSTEM.OUT.PRINTLN ("Login Failed");} User user2 = registerclient.createuser ("Test",  "123");if  (User2 != null) &NBSP;{SYSTEM.OUT.PRINTLN ("Create User success:"  + user2.getid ()  +  " " + user2.getname ());} &NBSP;ELSE&NBSP;{SYSTEM.OUT.PRINTLN ("Create user Failed");} Transport.close ();}  catch  (texception x)  {x.printstacktrace ();}}


Thrift PHP Server-side development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.