0
votes

I got a problem

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

The above code I am using to fecth the movieid . Now I realized that when I search for a particular movie it fetches two movies result

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

There are two movie Ids in this result. The Id which I want is 147405 but I am only getting the Id 44425. How can I get the other movie ID.

If we see the movie name I searched for <name>Dabangg 2</name> bit it send the <name>Dabangg</name> also . How can I make a check of name and take that id from the xml.

2
When you put example code here on the website, write it in a form that shows the issue. E.g. you don't need to put the database interaction code here if you actually have an issue with the XML parsing. See my exemplary edit. - hakre
I get the following problem with the URL you have provided: "Warning: simplexml_load_file(http://api.themoviedb.org/2.1/Movie.search/en/xml/mapi/Dabangg): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized" - hakre
@hakre Its beacuse I haven't provided the api with that URL . I dont want to show here publically . But the above URL will fetch the data which I have provided above - Yogus
Yes, I'm just seeing this now: {"status_code":7,"status_message":"Invalid API key - You must be granted a valid key","location":"http://api.themoviedb.org/2.1/Movie.search/en/xml/mapi/Dabangg"}. - You should explicitly say if your code does not work here. - hakre
Added you a working answer and also an API key that has been registered to be used in Stackoverflow examples. - hakre

2 Answers

1
votes

If you want to search for the exact term like

$movieName = 'Dabangg 2';

You need to wrap it in double-quotes:

$apiKey = '30f44b6ef9472d414e50d2acaa058b60';

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

Which will allow you to obtain the ID you're looking for immediately:

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

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

break ends execution of current while. then do not use it, because after first finding match, it ends the while loop