2015-06-21 87 views
1

我想從OMDb API獲取信息。我發現一些代碼,但它不工作的權利:爲什麼getElementsByTagName()不適合我?

$loc = 'http://www.omdbapi.com/?t='.$lookup_title.'&r=xml'; 
$dom = new DOMDocument(); 
$dom->load($loc); 
foreach ($dom->getElementsByTagName('movie') as $e) { 

    $imdb = $e->getElementsByTagName('imdbID')->item(0)->textContent; 
    $year = $e->getElementsByTagName('year')->item(0)->textContent; 
    $plot = $e->getElementsByTagName('plot')->item(0)->textContent; 
    $poster = $e->getElementsByTagName('poster')->item(0)->textContent; 

} 

從OMDb API的XML輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<root response="True"> 
    <movie title="Terminator 2: Judgment Day" 
     year="1991" 
     rated="R" 
     released="03 Jul 1991" 
     runtime="137 min" 
     genre="Action, Sci-Fi" 
     director="James Cameron" 
     writer="James Cameron, William Wisher Jr." 
     actors="Arnold Schwarzenegger, Linda Hamilton, Edward Furlong, Robert Patrick" 
     plot="Almost 10 years have passed since the first cyborg called The Terminator tried to kill Sarah Connor and her unborn son, John Connor. John Connor, the future leader of the human resistance, is now a healthy young boy. However another Terminator is sent back through time called the T-1000, which is more advanced and more powerful than its predecessor. The Mission: to kill John Connor when he's still a child. However, Sarah and John do not have to face this threat of a Terminator alone. Another Terminator is also sent back through time. The mission: to protect John and Sarah Connor at all costs. The battle for tomorrow has begun..." 
     language="English, Spanish" 
     country="USA, France" 
     awards="Won 4 Oscars. Another 20 wins &amp; 21 nominations." 
     poster="http://ia.media-imdb.com/images/M/[email protected]@._V1_SX300.jpg" 
     metascore="68" 
     imdbRating="8.5" 
     imdbVotes="636,472" 
     imdbID="tt0103064" 
     type="movie"/> 
</root> 

錯誤

注意:試圖獲得的非財產對象

回答

3

imdbyearplotposter部分是XML 屬性,不元素,所以使用DOMElement::getAttribute()而不是getElementsByTagName()

$imdb = $e->getAttribute('imdbID'); 
$year = $e->getAttribute('year'); 
$plot = $e->getAttribute('plot'); 
$poster = $e->getAttribute('poster'); 
+0

TY現在的作品! – rackemup420