質問編集履歴

1

コードブロックの追加、テキスト修正

2021/04/09 06:54

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -14,9 +14,7 @@
14
14
 
15
15
 
16
16
 
17
- ### 該当のソースコード
17
+ ```PHP
18
-
19
-
20
18
 
21
19
  <?php
22
20
 
@@ -42,6 +40,8 @@
42
40
 
43
41
  foreach ( $rss_items as $item ) : ?>
44
42
 
43
+
44
+
45
45
  <a href="<?php echo $item->get_permalink(); ?>">
46
46
 
47
47
  <?php echo $item->get_date('Y.m.d');// 日付 ?><span><?php echo $item->get_title();// タイトル ?></span>
@@ -54,6 +54,560 @@
54
54
 
55
55
  <?php endif; ?>
56
56
 
57
+ ```
58
+
59
+
60
+
61
+
62
+
63
+ Feed.phpのコード内容です。
64
+
65
+
66
+
67
+ ```Feed.php
68
+
69
+ <?php
70
+
71
+
72
+
73
+ /**
74
+
75
+ * RSS for PHP - small and easy-to-use library for consuming an RSS Feed
76
+
77
+ *
78
+
79
+ * @copyright Copyright (c) 2008 David Grudl
80
+
81
+ * @license New BSD License
82
+
83
+ * @version 1.5
84
+
85
+ */
86
+
87
+ class Feed
88
+
89
+ {
90
+
91
+ /** @var int */
92
+
93
+ public static $cacheExpire = '1 day';
94
+
95
+
96
+
97
+ /** @var string */
98
+
99
+ public static $cacheDir;
100
+
101
+
102
+
103
+ /** @var string */
104
+
105
+ public static $userAgent = 'FeedFetcher-Google';
106
+
107
+
108
+
109
+ /** @var SimpleXMLElement */
110
+
111
+ protected $xml;
112
+
113
+
114
+
115
+
116
+
117
+ /**
118
+
119
+ * Loads RSS or Atom feed.
120
+
121
+ * @param string
122
+
123
+ * @param string
124
+
125
+ * @param string
126
+
127
+ * @return Feed
128
+
129
+ * @throws FeedException
130
+
131
+ */
132
+
133
+ public static function load($url, $user = null, $pass = null)
134
+
135
+ {
136
+
137
+ $xml = self::loadXml($url, $user, $pass);
138
+
139
+ if ($xml->channel) {
140
+
141
+ return self::fromRss($xml);
142
+
143
+ } else {
144
+
145
+ return self::fromAtom($xml);
146
+
147
+ }
148
+
149
+ }
150
+
151
+
152
+
153
+
154
+
155
+ /**
156
+
157
+ * Loads RSS feed.
158
+
159
+ * @param string RSS feed URL
160
+
161
+ * @param string optional user name
162
+
163
+ * @param string optional password
164
+
165
+ * @return Feed
166
+
167
+ * @throws FeedException
168
+
169
+ */
170
+
171
+ public static function loadRss($url, $user = null, $pass = null)
172
+
173
+ {
174
+
175
+ return self::fromRss(self::loadXml($url, $user, $pass));
176
+
177
+ }
178
+
179
+
180
+
181
+
182
+
183
+ /**
184
+
185
+ * Loads Atom feed.
186
+
187
+ * @param string Atom feed URL
188
+
189
+ * @param string optional user name
190
+
191
+ * @param string optional password
192
+
193
+ * @return Feed
194
+
195
+ * @throws FeedException
196
+
197
+ */
198
+
199
+ public static function loadAtom($url, $user = null, $pass = null)
200
+
201
+ {
202
+
203
+ return self::fromAtom(self::loadXml($url, $user, $pass));
204
+
205
+ }
206
+
207
+
208
+
209
+
210
+
211
+ private static function fromRss(SimpleXMLElement $xml)
212
+
213
+ {
214
+
215
+ if (!$xml->channel) {
216
+
217
+ throw new FeedException('Invalid feed.');
218
+
219
+ }
220
+
221
+
222
+
223
+ self::adjustNamespaces($xml);
224
+
225
+
226
+
227
+ foreach ($xml->channel->item as $item) {
228
+
229
+ // converts namespaces to dotted tags
230
+
231
+ self::adjustNamespaces($item);
232
+
233
+
234
+
235
+ // generate 'url' & 'timestamp' tags
236
+
237
+ $item->url = (string) $item->link;
238
+
239
+ if (isset($item->{'dc:date'})) {
240
+
241
+ $item->timestamp = strtotime($item->{'dc:date'});
242
+
243
+ } elseif (isset($item->pubDate)) {
244
+
245
+ $item->timestamp = strtotime($item->pubDate);
246
+
247
+ }
248
+
249
+ }
250
+
251
+ $feed = new self;
252
+
253
+ $feed->xml = $xml->channel;
254
+
255
+ return $feed;
256
+
257
+ }
258
+
259
+
260
+
261
+
262
+
263
+ private static function fromAtom(SimpleXMLElement $xml)
264
+
265
+ {
266
+
267
+ if (!in_array('http://www.w3.org/2005/Atom', $xml->getDocNamespaces(), true)
268
+
269
+ && !in_array('http://purl.org/atom/ns#', $xml->getDocNamespaces(), true)
270
+
271
+ ) {
272
+
273
+ throw new FeedException('Invalid feed.');
274
+
275
+ }
276
+
277
+
278
+
279
+ // generate 'url' & 'timestamp' tags
280
+
281
+ foreach ($xml->entry as $entry) {
282
+
283
+ $entry->url = (string) $entry->link['href'];
284
+
285
+ $entry->timestamp = strtotime($entry->updated);
286
+
287
+ }
288
+
289
+ $feed = new self;
290
+
291
+ $feed->xml = $xml;
292
+
293
+ return $feed;
294
+
295
+ }
296
+
297
+
298
+
299
+
300
+
301
+ /**
302
+
303
+ * Returns property value. Do not call directly.
304
+
305
+ * @param string tag name
306
+
307
+ * @return SimpleXMLElement
308
+
309
+ */
310
+
311
+ public function __get($name)
312
+
313
+ {
314
+
315
+ return $this->xml->{$name};
316
+
317
+ }
318
+
319
+
320
+
321
+
322
+
323
+ /**
324
+
325
+ * Sets value of a property. Do not call directly.
326
+
327
+ * @param string property name
328
+
329
+ * @param mixed property value
330
+
331
+ * @return void
332
+
333
+ */
334
+
335
+ public function __set($name, $value)
336
+
337
+ {
338
+
339
+ throw new Exception("Cannot assign to a read-only property '$name'.");
340
+
341
+ }
342
+
343
+
344
+
345
+
346
+
347
+ /**
348
+
349
+ * Converts a SimpleXMLElement into an array.
350
+
351
+ * @param SimpleXMLElement
352
+
353
+ * @return array
354
+
355
+ */
356
+
357
+ public function toArray(SimpleXMLElement $xml = null)
358
+
359
+ {
360
+
361
+ if ($xml === null) {
362
+
363
+ $xml = $this->xml;
364
+
365
+ }
366
+
367
+
368
+
369
+ if (!$xml->children()) {
370
+
371
+ return (string) $xml;
372
+
373
+ }
374
+
375
+
376
+
377
+ $arr = [];
378
+
379
+ foreach ($xml->children() as $tag => $child) {
380
+
381
+ if (count($xml->$tag) === 1) {
382
+
383
+ $arr[$tag] = $this->toArray($child);
384
+
385
+ } else {
386
+
387
+ $arr[$tag][] = $this->toArray($child);
388
+
389
+ }
390
+
391
+ }
392
+
393
+
394
+
395
+ return $arr;
396
+
397
+ }
398
+
399
+
400
+
401
+
402
+
403
+ /**
404
+
405
+ * Load XML from cache or HTTP.
406
+
407
+ * @param string
408
+
409
+ * @param string
410
+
411
+ * @param string
412
+
413
+ * @return SimpleXMLElement
414
+
415
+ * @throws FeedException
416
+
417
+ */
418
+
419
+ private static function loadXml($url, $user, $pass)
420
+
421
+ {
422
+
423
+ $e = self::$cacheExpire;
424
+
425
+ $cacheFile = self::$cacheDir . '/feed.' . md5(serialize(func_get_args())) . '.xml';
426
+
427
+
428
+
429
+ if (self::$cacheDir
430
+
431
+ && (time() - @filemtime($cacheFile) <= (is_string($e) ? strtotime($e) - time() : $e))
432
+
433
+ && $data = @file_get_contents($cacheFile)
434
+
435
+ ) {
436
+
437
+ // ok
438
+
439
+ } elseif ($data = trim(self::httpRequest($url, $user, $pass))) {
440
+
441
+ if (self::$cacheDir) {
442
+
443
+ file_put_contents($cacheFile, $data);
444
+
445
+ }
446
+
447
+ } elseif (self::$cacheDir && $data = @file_get_contents($cacheFile)) {
448
+
449
+ // ok
450
+
451
+ } else {
452
+
453
+ throw new FeedException('Cannot load feed.');
454
+
455
+ }
456
+
457
+
458
+
459
+ return new SimpleXMLElement($data, LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_NOCDATA);
460
+
461
+ }
462
+
463
+
464
+
465
+
466
+
467
+ /**
468
+
469
+ * Process HTTP request.
470
+
471
+ * @param string
472
+
473
+ * @param string
474
+
475
+ * @param string
476
+
477
+ * @return string|false
478
+
479
+ * @throws FeedException
480
+
481
+ */
482
+
483
+ private static function httpRequest($url, $user, $pass)
484
+
485
+ {
486
+
487
+ if (extension_loaded('curl')) {
488
+
489
+ $curl = curl_init();
490
+
491
+ curl_setopt($curl, CURLOPT_URL, $url);
492
+
493
+ if ($user !== null || $pass !== null) {
494
+
495
+ curl_setopt($curl, CURLOPT_USERPWD, "$user:$pass");
496
+
497
+ }
498
+
499
+ curl_setopt($curl, CURLOPT_USERAGENT, self::$userAgent); // some feeds require a user agent
500
+
501
+ curl_setopt($curl, CURLOPT_HEADER, false);
502
+
503
+ curl_setopt($curl, CURLOPT_TIMEOUT, 20);
504
+
505
+ curl_setopt($curl, CURLOPT_ENCODING, '');
506
+
507
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // no echo, just return result
508
+
509
+ if (!ini_get('open_basedir')) {
510
+
511
+ curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // sometime is useful :)
512
+
513
+ }
514
+
515
+ $result = curl_exec($curl);
516
+
517
+ return curl_errno($curl) === 0 && curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200
518
+
519
+ ? $result
520
+
521
+ : false;
522
+
523
+
524
+
525
+ } else {
526
+
527
+ $context = null;
528
+
529
+ if ($user !== null && $pass !== null) {
530
+
531
+ $options = [
532
+
533
+ 'http' => [
534
+
535
+ 'method' => 'GET',
536
+
537
+ 'header' => 'Authorization: Basic ' . base64_encode($user . ':' . $pass) . "\r\n",
538
+
539
+ ],
540
+
541
+ ];
542
+
543
+ $context = stream_context_create($options);
544
+
545
+ }
546
+
547
+
548
+
549
+ return file_get_contents($url, false, $context);
550
+
551
+ }
552
+
553
+ }
554
+
555
+
556
+
557
+
558
+
559
+ /**
560
+
561
+ * Generates better accessible namespaced tags.
562
+
563
+ * @param SimpleXMLElement
564
+
565
+ * @return void
566
+
567
+ */
568
+
569
+ private static function adjustNamespaces($el)
570
+
571
+ {
572
+
573
+ foreach ($el->getNamespaces(true) as $prefix => $ns) {
574
+
575
+ $children = $el->children($ns);
576
+
577
+ foreach ($children as $tag => $content) {
578
+
579
+ $el->{$prefix . ':' . $tag} = $content;
580
+
581
+ }
582
+
583
+ }
584
+
585
+ }
586
+
587
+ }
588
+
589
+
590
+
591
+
592
+
593
+
594
+
595
+ /**
596
+
597
+ * An exception generated by Feed.
598
+
599
+ */
600
+
601
+ class FeedException extends Exception
602
+
603
+ {
604
+
605
+ }
606
+
607
+
608
+
609
+ ```
610
+
57
611
 
58
612
 
59
613