-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
71 lines (62 loc) · 2.09 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* Atom package example.
*
* @author Михаил Красильников <[email protected]>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Mekras\Atom\Example;
use Mekras\Atom\Document\EntryDocument;
use Mekras\Atom\Document\FeedDocument;
use Mekras\Atom\DocumentFactory;
use Mekras\Atom\Exception\AtomException;
require __DIR__ . '/../vendor/autoload.php';
if (PHP_SAPI !== 'cli') {
die("This script should be executed from console!\n");
}
if (2 !== $argc) {
die('Usage: php example.php <URL of Atom feed or entry>' . PHP_EOL);
}
$url = $argv[1];
if (!filter_var($url, FILTER_VALIDATE_URL)) {
die(sprintf('It seems that "%s" is not a valid URL', $url) . PHP_EOL);
}
@$xml = file_get_contents($url);
if (!$xml) {
die(sprintf('Failed to read document from "%s"', $url) . PHP_EOL);
}
$factory = new DocumentFactory();
try {
$document = $factory->parseXML($xml);
} catch (AtomException $e) {
die($e->getMessage() . PHP_EOL);
}
if ($document instanceof FeedDocument) {
$feed = $document->getFeed();
echo 'Feed: ', $feed->getTitle(), PHP_EOL;
echo 'Updated: ', $feed->getUpdated()->format('d.m.Y H:i:s'), PHP_EOL;
foreach ($feed->getAuthors() as $author) {
echo 'Author: ', $author->getName(), PHP_EOL;
}
foreach ($feed->getEntries() as $entry) {
echo PHP_EOL;
echo ' Entry: ', $entry->getTitle(), PHP_EOL;
if ($entry->getSelfLink()) {
echo ' URL: ', $entry->getSelfLink(), PHP_EOL;
} else {
echo PHP_EOL, (string) $entry->getContent(), PHP_EOL;
}
}
} elseif ($document instanceof EntryDocument) {
$entry = $document->getEntry();
echo 'Entry: ', $entry->getTitle(), PHP_EOL;
echo 'Updated: ', $entry->getUpdated()->format('d.m.Y H:i:s'), PHP_EOL;
foreach ($entry->getAuthors() as $author) {
echo 'Author: ', $author->getName(), PHP_EOL;
}
if ($entry->getContent()->getSrc()) {
echo 'Content URL: ', $entry->getContent()->getSrc() , PHP_EOL;
} else {
echo PHP_EOL, (string) $entry->getContent(), PHP_EOL;
}
}