0

我有问题

<?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。

4

2 回答 2

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
于 2013-05-05T12:03:33.900 回答
0

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

于 2013-05-05T03:43:48.100 回答