在条件检查中从xml中获取值

时间:2013-05-05 03:34:20

标签: php xml

我遇到了问题

<?php

$movie_name = 'Dabangg';

$url = sprintf(
    'http://api.themoviedb.org/2.1/Movie.search/en/xml/mapi/%s',
    rawurlencode(trim($movie_name))
);

$xml = simplexml_load_file($url);

if (!$xml) 
{
    throw new RuntimeException(sprintf("Failed to open XML from URL '%s'.", $url));
}

$movies = $xml->movies->movie;

foreach ($movies as $movie) 
{
    $movie_id = $movie->id;

    if ($movie_id && is_int($movie_id)) 
    {
        break;
    }
}

echo $movie_id;

上面的代码我用来修饰movieid。现在我意识到,当我搜索特定的电影时,它会获取两部电影结果

<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<opensearch:Query searchTerms="Dabangg"/>
<opensearch:totalResults>2</opensearch:totalResults>
<movies>
<movie>
<score/>
<popularity>3</popularity>
<translated>true</translated>
<adult>false</adult>
<language>en</language>
<original_name>Dabangg</original_name>
<name>Dabangg</name>
<alternative_name/>
<type>movie</type>
<id>44425</id>
<imdb_id>tt1620719</imdb_id>
<url>http://www.themoviedb.org/movie/44425</url>
<votes>6</votes>
<rating>5.6</rating>
<certification>PG-13</certification>
<overview>
Set in Uttar Pradesh, Dabangg is the story of a corrupt police officer played by Salman Khan and highlights the flaws and loopholes in the system. The film primarily deals with the unlawful practices in the states of UP and Bihar.
</overview>
<released>2010-09-10</released>
<images>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="thumb" width="92" height="138" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w154/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="w154" width="154" height="231" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="cover" width="185" height="278" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="w342" width="342" height="513" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w500/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="mid" width="500" height="750" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="original" width="1000" height="1500" id="4ea685f034f8633bdc00b45f"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="thumb" width="300" height="169" id="4ea6860234f8633bdc00b477"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w780/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="poster" width="780" height="439" id="4ea6860234f8633bdc00b477"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w1280/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="w1280" width="1280" height="720" id="4ea6860234f8633bdc00b477"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="original" width="1920" height="1080" id="4ea6860234f8633bdc00b477"/>
</images>
<version>800</version>
<last_modified_at>2013-04-09 03:03:56 UTC</last_modified_at>
</movie>
<movie>
<score/>
<popularity>3</popularity>
<translated>true</translated>
<adult>false</adult>
<language>en</language>
<original_name>Dabangg 2</original_name>
<name>Dabangg 2</name>
<alternative_name>沙曼罕之爆裂警官 2</alternative_name>
<type>movie</type>
<id>147405</id>
<imdb_id>tt2112131</imdb_id>
<url>http://www.themoviedb.org/movie/147405</url>
<votes>2</votes>
<rating>5.8</rating>
<certification>NR</certification>
<overview>
Dabangg 2 is an upcoming Bollywood action film directed and produced by Arbaaz Khan under the banner of Arbaaz Khan Productions.It is a sequel to the 2010 film Dabangg and is written by Dilip Shukla. Story is set in the city of Kanpur
</overview>
<released>2012-12-21</released>
<images>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="thumb" width="92" height="130" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w154/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="w154" width="154" height="217" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="cover" width="185" height="261" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="w342" width="342" height="483" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w500/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="mid" width="500" height="706" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="original" width="1156" height="1632" id="514727ab760ee3215200fe27"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="thumb" width="300" height="169" id="51472838760ee3216f010a1c"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w780/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="poster" width="780" height="439" id="51472838760ee3216f010a1c"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w1280/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="w1280" width="1280" height="720" id="51472838760ee3216f010a1c"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="original" width="1920" height="1080" id="51472838760ee3216f010a1c"/>
</images>
<version>2</version>
<last_modified_at>2013-04-28 21:28:49 UTC</last_modified_at>
</movie>
</movies>
</OpenSearchDescription>

此结果中有两个电影ID。我想要的Id是147405,但我只得到了Id 44425.我怎么能得到另一部电影ID。

如果我们看到我搜索<name>Dabangg 2</name>位的电影名称,它也会发送<name>Dabangg</name>。如何检查名称并从xml中获取该ID。

2 个答案:

答案 0 :(得分:1)

如果您想搜索确切的术语,如

$movieName = 'Dabangg 2';

你需要用双引号括起来:

$apiKey = '30f44b6ef9472d414e50d2acaa058b60';

$url = sprintf(
    'http://api.themoviedb.org/2.1/Movie.search/en/xml/%s/"%s"',
    $apiKey,
    rawurlencode(trim($movieName))
);

这将允许您获得您正在寻找的ID:

$xml = simplexml_load_file($url);
$movie = $xml->movies->movie;

echo $movieName, ': ', $movie->id; # Dabangg 2: 147405

答案 1 :(得分:0)

break结束当前while的执行。然后不要使用它,因为在第一次找到匹配后,它结束了while循环