/** * Processes the feed and rebuilds an array based on the feeds type (RSS, RDF, Atom). * * @param HttpSocketResponse $response * @param array $query * @param string $source * @return bool */ protected function _process(HttpSocketResponse $response, $query, $source) { if (!$response->isOk()) { return array(); } $feed = Converter::toArray($response->body()); $clean = array(); if (!empty($query['root']) && !empty($feed[$query['feed']['root']])) { $items = $feed[$query['feed']['root']]; } else { // RSS if (isset($feed['channel']) && isset($feed['channel']['item'])) { $items = $feed['channel']['item']; // RDF } else { if (isset($feed['item'])) { $items = $feed['item']; // Atom } else { if (isset($feed['entry'])) { $items = $feed['entry']; // XML } else { $items = $feed; } } } } if (empty($items) || !is_array($items)) { return $clean; } // Gather elements $elements = array('title' => array('title'), 'guid' => array('guid', 'id'), 'date' => array('date', 'pubDate', 'published', 'updated'), 'link' => array('link', 'origLink'), 'image' => array('image', 'thumbnail', 'enclosure'), 'author' => array('author', 'writer', 'editor', 'user'), 'source' => array('source'), 'description' => array('description', 'desc', 'summary', 'content', 'text')); if (is_array($query['fields'])) { $elements = array_merge_recursive($elements, $query['fields']); } // Loop the feed foreach ($items as $item) { $data = array(); foreach ($elements as $element => $keys) { if (isset($keys['attributes'])) { $attributes = $keys['attributes']; unset($keys['attributes']); } else { $attributes = array('value', 'href', 'src', 'name', 'label', 'url'); } if (isset($keys['keys'])) { $keys = $keys['keys']; } foreach ($keys as $key) { if (isset($item[$key]) && empty($data[$element])) { if ($value = $this->_extract($item[$key], $attributes)) { $data[$element] = $value; break; } } } } if (empty($data['link'])) { trigger_error(sprintf('Feed %s does not have a valid link element', $source), E_USER_NOTICE); continue; } if (empty($data['source']) && $source) { $data['source'] = (string) $source; } // Determine how to sort $sortBy = $query['feed']['sort']; if (isset($data[$sortBy])) { $sort = $data[$sortBy]; } else { if (isset($data['date'])) { $sort = $data['date']; } else { $sort = null; } } if ($sortBy === 'date' && $sort) { $sort = strtotime($sort); } else { if (!$sort) { $sort = microtime(); } } if ($data) { $clean[$sort] = $data; } } return $clean; }
/** * Decodes the response based on the content type * * @param \HttpSocketResponse $response * @return array $response * @author Dean Sofer */ public function decode(\HttpSocketResponse $response) { // Get content type header $contentType = explode(';', $response->getHeader('Content-Type')); // Decode response according to content type switch ($contentType[0]) { case 'application/xml': case 'application/atom+xml': case 'application/rss+xml': App::uses('Xml', 'Utility'); $Xml = Xml::build($response->body()); $return = Xml::toArray($Xml); //one of the two lines of code following is unecessary. //Unset will delete the reference and mark the memory for garbage collection. //setting null will clear the memory immediately. //There is some overhead involved in shrinking the stack, so if you are calling this repeatedly it may not be faster. $Xml = null; unset($Xml); break; case 'application/json': case 'application/javascript': case 'text/javascript': $return = json_decode($response->body(), true); break; default: $return = $response->body(); break; } return $return; }