PHPのSimpleXMLElementの簡単なサンプル
- Sun
- 00:10
- PHP
PHPのSimpleXMLElementの簡単なサンプル TechCrunchのfeedをサンプルに。
xmlのコードは、http://jp.techcrunch.com/feed/のソースをご参照。
コロン付きのタグや、タグ内のアトリビュートの取得なんかもやってます。
xml.php
実行結果:
xmlのコードは、http://jp.techcrunch.com/feed/のソースをご参照。
コロン付きのタグや、タグ内のアトリビュートの取得なんかもやってます。
xml.php
01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: | #!/usr/local/bin/php <? $ch = curl_init("http://jp.techcrunch.com/feed/"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($ch); curl_close($ch); //print_r($res); $xml_obj = new SimpleXMLElement($res); print "*** title=" . $xml_obj->channel->title . "\n"; print "*** url=" . $xml_obj->channel->link . "\n"; $buf = $xml_obj->channel->children("sy", 1)->updatePeriod; print "*** sy:updatePeriod=" . $buf . "\n"; print "*** item count=" . $xml_obj->channel->item->count() . "\n"; $idx = 0; foreach ($xml_obj->channel->item as $item) { print "*** item $idx\n"; print " title=" . $item->title . "\n"; print " link=" . $item->link . "\n"; $buf = $item->children("media", 1)->thumbnail->attributes(); if ($item->children("media", 1)->thumbnail) { print " media:thumbnail url=[" . $buf . "]\n"; } $idx++; } |
$ ./xml.php *** title=TechCrunch Japan *** url=http://jp.techcrunch.com *** sy:updatePeriod=hourly *** item count=10 *** item 0 title=LGのMacBook用5KディスプレイはWi-Fiルーターに近いと狂う、今後は電磁波シールドを装備へ link=http://jp.techcrunch.com/2017/02/04/20170203lg-monitor-router-2/ media:thumbnail url=[https://techcrunchjp.files.wordpress.com/2017/02/s-aolcdn1.jpg?w=210&h=158&crop=1] *** item 1 title=Appleの学生・教育者向けアプリケーション・バンドルはFinal CutとLogic込みで199ドル link=http://jp.techcrunch.com/2017/02/04/20170203apple-offers-a-199-app-bundle-for-education-with-final-cut-and-logic/ media:thumbnail url=[https://techcrunchjp.files.wordpress.com/2017/02/macbook-final-cut-pro.jpg?w=210&h=158&crop=1] *** item 2 title=Googleが衛星画像事業Terra BellaをPlanet Labsに売却、Earthの画像はライセンスにより継続 link=http://jp.techcrunch.com/2017/02/04/20170203google-selling-terra-bella-satellite-imaging-business-to-planet/ media:thumbnail url=[https://techcrunchjp.files.wordpress.com/2017/02/sf-20161227-crop-1500.jpg?w=210&h=158&crop=1] *** item 3 title=新築のワールド・トレード・センターのモールにFordが交通の未来をテーマに展示スペースをオープン link=http://jp.techcrunch.com/2017/02/04/20170203fordhub/ *** item 4 title=Uber CEOのTravis Kalanickが経済諮問委員を辞任 link=http://jp.techcrunch.com/2017/02/04/20170202uber-ceo-travis-kalanick-quits-donald-trumps-business-advisory-council/ media:thumbnail url=[https://techcrunchjp.files.wordpress.com/2017/02/kalanick14.jpg?w=210&h=158&crop=1] *** item 5 title=シリコンバレーを有効活用するためのサテライトオフィスという選択 link=http://jp.techcrunch.com/2017/02/04/20170202how-other-cities-can-benefit-from-silicon-valley/ media:thumbnail url=[https://techcrunchjp.files.wordpress.com/2017/02/8668050852_e42ba2efb2_o.jpg?w=210&h=158&crop=1] *** item 6 title=Google、低品質サイトの評価を下げるアップデートーーキュレーションメディアなどが対象に link=http://jp.techcrunch.com/2017/02/03/google-seo-1/ media:thumbnail url=[https://techcrunchjp.files.wordpress.com/2017/02/google-seo.png?w=210&h=158&crop=1] *** item 7 title=ユーザーの好みを学習するパートナーロボット「ユニボ」の開発元が総額3.24億円を調達 link=http://jp.techcrunch.com/2017/02/03/unibo-raises-324-million-yen/ media:thumbnail url=[https://techcrunchjp.files.wordpress.com/2017/02/unibo.png?w=210&h=158&crop=1] *** item 8 title=あくまでもパブリッシャープラットホームにこだわり続けるEv Williams、新装Mediumで有料購読制に挑戦 link=http://jp.techcrunch.com/2017/02/03/20170202medium-subscriptions/ media:thumbnail url=[https://techcrunchjp.files.wordpress.com/2017/02/evan-williams.jpg?w=210&h=158&crop=1] *** item 9 title=スマホを物の上にかざすとスペクトル分析でその成分(毒物の有無など)を当てるアプリがもうすぐ完成 link=http://jp.techcrunch.com/2017/02/03/20170202this-app-uses-spectral-analysis-to-analyze-objects-and-their-makeup/ media:thumbnail url=[https://techcrunchjp.files.wordpress.com/2017/02/fraun_hawkspex.jpg?w=210&h=158&crop=1]