feed ] if successful, [ 'error' => message] if not */ public static function parseFeed(string $content): array { try { return [ 'ok' => new SimpleXMLElement($content) ]; } catch (Exception $ex) { return [ 'error' => $ex->getMessage() ]; } } /** * Retrieve the feed * @param string $url * @return array|SimpleXMLElement[]|string[] [ 'ok' => feedXml, 'url' => actualUrl ] if successful, [ 'error' => message ] if not */ public static function retrieveFeed(string $url): array { $feedReq = curl_init($url); curl_setopt($feedReq, CURLOPT_FOLLOWLOCATION, true); curl_setopt($feedReq, CURLOPT_RETURNTRANSFER, true); curl_setopt($feedReq, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($feedReq, CURLOPT_TIMEOUT, 15); $feedContent = curl_exec($feedReq); $result = array(); $error = curl_error($feedReq); $code = curl_getinfo($feedReq, CURLINFO_RESPONSE_CODE); if ($error) { $result['error'] = $error; } else if ($code == 200) { $parsed = self::parseFeed($feedContent); if (array_key_exists('error', $parsed)) { $result['error'] = $parsed['error']; } else { $result['ok'] = $parsed['ok']; $result['url'] = curl_getinfo($feedReq, CURLINFO_EFFECTIVE_URL); } } else { $result['error'] = "HTTP Code $code: $feedContent"; } curl_close($feedReq); return $result; } /** * Add an RSS feed * @param string $url The URL of the RSS feed to add * @return array [ 'ok' => true ] if successful, [ 'error' => message ] if not */ public static function add(string $url): array { $feed = self::retrieveFeed($url); if (array_key_exists('error', $feed)) return $feed; $channel = $feed['ok']->channel; $feedId = Data::addFeed($feed['url'], (string) $channel->title, (string) $channel->lastBuildDate); return [ 'ok' => true ]; } }