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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?