2016-09-06 2 views

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