问题描述:
php调用thrift接口报错
[error] 14178#14178: *1298868 FastCGI sent in stderr: "PHP message: PHP Catchable fatal error: Argument 1 passed to MonitorOsView\AgentMetricServiceClient::SearchLog() must be an instance of MonitorOsView\BasicSearchLog, array given in Thrift/gen-php/MonitorOsView/AgentMetricService.php on line 1740" while reading response header from upstream, client: , request: "GET /api/"
原因分析:
传递的第一个参数需要是BasicSearchLog的实例,types.php这个文件一般不用它,忽略了它后遇到问题会有点蒙逼
解决方案:
thrift模板:
struct BasicSearchLog {
1: required string index,
2: required i64 startTime,
3: required i64 endTime,
4: required string id;
5: required string name;
6: optional i64 age;
7: optional string address;
8: optional bool isAdmin=false;
}
Types.php代码(自动生成的文件):
class BasicSearchLog {
static $_TSPEC;
public $index = null;
public $startTime = null;
public $endTime = null;
public $size = null;
....
public function __construct($vals=null) {
if (!isset(self::$_TSPEC)) {
self::$_TSPEC = array(
1 => array(
'var' => 'index',
'type' => TType::STRING,
),
2 => array(
'var' => 'startTime',
'type' => TType::I64,
),
3 => array(
'var' => 'endTime',
'type' => TType::I64,
),
......
);
}
if (is_array($vals)) {
if (isset($vals['index'])) {
$this->index = $vals['index'];
}
if (isset($vals['startTime'])) {
$this->startTime = $vals['startTime'];
}
if (isset($vals['endTime'])) {
$this->endTime = $vals['endTime'];
}
.......
}
}
直接实例化后传递即可
$BasicSearchLog = new \MonitorOsView\BasicSearchLog($basicLogSearch);
$monitorOsViewThrift = new Thrift_MonitorOsView();
$retData = $monitorOsViewThrift->SearchDistinctLogByField($BasicSearchLog, [], [], []);