dq1230123 2014-07-13 23:31
浏览 71
已采纳

使用Twitter Streaming API和ReactPHP的错误请求

I am working on a project that needs realtime data from twitter streaming api.

To do that I am using ReactPHP.

When I make the request to the twitter streamintg API, I am getting BadRequest response.

The request (with the payload) is the following:

POST /1.1/statuses/filter.json HTTP/1.0
Host: stream.twitter.com
User-Agent: React/alpha
Authorization: OAuth oauth_nonce="0f1add32ee06141004ea4c02d892bdaf",oauth_timestamp="1405130866",oauth_consumer_key="72XsNwUvJjw0xaSinIk1mzHL0",oauth_token="366141931-Zxljl6Ycwgdh9a5IYPqImJT5zeat2EJOAJOarTq6",oauth_signature_method="HMAC-SHA1",oauth_signature="Rs57ywc26IRNQA1l9zQZwoRMV5Q%3D"
Content-Type: application/x-www-form-urlencoded
Content-Length: 11

track=arg

and to do that I am using:

"jacobkiers/oauth": "1.0." => for oauth "react/http-client": "" => to connect to the streaming API

you can check the code here:

TwitterConnector.php

thanks

  • 写回答

2条回答 默认 最新

  • dpq39825 2014-07-14 13:14
    关注

    So the answer is really simple, you're making a HTTP 1.0 request while the Twitter API requires a HTTP 1.1 request.

    The source of the problem resides in the Request object. In order to work around that I've quickly created two more classes. RequestData11:

    <?php
    
    use React\HttpClient\RequestData;
    
    class RequestData11 extends RequestData
    {
        public function setProtocolVersion($version)
        {
        }
    }
    

    And Client11:

    <?php
    
    use React\EventLoop\LoopInterface;
    use React\HttpClient\Request;
    use React\SocketClient\ConnectorInterface;
    
    class Client11
    {
        private $connectionManager;
        private $secureConnectionManager;
    
        public function __construct(ConnectorInterface $connector, ConnectorInterface     $secureConnector)
        {
            $this->connector = $connector;
            $this->secureConnector = $secureConnector;
        }
    
        public function request($method, $url, array $headers = array())
        {
            $requestData = new RequestData11($method, $url, $headers);
            $connectionManager = $this->getConnectorForScheme($requestData->getScheme());
            return new Request($connectionManager, $requestData);
    
        }
    
        private function getConnectorForScheme($scheme)
        {
            return ('https' === $scheme) ? $this->secureConnector : $this->connector;
        }
    }
    

    You normally would create a new client like this:

    <?php
    
    $client = $httpClientFactory->create($loop, $dnsResolver);
    

    Instead you'll have to create it this way:

    <?php
    
    $connector = new Connector($loop, $dnsResolver);
    $secureConnector = new SecureConnector($connector, $loop);
    $client = new Client11($connector, $secureConnector);
    

    This forces the client to use HTTP 1.1, beware though, the http-client is a 1.0 client so forcing it to 1.1 should only be done when you know what you're doing.

    Note that you didn't lock the react\http-client version in your composer file so I have no clue what version you're using and the bove code might not work because of that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?