You are here

public function ShareaholicCurlMultiShareCount::get_counts in Share Buttons, Related Posts, Content Analytics - Shareaholic 7.3

Same name and namespace in other branches
  1. 8 lib/social-share-counts/curl_multi_share_count.php \ShareaholicCurlMultiShareCount::get_counts()

This function should get all the counts for the supported services

It should return an associative array with the services as the keys and the counts as the value.

Example: array('facebook' => 12, 'pinterest' => 0, 'twitter' => 14, ...);

Return value

Array an associative array of service => counts

Overrides ShareaholicShareCount::get_counts

File

lib/social-share-counts/curl_multi_share_count.php, line 33

Class

ShareaholicCurlMultiShareCount
A class that implements ShareaholicShareCounts This class will get the share counts by calling the social services via curl_multi

Code

public function get_counts() {
  $services_length = count($this->services);
  $config = self::get_services_config();
  $response = array();
  $response['status'] = 200;

  // array of curl handles
  $curl_handles = array();

  // multi handle
  $multi_handle = curl_multi_init();
  for ($i = 0; $i < $services_length; $i++) {
    $service = $this->services[$i];
    if (!isset($config[$service])) {
      continue;
    }
    if (isset($config[$service]['prepare'])) {
      $this
        ->{$config[$service]['prepare']}($this->url, $config);
    }

    // Create the curl handle
    $curl_handles[$service] = curl_init();

    // set the curl options to make the request
    $this
      ->curl_setopts($curl_handles[$service], $config, $service);

    // add the handle to curl_multi_handle
    curl_multi_add_handle($multi_handle, $curl_handles[$service]);
  }

  // Run curl_multi only if there are some actual curl handles
  if (count($curl_handles) > 0) {

    // While we're still active, execute curl
    $running = NULL;
    do {
      $mrc = curl_multi_exec($multi_handle, $running);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    while ($running && $mrc == CURLM_OK) {

      // Wait for activity on any curl-connection
      if (curl_multi_select($multi_handle) == -1) {
        usleep(1);
      }

      // Continue to exec until curl is ready to
      // give us more data
      do {
        $mrc = curl_multi_exec($multi_handle, $running);
      } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }

    // handle the responses
    foreach ($curl_handles as $service => $handle) {
      if (curl_errno($handle)) {
        $response['status'] = 500;
      }
      $result = array(
        'body' => curl_multi_getcontent($handle),
        'response' => array(
          'code' => curl_getinfo($handle, CURLINFO_HTTP_CODE),
        ),
      );
      $callback = $config[$service]['callback'];
      $counts = $this
        ->{$callback}($result);
      if (is_numeric($counts)) {
        $response['data'][$service] = $counts;
      }
      $this->raw_response[$service] = $result;
      curl_multi_remove_handle($multi_handle, $handle);
      curl_close($handle);
    }
    curl_multi_close($multi_handle);
  }
  return $response;
}