You are here

public function PuSHSubscriber::subscribe in Feeds 7.2

Same name and namespace in other branches
  1. 6 libraries/PuSHSubscriber.inc \PuSHSubscriber::subscribe()
  2. 7 libraries/PuSHSubscriber.inc \PuSHSubscriber::subscribe()

Subscribe to a given URL. Attempt to retrieve 'hub' and 'self' links from document at $url and issue a subscription request to the hub.

Parameters

$url: The URL of the feed to subscribe to.

$callback_url: The full URL that hub should invoke for subscription verification or for notifications.

$hub: The URL of a hub. If given overrides the hub URL found in the document at $url.

File

libraries/PuSHSubscriber.inc, line 70
Pubsubhubbub subscriber library.

Class

PuSHSubscriber
PubSubHubbub subscriber.

Code

public function subscribe($url, $callback_url, $hub = '') {

  // Fetch document, find rel=hub and rel=self.
  // If present, issue subscription request.
  $request = curl_init($url);
  curl_setopt($request, CURLOPT_FOLLOWLOCATION, TRUE);
  curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
  $data = curl_exec($request);
  if (curl_getinfo($request, CURLINFO_HTTP_CODE) == 200) {
    try {
      $xml = @new SimpleXMLElement($data);
      $xml
        ->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');
      if (empty($hub) && ($hub = @current($xml
        ->xpath("//atom:link[attribute::rel='hub']")))) {
        $hub = (string) $hub
          ->attributes()->href;
      }
      if ($self = @current($xml
        ->xpath("//atom:link[attribute::rel='self']"))) {
        $self = (string) $self
          ->attributes()->href;
      }
    } catch (Exception $e) {

      // Do nothing.
    }
  }
  curl_close($request);

  // Fall back to $url if $self is not given.
  if (!$self) {
    $self = $url;
  }
  if (!empty($hub) && !empty($self)) {
    $this
      ->request($hub, $self, 'subscribe', $callback_url);
  }
}