2016-09-06 66 views

回答

0

回答这个问题,我只是想确保我所做的一切都是正确的,也许可以在您的帮助下改进我的TwitterGnipClient课程。 但是,如果没有答案,让它成为FAQ风格的问题。

主要方法见下文:

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

所有I类为主旨共享 - here

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