1. Go to this page and download the library: Download baraveli/rss-scraper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
baraveli / rss-scraper example snippets
namespace Baraveli\RssScraper\Util;
use Baraveli\RssScraper\Interfaces\IConfigLoader;
class ConfigLoader implements IConfigLoader
{
/**
* load
*
* @param mixed $filename
*
* This static method loads configuration files from the configs directory
*
* @return array
*/
public static function load(string $filename): array
{
$path = IConfigLoader::DIRECTORY_PATH . $filename . '.json';
if (!file_exists($path)) {
$path = getcwd() . '/'. $filename . '.json';
}
$file = file_get_contents($path, FILE_USE_INCLUDE_PATH);
$urls = json_decode($file, true);
if (!isset($file, $urls)) {
throw new \Exception("Error reading the config file or it it is empty");
}
return $urls;
}
}
namespace Baraveli\RssScraper\Http;
use GuzzleHttp\Client as GuzzleClient;
use Baraveli\RssScraper\Util\Helper;
class Client
{
use Helper;
private $client;
public function __construct()
{
$this->client = new GuzzleClient();
}
/**
* get
*
* Method to get the rss feed.
*
* This method does parsing of xml to php array and validation checks before returning data.
*
* @param mixed $link
*
* @return void
*/
public function get($link)
{
$response = $this->client->request('GET', $link);
$responseBody = $response->getBody();
if (!$this->isValidXml($responseBody)) {
throw new \Exception("The file doesn't contain valid XML");
}
$xmlfile = simplexml_load_string($responseBody);
$data = $this->parseXML($xmlfile);
return $data;
}
/**
* parseXML
*
* This method decode the xml data to php array
*
* @param mixed $xmlfile
*
* @return void
*/
protected function parseXML($xmlfile)
{
$json = json_encode($xmlfile);
$data = json_decode($json, true);
return $data;
}
}
namespace Baraveli\RssScraper\Collections;
use Countable;
class ArticleCollection implements Countable
{
protected $items = [];
/**
* __toString
*
* Jsonify the collection automatically when the trying to output as a string.
*
* @return void
*/
public function __toString()
{
return $this->jsonify();
}
/**
* add
*
* @param mixed $value
*
* Method to add items to the collection array.
*
* @return void
*/
public function add($value)
{
$this->items[] = $value;
}
/**
* get
*
* @param mixed $key
*
* Method to get the items from the collection array given a (int)key value
*
* @return void
*/
public function get($key)
{
return array_key_exists($key, $this->items) ? $this->items[$key] : null;
}
/**
* jsonify
*
* Method to convert the response to json
*
* This method is chainable with the getrss() function.
*
* @return void
*/
public function jsonify()
{
return json_encode($this->items);
}
/**
* toArray
*
* Method to return the response as an array
*
* This method is chainable with the getrss() function.
*
* @return void
*/
public function toArray()
{
return $this->items;
}
/**
* count
*
* Method to count how many items are in the article collection array
*
* @return void
*/
public function count()
{
return count($this->items);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.