/** * {@inheritdoc} */ protected function read($length = null) { if (is_null($length)) { return trim(Utils::readLine($this->inputStream)); } return $this->inputStream->read($length); }
public function writeContainerLogs(Container $container) { $stream = $this->restClient->get(sprintf('/containers/%s/logs?stderr=1&stdout=1&follow=1', $container->getId()), ['stream' => true]); while (!$stream->getBody()->eof()) { $line = \GuzzleHttp\Stream\Utils::readLine($stream->getBody()); $line = substr($line, 8); $container->addToLog($line); } }
public function getStatuses($param, $callback) { $response = $this->client->post('statuses/filter.json', ['body' => $param]); $body = $response->getBody(); while (!$body->eof()) { $line = Utils::readLine($body); $data = json_decode($line, true); if (is_null($data)) { continue; } call_user_func($callback, $data); if (ob_get_level() > 0) { ob_flush(); } flush(); } }
public function testTwitterStreamingIntegration() { if (empty($_SERVER['OAUTH_CONSUMER_SECRET'])) { $this->markTestSkipped('No OAUTH_CONSUMER_SECRET provided in phpunit.xml'); return; } $client = new Client(['base_url' => 'https://stream.twitter.com/1.1/', 'defaults' => ['auth' => 'oauth']]); $oauth = new Oauth1(['consumer_key' => $_SERVER['OAUTH_CONSUMER_KEY'], 'consumer_secret' => $_SERVER['OAUTH_CONSUMER_SECRET'], 'token' => $_SERVER['OAUTH_TOKEN'], 'token_secret' => $_SERVER['OAUTH_TOKEN_SECRET']]); $client->getEmitter()->attach($oauth); try { $response = $client->post('statuses/filter.json', ['body' => ['track' => 'bieber'], 'stream' => true]); $body = $response->getBody(); $data = Utils::readLine($body); $this->assertContains('bieber', strtolower($data)); $this->assertNotEmpty(json_decode($data, true)); $body->close(); } catch (ClientException $e) { if ($e->getResponse()->getStatusCode() == 429) { $this->markTestIncomplete('You are being throttled'); } else { throw $e; } } }
/** * This method opens a connection to the Twitter streaming API and applies the given callback function to each Tweet * @param $track * @param $callback */ public function getStream($track, $callback) { $client = $this->createClient(self::TWITTER_STREAM_URL); $response = $client->post(self::STATUSES_ENDPOINT, ['body' => $track]); $body = $response->getBody(); while (!$body->eof()) { //Read a line of the response $line = Utils::readLine($body); if (!empty($line)) { //callback call_user_func($callback, $line); } if (ob_get_level() > 0) { ob_flush(); } flush(); } }