PHP 7 Stream Parser is a package by Sergio Ródenas for PHP multi-format stream parsing. Using streams with a pull parser is more efficient in large documents than DOM loading.
Here’s an example of how you might use the parser with a Laravel queue:
1StreamParser::xml("https://example.com/users.xml")->each(function(Collection $user){2 dispatch(new App\Jobs\SendEmail($user));3});
Given the following XML data example, it becomes clear how this stream parser works with Collections:
1<bookstore> 2 <book ISBN="10-000000-001"> 3 <title>The Iliad and The Odyssey</title> 4 <price>12.95</price> 5 <comments> 6 <userComment rating="4"> 7 Best translation I've read. 8 </userComment> 9 <userComment rating="2">10 I like other versions better.11 </userComment>12 </comments>13 </book>14 [...]15</bookstore>
And an example of the PHP code and output:
1StreamParser::xml("https://example.com/books.xml")->each(function(Collection $book){ 2 var_dump($book); 3 var_dump($book->get('comments')->toArray()); 4}); 5 6// Output 7class Illuminate\Support\Collection#19 (1) { 8 protected $items => 9 array(4) {10 'ISBN' =>11 string(13) "10-000000-001"12 'title' =>13 string(25) "The Iliad and The Odyssey"14 'price' =>15 string(5) "12.95"16 'comments' =>17 class Illuminate\Support\Collection#17 (1) {18 protected $items =>19 array(2) {20 ...21 }22 }23 }24}25array(2) {26 [0] =>27 array(2) {28 'rating' =>29 string(1) "4"30 'userComment' =>31 string(27) "Best translation I've read."32 }33 [1] =>34 array(2) {35 'rating' =>36 string(1) "2"37 'userComment' =>38 string(29) "I like other versions better."39 }40}
Here’s the same example using CSV data:
1title,price,comments2The Iliad and The Odyssey,12.95,"Best translation I've read.,I like other versions better."3Anthology of World Literature,24.95,"Needs more modern literature.,Excellent overview of world literature."
And an example of the stream parser code:
1StreamParser::csv("https://example.com/books.csv")->each(function(Collection $book){2 var_dump($book->get('comments')->last());3});45// Output6string(29) "I like other versions better."7string(39) "Excellent overview of world literature."
Learn More
You can learn more about this package on GitHub and install it with composer require Rodenastyle/stream-parser
. The README also mentions this article, which explains processing XML files via DOM vs. Streaming.
Filed in:
Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.