Da Twitter auf Streaming seiner API umgeschaltet hat, müssen wir die Daten selbst sammeln. Wie können wir es mit GNIP API in PHP tun?So sammeln Sie Tweets/Retweets mit GNIP API in PHP
-1
A
Antwort
0
Als Antwort darauf wollte ich nur sicherstellen, dass ich alles richtig gemacht habe und vielleicht meine Klasse TwitterGnipClient
mit Ihrer Hilfe verbessern könnte. Aber wenn es keine Antworten gibt, lass es FAQ Stil Frage sein.
Haupt Methoden siehe unten:
/**
* Add rules to PowerTrack stream’s ruleset.
* @example ['id' => 'url', ...]
* @param array $data
*/
public function addRules(array $data)
{
$rules = [];
foreach ($data as $id => $url) {
$rules[] = $this->buildRuleForUrl($url, $id);
}
$this->httpClient->post($this->getRulesUrl(), [
'auth' => [$this->getUser(), $this->getPassword()],
'json' => ['rules' => $rules]
]);
}
/**
* Retrieves all existing rules for a stream.
* @return \Generator
*/
public function getRules()
{
$response = $this->httpClient->get($this->getRulesUrl(), [
'auth' => [$this->getUser(), $this->getPassword()]
]);
$batchStr = '';
$body = $response->getBody();
while (!$body->eof()) {
$batchStr .= $body->read(1024);
}
$batchArray = explode(PHP_EOL, $batchStr);
unset($batchStr);
foreach ($batchArray as $itemJson) {
yield $this->unpackJson($itemJson);
}
}
/**
* Removes the specified rules from the stream.
* @param $data
*/
public function deleteRules($data)
{
$rules = [];
foreach ($data as $id => $url) {
$rules[] = $this->buildRuleForUrl($url, $id);
}
$this->httpClient->delete($this->getRulesUrl(), [
'auth' => [$this->getUser(), $this->getPassword()],
'json' => ['rules' => array_values($rules)]
]);
}
/**
* Open stream through which the social data will be delivered.
* @return \Generator
*/
public function listenStream()
{
$response = $this->httpClient->get($this->getStreamUrl(), [
'auth' => [$this->getUser(), $this->getPassword()],
'stream' => true
]);
$batchStr = '';
$body = $response->getBody();
while (!$body->eof()) {
$batchStr .= $body->read(1024);
$batchArray = explode(PHP_EOL, $batchStr);
// leave the last piece of response as it can be incomplete
$batchStr = array_pop($batchArray);
foreach ($batchArray as $itemJson) {
yield $this->processBroadcastItem($this->unpackJson($itemJson));
}
}
$body->close();
}
/**
* Process broadcast item data
* @param $data
* @return array
*/
protected function processBroadcastItem($data)
{
if (is_array($data)) {
$url = str_replace('url_contains:', '', $data['gnip']['matching_rules'][0]['value']);
switch ($data['verb']) {
// Occurs when a user posts a new Tweet.
case 'post':
return $this->getMappedResponse($url, 'tweet', 1);
break;
// Occurs when a user Retweets another user's Tweet
case 'share':
return $this->getMappedResponse($url, 'retweet', $data['retweetCount']);
break;
}
}
return [];
}
Alle Klasse I als Gist geteilt - here
P. S. Wenn Sie ein offensichtliches Problem sehen - kommentieren Sie es bitte.
Verwandte Themen
- 1. So sammeln Sie Tweets schneller mit Twitter API in Python?
- 2. So sammeln Sie GCLogs
- 3. So machen Sie PHP Kontaktformular sammeln Sie alle ausgewählten Kontrollkästchen
- 4. Sammeln und Verarbeiten von Daten mit PHP (Twitter Streaming API)
- 5. So sammeln Sie Benutzermeinungen über Software
- 6. So sammeln Sie Daten aus einem Bild mit Python
- 7. Stream-API Summe & sammeln in eine Karte
- 8. So erstellen Sie ein Thema auf shopify mit PHP API
- 9. So twittern Sie in Twitter mit PHP
- 10. Sammeln von API-Returns in einem Array
- 11. Scala: Sammeln Sie mit Generika
- 12. So sammeln Sie Javadoc-Gläser in eine Zip-Datei
- 13. Sammeln Einzigartige Tweets REST API
- 14. Sammeln Produktvariable und finden Sie die größte in PHP
- 15. So rufen Sie SOAP-API über SOAP-Client in PHP
- 16. So erstellen Sie Rest-API mit Abfrageparametern
- 17. So verwenden Sie Locken mit API-Schlüssel und API Geheimlizenzschlüssel
- 18. Wie alle Tweets in Englisch mit twitter API sammeln?
- 19. So validieren Sie SPF-Datensätze mit PHP
- 20. So aktualisieren Sie Werte mit PHP Curl
- 21. So lesen Sie JavaScript-Cookie mit PHP
- 22. So rufen Sie PHP-Datei mit Javascript
- 23. Google Drive PHP API - So streamen Sie eine große Datei
- 24. So validieren Sie PHP in VS-Code mit WSL-PHP
- 25. So sammeln Sie Informationen aus nicht identischen Klassen. (Selen, Python)
- 26. Sammeln Sie MySQL-Daten mit mehreren Variablen
- 27. So testen Sie Redux-API mit Knoten
- 28. So rufen Sie Soap API mit Python
- 29. So verwenden Sie Houndify API mit Curl
- 30. Sammeln Sie alle Blattknoten mit Postgres ltree