网站地图    收藏   

主页 > 后端 > php资料库 >

Thrift在PHP中调用的方法_自学php网

来源:自学PHP网    时间:2014-12-04 22:12 作者: 阅读:

[导读] 最近需要用到Thrift接口, 是Facebook开发的apache开源项目,公司要用,研究了一下 所以写了个PHP调用Thrift的方法事例 以下是代码,以免以后别人再走弯路 我是在Yii框架中实现的,和原生代码...

最近需要用到Thrift接口,
是Facebook开发的apache开源项目,公司要用,研究了一下
所以写了个PHP调用Thrift的方法事例
以下是代码,以免以后别人再走弯路
我是在Yii框架中实现的,和原生代码应该是一样的
官方下载包里也有PHP调用Thrift的例子

<?php
/**
* @author fzxa
* @create 2012/05/22

Thrift推荐职位相关接口
注:详细说明文档在 \protected\components\thrift\推荐系统API.docx

@desc 说明Thrift接口数据格式:
EntityType接口数据格式
enum EntityType {
POSITION = 0,
RESUME = 1
}

EntityInfo接口数据格式
struct EntityInfo {
1: EntityType type,
2: string id, // 实体id
3: double rank, // rank值
4: string address, // 工作地点
5: i32 salary, // 薪水
6: string industry, // 行业类别
7: string profession, // 职业类别
8: i32 workingage, // 工作年限
9: i32 degree, // 学历
10: string time, // 过期时间或更新时间
11: map<string,string> descriptions, // 文字描述,其中key为字段名,value为字段内容
12: map<string,i32> tags, // 技能标签
}

ResultInfo接口数据格式
struct ResultInfo {
1: EntityType type,
2: string id, // 实体id
3: i32 score, // 推荐分数,取值范围:[0, 100]
}

*/

$GLOBALS['THRIFT_ROOT'] = $_SERVER['DOCUMENT_ROOT'].'/p/protected/components/thrift';
require_once( $GLOBALS['THRIFT_ROOT'] . '/Thrift.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TSocket.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TBufferedTransport.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/protocol/TBinaryProtocol.php' );

//生成的文件 RecommendService.php
require_once( $GLOBALS['THRIFT_ROOT'] . '/RecommendService.php' );
//ini_set('display_errors', E_ALL);

class ThriftInterface
{
private static $thriftHost = '*.*.*.*'; //Thrift接口服务器IP
private static $thriftPort = 8080; //Thrift端口

/**
* 建立Thrift连接
* @return boolean
*/
private static function client(){


$socket = new TSocket(self::$thriftHost, self::$thriftPort);

$socket->setSendTimeout(10000);
$socket->setRecvTimeout(20000);
$transport = new TBufferedTransport($socket);
$protocol = new TBinaryProtocol($transport);
$client = new RecommendServiceClient($protocol);
$transport->open();
$socket->setDebug(TRUE);

return $client;

}

/**
* 获取职位推荐列表ById
*
* @param Number $type 0:代表职位,1:代表简历
* @param Number $id 实体ID
* @param Array 推荐结果列表
* @example: $Recommend = ThriftInterface::getRecommendedResultById('1','1982');
*/
public static function getRecommendedResultById($type,$id){

$client = self::client();
$ResultById = $client->getRecommendedResultById($type, $id);

return $ResultById;

}

/**
* 获取推荐列表ByEntity
* @param EntityInfo $entity 职位或简历实体信息
*/
public static function getRecommendedResultByEntity($entity){

$client = self::client();

$EntityInfo = new EntityInfo();

$EntityInfo->type = (string)$entity['type'];
$EntityInfo->id = (string)$entity['id'];
$EntityInfo->rank = (float)$entity['rank'];
$EntityInfo->address = (string)$entity['address'];
$EntityInfo->salary = (int)$entity['salary'];
$EntityInfo->industry = (string)$entity['industry'];
$EntityInfo->profession = (string)$entity['profession'];
$EntityInfo->workingage = (int)$entity['workingage'];
$EntityInfo->degree = (int)$entity['degree'];
$EntityInfo->time = (string)$entity['time'];
$EntityInfo->descriptions = (array)$entity['descriptions'];
$EntityInfo->tags = (array)$entity['tags'];

$ResultByEntity = $client->getRecommendedResultByEntity($EntityInfo);

return $ResultByEntity;
}

/**
* 计算任一简历与职位的匹配分数ById
*
* @param Number $type1 0:代表职位,1:代表简历
* @param Number $id1 实体ID
* @param Number $type2 0:代表职位,1:代表简历
* @param Number $id2 实体ID
* @example Number
*/
public static function getRecommendedScoreById($type1, $id1, $type2, $id2){

$client = self::client();

$ScoreById = $client->getRecommendedScoreById($type1, $id1, $type2, $id2);

return $ScoreById;
}

/**
* 计算任一简历与职位的匹配分数ByEntity
*
* @param EntityInfo $entity 实体的所有信息
* @param EntityType $type2
* @param string $id2 实体ID2
* @return i32 简历与职位的推荐分数,取值范围:[0, 100]
*/
public static function getRecommendedScoreByEntity($entity, $type2, $id2){

$client = self::client();

$EntityInfo = new EntityInfo();
$EntityInfo->type = (string)$entity['type'];
$EntityInfo->id = (string)$entity['id'];
$EntityInfo->rank = (float)$entity['rank'];
$EntityInfo->address = (string)$entity['address'];
$EntityInfo->salary = (int)$entity['salary'];
$EntityInfo->industry = (string)$entity['industry'];
$EntityInfo->profession = (string)$entity['profession'];
$EntityInfo->workingage = (int)$entity['workingage'];
$EntityInfo->degree = (int)$entity['degree'];
$EntityInfo->time = (string)$entity['time'];
$EntityInfo->descriptions = (array)$entity['descriptions'];
$EntityInfo->tags = (array)$entity['tags'];

$ResultInfo = new ResultInfo();
$ResultInfo->type = (string)$type2['type'];
$ResultInfo->id = (string)$type2['id'];
$ResultInfo->score = (int)$type2['score'];

$ScoreByEntity = $client->getRecommendedScoreByEntity($EntityInfo, $type2, $id2);

return $ScoreByEntity;
}


}

 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论