如何设置php thrift 超时时间

 我来答
huanglenzhi
2015-01-01 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517184
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
  最近需要用到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;
  }

  }
转载
匿名用户
2015-06-26
展开全部
最近需要用到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;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式