Yii 1.1: thriftclient

Thrift client for Yii
7 followers

PHP client for Thrift 0.5.0, for Yii.

As thrift is extensively used by Facebook, which is using PHP in frontend, I think that may prove that thrift is php friendly. So I choose to use it to communicate with python and java in my project.

Requirements

Yii 1.1 or above

Usage

As a consumer of a thrift service, first you need to get the .thrift definition file, then using the thrift compiler generate interface files, as in Windows:

[shell]
thrift.exe -gen php:oop service_defination.thrift

the generated files will be in a gen-php folder, a folder with the name of the php namespace definition in the .thrift file.

I assume you already extracted this extension at application.vendors directory, now copy the generated files to application.vendors.thrift.src.packages

Now add something in the config file:

'components' => array(
    ...
 
    'thrift'=>array(
        'class'=>'application.vendors.thrift.ThriftClient',
        'server_host'=>'localhost',
        'server_port'=>'9090',
        'send_timeout'=>1,
        'recv_timeout'=>5,
        'service_config'=>array(
            'search'=>array('server_host'=>'localhost','server_port'=>'11901'),
            'classifier'=>array('server_host'=>'www.leric.info','server_port'=>'11900'),
        ),
    ),
 
    ...
),

Configuration, it's done! Now you can use thrift in your code as this:

Yii::app()->thrift->service_namespace->service_method();

service_namespace is the namespace definition in the .thrift file, service_method is the method you want invoke in the service.

The client used TBufferedTransport as default transport, you can modify the ThriftClient.php around line 29 as you need

Resources

If you run into a problem, email me or leave a comment. leric.zhang#gmail.com

Change Log

  • thrift_0.2 Fix connection init bug, Add multiple service config support

Be the first person to leave a comment

Please to leave your comment.

Create extension
Downloads