0

これが私の問題です。XML URL(last.fm apiから)から値を取得しようとしています。たとえば、芸術家の伝記。

私はすでにこのようにそれを行うことができることを知っています(例えばアデルの場合):

<?php           
    $xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=adele&api_key=b25b959554ed76058ac220b7b2e0a026");      
    $info = $xml->artist->bio->summary;
    echo $info;  
?>

これは次を返します:

Adele Laurie Blue Adkins, (born 5 May 1988), is a Grammy Award-Winning English <a href="http://www.last.fm/tag/singer-songwriter" class="bbcode_tag" rel="tag">singer-songwriter</a> from Enfield, North London. Her debut album, <a title="Adele - 19" href="http://www.last.fm/music/Adele/19" class="bbcode_album">19</a>, was released in January 2008 and entered the UK album chart at #1. The album has since received four-times Platinum certification in the UK and has sold 5,500,000 copies worldwide. The album included the hugely popular song <a title="Adele &ndash; Chasing Pavements" href="http://www.last.fm/music/Adele/_/Chasing+Pavements" class="bbcode_track">Chasing Pavements</a>. 19 earned Adele two Grammy Awards in February 2009 for Best New Artist and Best Female Pop Vocal Performance.

その結果をテキストファイルに入れて、ウェブサイトのフォルダ(たとえば、「http://mysite.com/artistdescriptions/adele.txt」)に保存します。

私はすでに試しました:

<?php
    $file_path = $_SERVER['DOCUMENT_ROOT'].'/artistdescriptions/adele.txt';

    $xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=adele&api_key=b25b959554ed76058ac220b7b2e0a026");      
    $info = $xml->artist->bio->summary;  
    file_put_contents($file_path, serialize($info));


    $file_contents = file_get_contents($file_path);
    if(!empty($file_contents)) {
       $data = unserialize($file_contents);
    echo $data;
    }
?>

しかし、残念ながらこれは機能しませんでした。どんな助けでも大歓迎です!

4

1 に答える 1

0

魔法のメソッド __toString() をエコー コールしますが、シリアル化は行われず、SimpleXMLElement ではシリアル化が許可されていないため、例外がスローされます。しかし、__toString() マジック メソッドを自分で呼び出すことができ、これで問題は解決します。

あなたのラインを私のものに置き換えてください

$info = $xml->artist->bio->summary->__toString();  
于 2012-07-10T11:16:59.467 に答える