You are here

protected function SocialGoogleFormatter::getActivityId in Open Social 7

Get activity ID from URL.

Parameters

string $url: Google activity URL.

Return value

string Activity ID.

1 call to SocialGoogleFormatter::getActivityId()
SocialGoogleFormatter::getData in includes/social_comments.google.inc

File

includes/social_comments.google.inc, line 39
Google class

Class

SocialGoogleFormatter
@file Google class

Code

protected function getActivityId($url) {

  // Get URL path.
  $url = parse_url($url, PHP_URL_PATH);

  // Explode for arguments.
  $args = explode('/', $url);
  $user_id = isset($args[1]) ? $args[1] : NULL;
  $post_key = isset($args[3]) ? $args[3] : NULL;
  $cache_key = 'social_comments:google:' . $post_key;
  $id = FALSE;
  if ($cache = cache_get($cache_key)) {
    $id = $cache->data;
  }
  else {
    $response_url = url('https://www.googleapis.com/plus/v1/people/' . $user_id . '/activities/public', array(
      'query' => array(
        'key' => $this->api_key,
      ),
    ));
    $data = drupal_http_request($response_url);
    if ($data->code != 200) {
      watchdog('social_comments', $data->error, array(), WATCHDOG_WARNING);
      return FALSE;
    }
    $result = drupal_json_decode($data->data);
    if (!empty($result['items'])) {
      foreach ($result['items'] as $item) {
        if (strpos($item['url'], $post_key) && strpos($item['url'], $user_id)) {
          $id = $item['id'];

          // Set data to cache.
          cache_set($cache_key, $id, 'cache', $this->expire + REQUEST_TIME);
          break;
        }
      }
    }
  }
  return $id;
}