开发者

Using XPath with SimpleXML in PHP

开发者 https://www.devze.com 2023-01-26 02:37 出处:网络
$xml = new SimpleXMLElement(getpage(\'http://api.themoviedb.org/2.1/Genres.getList/en/xml/APIKEY\'));
$xml = new SimpleXMLElement(getpage('http://api.themoviedb.org/2.1/Genres.getList/en/xml/APIKEY'));
$genres = $xml->xpath('/opensearchdescription/genres/genre');
foreach($genres as $genre)
{
    echo $genre->id;
}

I'm not sure why it isn't working :( (yes APIKEY is replaced in my code)

The XML is:

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
  <opensearch:Query searchTerms=""/>
  <opensearch:totalResults>30</opensearch:totalResults>
  <genres>
    <translated>true</translated>
    <genre name="Action">
      <id>28</id>

      <url>http://www.themoviedb.org/genre/action</url>
    </genre>
    <genre name="Adventure">
      <id>12</id>
      <url>http://www.themoviedb.org/genre/adventure</url>
    </genre>
    <genre name="Animation">

      <id>16</id>
      <url>http://www.themoviedb.org/genre/animation</url>
    </genre>
    <genre name="Comedy">
      <id>35</id>
      <url>http://www.themoviedb.org/genre/comedy</url>
    </genre>

    <genre name="Crime">
    开发者_如何学编程  <id>80</id>
      <url>http://www.themoviedb.org/genre/crime</url>
    </genre>
    <genre name="Disaster">
      <id>105</id>
      <url>http://www.themoviedb.org/genre/disaster</url>

    </genre>
    <genre name="Documentary">
      <id>99</id>
      <url>http://www.themoviedb.org/genre/documentary</url>
    </genre>
    <genre name="Drama">
      <id>18</id>

      <url>http://www.themoviedb.org/genre/drama</url>
    </genre>
    <genre name="Eastern">
      <id>82</id>
      <url>http://www.themoviedb.org/genre/eastern</url>
    </genre>
    <genre name="Erotic">

      <id>2916</id>
      <url>http://www.themoviedb.org/genre/erotic</url>
    </genre>
    <genre name="Family">
      <id>10751</id>
      <url>http://www.themoviedb.org/genre/family</url>
    </genre>

    <genre name="Fan Film">
      <id>10750</id>
      <url>http://www.themoviedb.org/genre/fan-film</url>
    </genre>
    <genre name="Fantasy">
      <id>14</id>
      <url>http://www.themoviedb.org/genre/fantasy</url>

    </genre>
    <genre name="Film Noir">
      <id>10753</id>
      <url>http://www.themoviedb.org/genre/film-noir</url>
    </genre>
    <genre name="History">
      <id>36</id>

      <url>http://www.themoviedb.org/genre/history</url>
    </genre>
    <genre name="Holiday">
      <id>10595</id>
      <url>http://www.themoviedb.org/genre/holiday</url>
    </genre>
    <genre name="Horror">

      <id>27</id>
      <url>http://www.themoviedb.org/genre/horror</url>
    </genre>
    <genre name="Music">
      <id>10402</id>
      <url>http://www.themoviedb.org/genre/music</url>
    </genre>

    <genre name="Musical">
      <id>22</id>
      <url>http://www.themoviedb.org/genre/musical</url>
    </genre>
    <genre name="Mystery">
      <id>9648</id>
      <url>http://www.themoviedb.org/genre/mystery</url>

    </genre>
    <genre name="Neo-noir">
      <id>10754</id>
      <url>http://www.themoviedb.org/genre/neo-noir</url>
    </genre>
    <genre name="Road Movie">
      <id>1115</id>

      <url>http://www.themoviedb.org/genre/road-movie</url>
    </genre>
    <genre name="Romance">
      <id>10749</id>
      <url>http://www.themoviedb.org/genre/romance</url>
    </genre>
    <genre name="Science Fiction">

      <id>878</id>
      <url>http://www.themoviedb.org/genre/science-fiction</url>
    </genre>
    <genre name="Short">
      <id>10755</id>
      <url>http://www.themoviedb.org/genre/short</url>
    </genre>

    <genre name="Sport">
      <id>9805</id>
      <url>http://www.themoviedb.org/genre/sport</url>
    </genre>
    <genre name="Suspense">
      <id>10748</id>
      <url>http://www.themoviedb.org/genre/suspense</url>

    </genre>
    <genre name="Thriller">
      <id>53</id>
      <url>http://www.themoviedb.org/genre/thriller</url>
    </genre>
    <genre name="War">
      <id>10752</id>

      <url>http://www.themoviedb.org/genre/war</url>
    </genre>
    <genre name="Western">
      <id>37</id>
      <url>http://www.themoviedb.org/genre/western</url>
    </genre>
  </genres>

</OpenSearchDescription>


XML is case sensitive - OpenSearchDescription is not the same as opensearchdescription.

Try this:

$genres = $xml->xpath('/OpenSearchDescription/genres/genre');
0

精彩评论

暂无评论...
验证码 换一张
取 消