2013-05-05 59 views
0

從XML值我有一個問題Feching的條件檢查

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

上面的代碼我使用的fecth的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> 

有兩個電影IDS這個結果。我想要的Id是147405,但我只能得到Id 44425.我怎樣才能獲得其他電影ID。

如果我們看到電影名稱,我搜索了<name>Dabangg 2</name>位它也發送<name>Dabangg</name>。我如何檢查名稱並從XML中獲取該ID。

+0

當你把鱈魚例子在網站上的這裏,以表明問題的形式寫下來。例如。如果您實際上遇到XML解析問題,則不需要將數據庫交互代碼放在此處。看到我的示例性編輯。 – hakre 2013-05-05 11:29:03

+0

我收到以下問題:*「Warning:'simplexml_load_file(http://api.themoviedb.org/2.1/Movie.search/en/xml/mapi/Dabangg)':無法打開流:HTTP請求失敗!HTTP/1.1 401 Unauthorized「* – hakre 2013-05-05 11:33:19

+0

@hakre它的問題我沒有提供該URL的API。我不想在這裏公開展示。但是上面的URL將獲取我上面提供的數據 – Yogus 2013-05-05 11:35:33

回答

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 
+0

得到它感謝:D – Yogus 2013-05-05 12:23:30

0

break結束目前的執行while。然後因爲後首次發現匹配,則結束while循環

+0

我仍然在下面刪除了回聲Dabangg%202 44425 – Yogus 2013-05-05 03:44:59

+0

@Deonia,在'while'循環中移動回顯 – Amir 2013-05-05 03:47:14

+0

我在循環中添加echo,它顯示Dabangg%202 147405 44425.這意味着我得到了兩個Id,但是如何只能選擇147405,可以根據標記 – Yogus 2013-05-05 03:52:44