先日、こちらで、ttt.txt に記載したタブ区切りの指定したデータを拾ってくるサンプルソースを作っていただきました。
ttt.txt の中身 「 abc def ghi jkl mno 」
test.php?p=2&e=3 とすると、◇マークと行番号を付与した上で、2行目から3つのデータを拾ってきて、
◇2 def
◇3 ghi
◇4 jkl
と表示します。
php
1 2 3<?php 4 5use SplFileObject as File; 6 7// パラメータ受け取り 8$p = max(filter_input(INPUT_GET, 'p') - 1, 0); // 0以上を保証 9$e = filter_input(INPUT_GET, 'e'); 10 11// 実験データ 12$filename = ("ttt.txt"); 13 14// SplFileObjectを使ったほうがコードがきれいになります 15$file = new File($filename, 'rb'); 16$file->setFlags(File::READ_CSV | File::SKIP_EMPTY | File::READ_AHEAD | File::DROP_NEW_LINE); // CSVモード 17$file->setCsvControl("\t"); // セパレータをタブ文字に 18 19// イテレータを配列に変換して切り取り 20$records = array_slice(iterator_to_array($file), $p, $e, true); 21 22?> 23<!DOCTYPE html> 24 25<meta charset="UTF-8"> 26<title>Example</title> 27 28<style> 29ul { 30 list-style: none; 31 margin-left: 0; 32 padding-left: 1.2em; 33 text-indent: -1.2em; 34} 35li:before { 36 content: "◇"; 37 display: block; 38 float: left; 39 width: 1.2em; 40} 41</style> 42 43<ul> 44<?php foreach ($records as $i => $record): ?> 45 <li><?=($i+1)."\t".implode("\t", array_map('htmlspecialchars', $record))?></li> 46<?php endforeach; ?> 47</ul> 48
今回の変更点
$filename = ("ttt.txt");
の部分について
$filename = ("http://example.com/ttt.txt");
とURLで拾ってきたいというのが質問の趣旨です。
【当方でテストしてみたこと】
まずは、php.ini の「Fopen wrappers」が「allow_url_fopen = On」になっていることを確認しました。
その上でttt.txt に記載したタブ区切りの内容( abc def ghi jkl mno ) をURLから拾って全て書き出すPHPを作りました。
php
1<?php 2$inputs = new NoRewindIterator(new SplFileObject("http://example.com/ttt.txt")) 3foreach($inputs as $line) { 4 print $line; 5} 6?> 7
上記のPHPは、http://example.com/ttt.txt のタブ区切り内容を全て拾うことができます。
上記の2つを合体させれば問題ないと考え、下記のように作成してみました。
php
1<?php 2 3$inputs = new NoRewindIterator(new SplFileObject("http://example.com/ttt.txt")); 4 5use SplFileObject as File; 6 7// パラメータ受け取り 8$p = max(filter_input(INPUT_GET, 'p') - 1, 0); // 0以上を保証 9$e = filter_input(INPUT_GET, 'e'); 10 11// 実験データ 12$filename = $inputs; 13 14// SplFileObjectを使ったほうがコードがきれいになります 15$file = new File($filename, 'rb'); 16$file->setFlags(File::READ_CSV | File::SKIP_EMPTY | File::READ_AHEAD | File::DROP_NEW_LINE); // CSVモード 17$file->setCsvControl("\t"); // セパレータをタブ文字に 18 19// イテレータを配列に変換して切り取り 20$records = array_slice(iterator_to_array($file), $p, $e, true); 21 22?> 23<!DOCTYPE html> 24 25<meta charset="UTF-8"> 26<title>Example</title> 27 28<style> 29ul { 30 list-style: none; 31 margin-left: 0; 32 padding-left: 1.2em; 33 text-indent: -1.2em; 34} 35li:before { 36 content: "◇"; 37 display: block; 38 float: left; 39 width: 1.2em; 40} 41</style> 42 43<ul> 44<?php foreach ($records as $i => $record): ?> 45 <li><?=($i+1)."\t".implode("\t", array_map('htmlspecialchars', $record))?></li> 46<?php endforeach; ?> 47</ul> 48 49
結果
下記のようなエラーが出ます。
Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct() expects parameter 1 to be a valid path, object given' in C:\xampp\htdocs\test.php:15 Stack trace: #0 C:\xampp\htdocs\test.php(15): SplFileObject->__construct(Object(NoRewindIterator), 'rb') #1 {main} thrown in C:\xampp\htdocs\test.php on line 15
上記でエラーとなったいる場所は
$file = new File($filename, 'rb');
の部分です。
どこが間違っているのでしょうか?
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/26 20:35
2017/05/27 06:26 編集
2017/05/27 07:34