本文整理汇总了PHP中Zend\Feed\Reader\Reader::useHttpConditionalGet方法的典型用法代码示例。如果您正苦于以下问题:PHP Reader::useHttpConditionalGet方法的具体用法?PHP Reader::useHttpConditionalGet怎么用?PHP Reader::useHttpConditionalGet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Feed\Reader\Reader
的用法示例。
在下文中一共展示了Reader::useHttpConditionalGet方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: feedRead
/**
* Import a feed by providing a URI
*
* @param string $uri
* The URI to the feed
* @param string $format
* The output format. Possible values xml or json (default).
* @return array
*/
public function feedRead($uri, $format = "json", $fixencoding = true)
{
$zuri = \Zend\Uri\UriFactory::factory($uri);
if (!$zuri->isValid()) {
$this->logger->logWarn("Invalid Uri ({$uri})");
return false;
}
$cache = \Zend\Cache\StorageFactory::factory(array('adapter' => array('name' => 'filesystem', 'options' => array('cache_dir' => __DIR__ . DIRECTORY_SEPARATOR . 'cache', 'ttl' => 3600)), 'plugins' => array(array('name' => 'serializer', 'options' => array()))));
\Zend\Feed\Reader\Reader::setCache($cache);
\Zend\Feed\Reader\Reader::useHttpConditionalGet();
// Change to firefox agent
$httpClient = \Zend\Feed\Reader\Reader::getHttpClient();
$httpClient->setOptions(array('useragent' => 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0', 'timeout' => 25));
\Zend\Feed\Reader\Reader::setHttpClient($httpClient);
// Import feed
try {
$feed = \Zend\Feed\Reader\Reader::import($uri);
$httpClient = \Zend\Feed\Reader\Reader::getHttpClient();
$httpClientResponse = $httpClient->getResponse();
$feedencoding = $feed->getEncoding();
if (!$feedencoding) {
$feedencoding = 'utf-8';
}
$feedResponse = array('feed' => $uri, 'statuscode' => $httpClientResponse->getStatusCode(), 'headers' => $httpClientResponse->getHeaders()->toArray(), 'encoding' => $feedencoding);
} catch (\Exception $e) {
$this->logger->logWarn("Zend feed reader cannot fetch: ({$uri}) because : " . $e->getMessage() . " trying casperjs");
$command = 'casperjs ' . __DIR__ . '/html.js --cookies-file=cookies.txt --url=' . escapeshellarg($uri);
exec($command, $response, $return_var);
$response = $this->strip_json($response);
if ($return_var !== 0 || !isset($response['content']) || empty($response['content'])) {
$this->logger->logWarn("Cannot fetch: ({$uri}) exit code {$return_var}", array('command' => $command, 'response' => $response));
$failed = new \Rss\Feed\Reader\ReadFailures($this->postgresql);
$failed->update_failure($uri, json_encode(array('zend' => $e->getMessage(), 'casper' => $response)));
return false;
}
try {
$feed = \Zend\Feed\Reader\Reader::importString($response['content']);
$feedencoding = $feed->getEncoding();
if (!$feedencoding) {
$feedencoding = 'utf-8';
}
$feedResponse = array('feed' => $uri, 'statuscode' => $response['http']['status'], 'headers' => $response['http'], 'encoding' => $feedencoding);
} catch (\Exception $e) {
$this->logger->logWarn("Cannot parse feed content from ({$uri}) because " . $e->getMessage());
// $failed = new \Rss\Feed\Reader\ReadFailures ( $this->postgresql );
// $failed->update_failure ( $uri );
return false;
}
}
// Fix relative links
$newdata = $this->fix_links($feed, $uri);
// Fix encoding errors
if ($fixencoding) {
$newdata = $this->fix_encoding($newdata);
}
// Return XML
if ($format === "xml") {
$feedResponse['data'] = $newdata;
return $feedResponse;
}
// Reload fixed data
try {
$feed = \Zend\Feed\Reader\Reader::importString($newdata);
} catch (\Exception $e) {
$this->logger->logWarn("Cannot parse corrected feed content from ({$uri}) because " . $e->getMessage());
return false;
}
$data = array('title' => $feed->getTitle(), 'link' => $feed->getLink(), 'date' => $this->get_feed_date($feed), 'description' => $feed->getDescription(), 'language' => $feed->getLanguage(), 'entries' => array());
foreach ($feed as $entry) {
if (is_object($entry)) {
$DateCreated = $entry->getDateCreated();
if (is_object($DateCreated)) {
$DateCreated = $DateCreated->getTimestamp();
} else {
$DateCreated = $data['date'];
}
$DateModified = $entry->getDateModified();
if (is_object($DateModified)) {
$DateModified = $DateModified->getTimestamp();
} else {
$DateModified = $data['date'];
}
if (empty($DateModified)) {
$DateModified = time();
}
if (empty($DateCreated)) {
$DateCreated = time();
}
$description = $entry->getDescription();
$content = $entry->getContent();
if (empty($description)) {
//.........这里部分代码省略.........