Thrift PHP Server-side development

Source: Internet
Author: User
Tags php server
In the previous article (Thrift Java and PHP data interaction) Only introduced Java as the server side, but "PHP is the best language", naturally less PHP development of 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)

 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 Calls

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 ', +, '/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);//$transportClose ();} 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 THTTPCLI ENT ("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 succeeded:" + user.getid () + "" + user.ge Tname ());} else {System.out.println ("Login Failed");} User user2 = registerclient.createuser ("Test", "123"), if (user2! = null) {SYSTEM.OUT.PRINTLN ("Create user success: "+ User2.getid () +" "+ user2.getname ());} else {System.out.println ("Create user Failed");} Transport.close ();} catch (Texception x) {x.printstacktrace ();}}}
  • Related Article

    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.