Link to home
Start Free TrialLog in
Avatar of NicoJuicy
NicoJuicyFlag for Belgium

asked on

SimpleXML error on check if element exists

I have the below code in my script.

Is there anyway to change the "if element exists"? or the get_file_contents?

Sometimes this scripts works (one by one) and sometimes it doesn't....

(Warning: file_get_contents(http://www.trynt.com/movie-imdb-api/v2/?t=Ali+G-In+Da+House&fo=xml&f=0) [function.file-get-contents]: failed to open stream: HTTP request failed! in C:\xampp\htdocs\ListFilms\includes\functions.php on line 33)

I have used the 2 different if statements, but both could give an error.

The first one is
 if($xmlObj->{'movie-imdb'}->{'matched-url'} )      

And the second one is
if(count($xmlObj->{'movie-imdb'}->{'matched-url'}) > 0)      

The second possibilty is, that the error is fully dedicated to the file_get_contents

I want a solution to solve this problem.
function updateDB($connection)
{	
	$sql ="SELECT * FROM films WHERE imdbUrl = '' OR imdbUrl = 'no'";
if (!($temp = mysql_query($sql,$connection)))
showerror(); 
	while ($films = mysql_fetch_array($temp)) 
	{
	echo $films['titel'];
	//get contents of de xml pagina
	$stringXML = file_get_contents("http://www.trynt.com/movie-imdb-api/v2/?t=" .str_ireplace(" ","+",$films['titel']) . "&fo=xml&f=0"); 
	$xmlObj = new SimpleXMLElement($stringXML); //doorzoek de xmlpagina
 
//voorgaand : if($xmlObj->{'movie-imdb'}->{'matched-url'} )	
 
 
	if(count($xmlObj->{'movie-imdb'}->{'matched-url'}) > 0)	
	{
		$imdbUrl= $xmlObj->{'movie-imdb'}->{'matched-url'}; //exacte Url van imdb link
	}else{
		$imdbUrl = "no";
	}
	echo "<i>" . $imdbUrl . "</i>";
	$sql2 = "UPDATE films SET imdbUrl = '" . $imdbUrl . "' WHERE id = '" . $films['id'] . "'";
	echo $sql2;
	mysql_query($sql2);
	usleep(50);
	}
}

Open in new window

Avatar of NicoJuicy
NicoJuicy
Flag of Belgium image

ASKER

I have to add, that i just noticed that only 1 query gets done every time.

The second query always fails on the file_get_contents() ...

Can anyone help me out? :)
Avatar of Chris Harte
The error on file_get_contents() means it could not find the file it was meant to open.
Put these two lines in to make sure you are parsing the file name and path correctly.

$file_name = "http://www.trynt.com/movie-imdb-api/v2/?t=" .str_ireplace(" ", "+", $films['titel']) . "&fo=xml&f=0";
echo "the file name is : $file_name";
        

Open in new window

The url is always correct.. :(
Are you sure this is right?
if ($xmlObj->{'movie-imdb'}->{'matched-url'})

I thought it would be
if ($xmlObj->movie-imdb->matched-url)
Yes i am,

because simplexml doesn't support other chars (eg.   -   )
when there is        -       in an element, you have to do this with {'element-name')

because ->element-name

wont work :)
ASKER CERTIFIED SOLUTION
Avatar of NicoJuicy
NicoJuicy
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial