THttpClient 无法打开流:HTTP 请求失败!HTTP/1.1 400 错误请求 [英] THttpClient failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

查看:54
本文介绍了THttpClient 无法打开流:HTTP 请求失败!HTTP/1.1 400 错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用 Python 编写的代码,用于连接到运行良好的服务器.

This is my code in Python to connect to the server which works very well.

transport = THttpClient.THttpClient(secureUrl)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = EdenRemote.Client(protocol)
transport.open()

print "calling openSession..."
client.openSession(sessionID, 1)

当我尝试像这样用 PHP 编写上述代码时

When I try to write the above code in PHP like this

$socket = new THttpClient($liveURL, 80, '', 'https');
$socket->setTimeoutSecs(50);
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
$client = new \edenremotephp\pub\EdenRemote\EdenRemoteClient($protocol);
$transport->open();
$client->openSession($result->sessionID, 1);

然后我得到以下错误

Array ( [type] => 2 [message] => fopen(https://edenremote.paceap.com/EdenRemote/d11fbf20-ca24-45f0-b176-4a22e28907c5): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request [file] => /home/abbas/www/local.edenremote.com/sdk/Thrift/Transport/THttpClient.php [line] => 207 )

第 207 行代码是

Line 207 code is

public function flush() {
    // God, PHP really has some esoteric ways of doing simple things.
    $host = $this->host_.($this->port_ != 80 ? ':'.$this->port_ : '');

    $headers = array();
    $defaultHeaders = array('Host' => $host,
                            'Accept' => 'application/x-thrift',
                            'User-Agent' => 'PHP/THttpClient',
                            'Content-Type' => 'application/x-thrift',
                            'Content-Length' => TStringFuncFactory::create()->strlen($this->buf_));
    foreach (array_merge($defaultHeaders, $this->headers_) as $key => $value) {
        $headers[] = "$key: $value";
    }

    $options = array('method' => 'POST',
                     'header' => implode("\r\n", $headers),
                     'max_redirects' => 1,
                     'content' => $this->buf_);
    if ($this->timeout_ > 0) {
      $options['timeout'] = $this->timeout_;
    }
    $this->buf_ = '';

    $contextid = stream_context_create(array('http' => $options));
    $this->handle_ = @fopen($this->scheme_.'://'.$host.$this->uri_, 'r', false, $contextid);

    // Connect failed?
    if ($this->handle_ === FALSE) {
      $this->handle_ = null;
      $error = 'THttpClient: Could not connect to '.$host.$this->uri_;
      throw new TTransportException($error, TTransportException::NOT_OPEN);
    }
  }

即使我试图用 ssl 更改 https.对于运输,即使我尝试过

Even I tried to change the https with the ssl. For transport even i tried

$transport = new TFramedTransport($socket, 1024, 1024);

推荐答案

这里是 THttpClient 调用的问题.第一个参数应该是主机,uri 应该在第三个参数中.

Here was the issue with the THttpClient call. The first parameter should be the host and the uri should be in the 3rd parameter.

所以调用它的确切方法是

so the exact way to call it is

$result = json_decode(exec('./Authenticator.py'));
$liveURL = $result->secureUrl;
$liveURI = preg_replace('#^https?://edenremote.paceap.com#', '', rtrim($result->secureUrl,'/'));
$socket = new THttpClient('edenremote.paceap.com', 80, $liveURI, 'https');
$socket->setTimeoutSecs(50);
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
$client = new \edenremotephp\pub\EdenRemote\EdenRemoteClient($protocol);
$transport->open();
$client->openSession($result->sessionID, 1);
$isExist = $client->findUserByEmailAddress($result->sessionID, $account->primaryEmail);
var_dump($isExist);
$client->closeSession($result->sessionID);
echo 'Done';

这篇关于THttpClient 无法打开流:HTTP 请求失败!HTTP/1.1 400 错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆