-1

由于 twitter 已经切换到流式传输他的 API,我们必须自己收集数据。我们如何在 php 中使用 GNIP API 来做到这一点?

4

1 回答 1

0

回答这个问题,我只是想确保我做的每件事都是正确的,也许可以TwitterGnipClient在你的帮助下改进我的课程。但如果没有答案,就让它成为常见问题。

主要方法见下图:

/**
 * 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 [];
}

我作为 Gist 分享的所有课程 -在这里

PS如果你看到一个明显的问题 - 请评论它。

于 2016-09-07T07:11:45.110 回答