?
Solved

SimpleXML error on check if element exists

Posted on 2009-04-22
6
Medium Priority
?
1,024 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:NicoJuicy
  • 4
  • 2
6 Comments
 
LVL 7

Author Comment

by:NicoJuicy
ID: 24203039
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? :)
0
 
LVL 17

Expert Comment

by:Chris Harte
ID: 24203392
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

0
 
LVL 7

Author Comment

by:NicoJuicy
ID: 24203549
The url is always correct.. :(
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
LVL 17

Expert Comment

by:Chris Harte
ID: 24203632
Are you sure this is right?
if ($xmlObj->{'movie-imdb'}->{'matched-url'})

I thought it would be
if ($xmlObj->movie-imdb->matched-url)
0
 
LVL 7

Author Comment

by:NicoJuicy
ID: 24203750
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 :)
0
 
LVL 7

Accepted Solution

by:
NicoJuicy earned 0 total points
ID: 24208249
I know how it came.

The php code was all okay, but the api service isn't fast enough :s
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question