7 Replies - 1086 Views - Last Post: 01 June 2010 - 10:09 AM Rate Topic: -----

#1 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Checking for existence of xml file before loading

Posted 28 May 2010 - 08:36 AM

Hey all,

I wrote a simple function that takes a url to a RSS feed, creates a SimpleXmlElement object from the xml using simplexml_load_file, parses it and displays the info:

function readRSS($url)
{
	//assign the file to an object
	$rss = simplexml_load_file($url);
        // format and output content
}
 //call the function using CNN's top story feed
 readRSS("http://rss.cnn.com/rss/cnn_topstories.rss");




This works fine for me but I am not sure how to check to see if the file is there before using simplexml_load_file. I have tried:
   if(file_exists($url))
   {
    $rss = simplexml_load_file($url);
   }



But that returns false even when the url is valid. I also tried
   if($rss = simplexml_load_file($url))
   {
    // do stuff
   }




But, as expected that didn't work. I could just suppress errors using @ so nothing gets displayed if the url is not valid but that seems like a sloppy solution to me. Could anyone suggest another way to handle broken urls when used in this manner? Any advice is appreciated, thanks much.

Is This A Good Question/Topic? 0
  • +

Replies To: Checking for existence of xml file before loading

#2 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 142
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Checking for existence of xml file before loading

Posted 28 May 2010 - 09:05 AM

You can check the existence of file by following code, which just check for header-

function url_exists($url)
{
	if((strpos($url, "http")) === false)
	{
		$url = "http://" . $url;
	}

	if (is_array(@get_headers($url)))
	{
		return true;
	}
	else
	{
		return false;
	}
}


Was This Post Helpful? 1
  • +
  • -

#3 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Checking for existence of xml file before loading

Posted 31 May 2010 - 05:41 AM

Hey there,

Thanks much for the reply, I tried what you suggested but it does not seem to be working, the function will return true even when it receives an invalid url. For example the url for the CNN Health news feed is :
http://rss.cnn.com/rss/cnn_health.rss



I tried messing with it , removing characters and found the function still returns true :

url_exists("http://rss.cnn.com/rss/")
url_exists("http://rs.com/rss/cnn_health.rss")
url_exists("http://rss.cn.com/rs/.rss")




Apparently in this case get_headers still returns an array of some kind. Could anyone think of another way to do this? Thanks again for the response, I appreciate it. And forgive the lateness of my reply, I was away from home most of the weekend.
Was This Post Helpful? 0
  • +
  • -

#4 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 142
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Checking for existence of xml file before loading

Posted 31 May 2010 - 07:17 AM

The function is not returning true in case of invalid url!

The urls you have tested all issues http response from the server. If you look at the status line of server response, you will see there is status codes like 404 or 301 (which means resource not found or moved permanently respectively). As the code was only checking for get_headers(), not the status, thats why the function was returning true. So if there is a check of status code is made, then the function will return only the responses which return status of OK, which means status code 200. So you can try the following code-

function url_exists($url)
{
	if((strpos($url, "http")) === false)
	{
		$url = "http://" . $url;
	}
	@$a=get_headers($url);
	if (is_array($a))
	{
		$status=$a[0];
		$statusArr=explode(" ",$status);
		$statusCode=$statusArr[1];

		if(intval($statusCode==200))
		{
			return true;
		}
		
		else
		{
			return false;
		}
	}
}


This post has been edited by atik97: 31 May 2010 - 07:21 AM

Was This Post Helpful? 1
  • +
  • -

#5 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Checking for existence of xml file before loading

Posted 31 May 2010 - 07:24 AM

View PostJstall, on 28 May 2010 - 08:36 AM, said:

But that returns false even when the url is valid. I also tried
   if($rss = simplexml_load_file($url))
   {
    // do stuff
   }




But, as expected that didn't work.


That should actually work. Check out how I loaded the XML file for the DIC Dynamic Signature~
http://www.dreaminco...ng-new-xml-api/
(It's in the parseXML() method...)

Yours,
Shane~

This post has been edited by ShaneK: 31 May 2010 - 07:25 AM

Was This Post Helpful? 1
  • +
  • -

#6 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Checking for existence of xml file before loading

Posted 31 May 2010 - 12:53 PM

@atik97 Thanks much for clearing that up for, and your suggestion works great : - )

@ShaneK Thank you for your response. I took a look at what you did and tried to take the same approach like :

   if(!$rss = simplexml_load_file($url))
   {
     echo "Error message";
   }
   else
   {
     // do stuff 
   }




It would evaluate to true(in that it couldn't call simplexml_load_file on the url), I would get the error message I output however I would then get PHP generated error messages as well. Not sure why I am getting these results.Thanks again for responding.
Was This Post Helpful? 0
  • +
  • -

#7 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Checking for existence of xml file before loading

Posted 31 May 2010 - 04:16 PM

View PostJstall, on 31 May 2010 - 12:53 PM, said:

@ShaneK Thank you for your response. I took a look at what you did and tried to take the same approach like :

   if(!$rss = simplexml_load_file($url))
   {
     echo "Error message";
   }
   else
   {
     // do stuff 
   }




It would evaluate to true(in that it couldn't call simplexml_load_file on the url), I would get the error message I output however I would then get PHP generated error messages as well. Not sure why I am getting these results.Thanks again for responding.


I believe you also need to set
libxml_use_internal_errors(true);
.

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#8 bstonehill  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Re: Checking for existence of xml file before loading

Posted 01 June 2010 - 10:09 AM

View PostJstall, on 28 May 2010 - 10:36 AM, said:

Hey all,

I wrote a simple function that takes a url to a RSS feed, creates a SimpleXmlElement object from the xml using simplexml_load_file, parses it and displays the info:

function readRSS($url)
{
	//assign the file to an object
	$rss = simplexml_load_file($url);
        // format and output content
}
 //call the function using CNN's top story feed
 readRSS("http://rss.cnn.com/rss/cnn_topstories.rss");




This works fine for me but I am not sure how to check to see if the file is there before using simplexml_load_file. I have tried:
   if(file_exists($url))
   {
    $rss = simplexml_load_file($url);
   }



But that returns false even when the url is valid. I also tried
   if($rss = simplexml_load_file($url))
   {
    // do stuff
   }




But, as expected that didn't work. I could just suppress errors using @ so nothing gets displayed if the url is not valid but that seems like a sloppy solution to me. Could anyone suggest another way to handle broken urls when used in this manner? Any advice is appreciated, thanks much.


You could also use fopen. file_exists only works with local files.

$fp = fopen($url,"r");
if($fp){
   echo "File Exists";
} else {
   echo "File Does Not Exist";
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1