这是我的节俭定义
namespace php mobiledata
struct sms
{
1: string from,
2: string to,
3: string smstext,
4: string smsdatetime,
5: string smsdirection
}
struct smsdetails
{
1: list<sms> smsdata
}
service mobiledataservice
{
void insertsmsdata (1: smsdetails smslist)
}
Types.php中的thrift生成的php类型是
<?php
namespace mobiledata;
/**
* Autogenerated by Thrift Compiler (1.0.0-dev)
*
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
* @generated
*/
use Thrift\Base\TBase;
use Thrift\Type\TType;
use Thrift\Type\TMessageType;
use Thrift\Exception\TException;
use Thrift\Exception\TProtocolException;
use Thrift\Protocol\TProtocol;
use Thrift\Protocol\TBinaryProtocolAccelerated;
use Thrift\Exception\TApplicationException;
class sms {
static $_TSPEC;
/**
* @var string
*/
public $from = null;
/**
* @var string
*/
public $to = null;
/**
* @var string
*/
public $smstext = null;
/**
* @var string
*/
public $smsdatetime = null;
/**
* @var string
*/
public $smsdirection = null;
public function __construct($vals=null) {
if (!isset(self::$_TSPEC)) {
self::$_TSPEC = array(
1 => array(
'var' => 'from',
'type' => TType::STRING,
),
2 => array(
'var' => 'to',
'type' => TType::STRING,
),
3 => array(
'var' => 'smstext',
'type' => TType::STRING,
),
4 => array(
'var' => 'smsdatetime',
'type' => TType::STRING,
),
5 => array(
'var' => 'smsdirection',
'type' => TType::STRING,
),
);
}
if (is_array($vals)) {
if (isset($vals['from'])) {
$this->from = $vals['from'];
}
if (isset($vals['to'])) {
$this->to = $vals['to'];
}
if (isset($vals['smstext'])) {
$this->smstext = $vals['smstext'];
}
if (isset($vals['smsdatetime'])) {
$this->smsdatetime = $vals['smsdatetime'];
}
if (isset($vals['smsdirection'])) {
$this->smsdirection = $vals['smsdirection'];
}
}
}
public function getName() {
return 'sms';
}
public function read($input)
{
$xfer = 0;
$fname = null;
$ftype = 0;
$fid = 0;
$xfer += $input->readStructBegin($fname);
while (true)
{
$xfer += $input->readFieldBegin($fname, $ftype, $fid);
if ($ftype == TType::STOP) {
break;
}
switch ($fid)
{
case 1:
if ($ftype == TType::STRING) {
$xfer += $input->readString($this->from);
} else {
$xfer += $input->skip($ftype);
}
break;
case 2:
if ($ftype == TType::STRING) {
$xfer += $input->readString($this->to);
} else {
$xfer += $input->skip($ftype);
}
break;
case 3:
if ($ftype == TType::STRING) {
$xfer += $input->readString($this->smstext);
} else {
$xfer += $input->skip($ftype);
}
break;
case 4:
if ($ftype == TType::STRING) {
$xfer += $input->readString($this->smsdatetime);
} else {
$xfer += $input->skip($ftype);
}
break;
case 5:
if ($ftype == TType::STRING) {
$xfer += $input->readString($this->smsdirection);
} else {
$xfer += $input->skip($ftype);
}
break;
default:
$xfer += $input->skip($ftype);
break;
}
$xfer += $input->readFieldEnd();
}
$xfer += $input->readStructEnd();
return $xfer;
}
public function write($output) {
$xfer = 0;
$xfer += $output->writeStructBegin('sms');
if ($this->from !== null) {
$xfer += $output->writeFieldBegin('from', TType::STRING, 1);
$xfer += $output->writeString($this->from);
$xfer += $output->writeFieldEnd();
}
if ($this->to !== null) {
$xfer += $output->writeFieldBegin('to', TType::STRING, 2);
$xfer += $output->writeString($this->to);
$xfer += $output->writeFieldEnd();
}
if ($this->smstext !== null) {
$xfer += $output->writeFieldBegin('smstext', TType::STRING, 3);
$xfer += $output->writeString($this->smstext);
$xfer += $output->writeFieldEnd();
}
if ($this->smsdatetime !== null) {
$xfer += $output->writeFieldBegin('smsdatetime', TType::STRING, 4);
$xfer += $output->writeString($this->smsdatetime);
$xfer += $output->writeFieldEnd();
}
if ($this->smsdirection !== null) {
$xfer += $output->writeFieldBegin('smsdirection', TType::STRING, 5);
$xfer += $output->writeString($this->smsdirection);
$xfer += $output->writeFieldEnd();
}
$xfer += $output->writeFieldStop();
$xfer += $output->writeStructEnd();
return $xfer;
}
}
class smsdetails {
static $_TSPEC;
/**
* @var \mobiledata\sms[]
*/
public $smsdata = null;
public function __construct($vals=null) {
if (!isset(self::$_TSPEC)) {
self::$_TSPEC = array(
1 => array(
'var' => 'smsdata',
'type' => TType::LST,
'etype' => TType::STRUCT,
'elem' => array(
'type' => TType::STRUCT,
'class' => '\mobiledata\sms',
),
),
);
}
if (is_array($vals)) {
if (isset($vals['smsdata'])) {
$this->smsdata = $vals['smsdata'];
}
}
}
public function getName() {
return 'smsdetails';
}
public function read($input)
{
$xfer = 0;
$fname = null;
$ftype = 0;
$fid = 0;
$xfer += $input->readStructBegin($fname);
while (true)
{
$xfer += $input->readFieldBegin($fname, $ftype, $fid);
if ($ftype == TType::STOP) {
break;
}
switch ($fid)
{
case 1:
if ($ftype == TType::LST) {
$this->smsdata = array();
$_size0 = 0;
$_etype3 = 0;
$xfer += $input->readListBegin($_etype3, $_size0);
for ($_i4 = 0; $_i4 < $_size0; ++$_i4)
{
$elem5 = null;
$elem5 = new \mobiledata\sms();
$xfer += $elem5->read($input);
$this->smsdata []= $elem5;
}
$xfer += $input->readListEnd();
} else {
$xfer += $input->skip($ftype);
}
break;
default:
$xfer += $input->skip($ftype);
break;
}
$xfer += $input->readFieldEnd();
}
$xfer += $input->readStructEnd();
return $xfer;
}
public function write($output) {
$xfer = 0;
$xfer += $output->writeStructBegin('smsdetails');
if ($this->smsdata !== null) {
if (!is_array($this->smsdata)) {
throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA);
}
$xfer += $output->writeFieldBegin('smsdata', TType::LST, 1);
{
$output->writeListBegin(TType::STRUCT, count($this->smsdata));
{
foreach ($this->smsdata as $iter6)
{
$xfer += $iter6->write($output);
}
}
$output->writeListEnd();
}
$xfer += $output->writeFieldEnd();
}
$xfer += $output->writeFieldStop();
$xfer += $output->writeStructEnd();
return $xfer;
}
}
在客户端我通过制作像
这样的对象来发送数据 $transport->open();
$smsobj = new sms();
$smsobj->from = "9999074909";
$smsobj->to = "9999074909";
$smsobj->smstext = "hello brij";
$smsobj->smsdatetime = "today";
$smsobj->smsdirection = "I";
$smsdetailsobj = new smsdetails();
$smsdetailsobj->smsdata = array($smsobj);
var_dump(array($smsobj));
$client->insertsmsdata($smsdetailsobj);
$transport->close();
运行客户端时出现此错误
PHP Fatal error: Uncaught exception 'Thrift\Exception\TTransportException' with message 'THttpClient: Could not read 1936027957 bytes from localhost:80/phpserver/php-server.php' in thrift/thrift/lib/php/lib/Thrift/Transport/THttpClient.php:162
Stack trace:
看起来我没有让我的php客户端对象正确,这是我应该如何使用php创建一个struct对象列表 - 就像sms类型的对象数组。
答案 0 :(得分:0)
最常见的原因是不兼容的传输/协议堆栈,特别是服务器期望TFramedTransport
和客户端未使用TFramedTransport
。
您必须确保双方使用与线格式完全相同的堆栈。根据经验,从每一侧的相同组件开始。
轻微警告:许多服务器类型(例如,非阻塞)暗示TFramedTransport
在客户端使用,即使未明确添加到服务器协议/传输堆。