PHP - XML Parse issue

This script simply fetches the google weather xml and parses it. When I run this script on my server. I receive the errors below. However, it runs fine on other servers. Obviously, some setting is missing on my server

Any ideas?





weather.php (attached)

I receive the following errors
-----------------------------------------------------------------
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 2: parser error : Opening and ending tag mismatch: meta line 1 and HEAD in /home/motoho/public_html/forums/weather.php on line 6

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <TITLE>302 Moved</TITLE></HEAD><BODY> in /home/motoho/public_html/forums/weather.php on line 6

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/motoho/public_html/forums/weather.php on line 6

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 6: parser error : Opening and ending tag mismatch: HEAD line 1 and HTML in /home/motoho/public_html/forums/weather.php on line 6

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: </BODY></HTML> in /home/motoho/public_html/forums/weather.php on line 6

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/motoho/public_html/forums/weather.php on line 6

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 7: parser error : Premature end of data in tag HTML line 1 in /home/motoho/public_html/forums/weather.php on line 6

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: in /home/motoho/public_html/forums/weather.php on line 6

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/motoho/public_html/forums/weather.php on line 6

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/motoho/public_html/forums/weather.php:6 Stack trace: #0 /home/motoho/public_html/forums/weather.php(6): SimpleXMLElement->__construct('<HTML><HEAD><me...') #1 /home/motoho/public_html/forums/weather.php(31): getWeather() #2 {main} thrown in /home/motoho/public_html/forums/weather.php on line 6
---------------------------------------------------------
<?php function getWeather() {

	$requestAddress = "http://www.google.com/ig/api?weather=77071&hl=en";
	$xml_str = file_get_contents($requestAddress,0);
	// Parses XML
	$xml = new SimplexmlElement($xml_str);
	// Loops XML
	$count = 0;
	echo <<< END

END;
//echo $xml->forecast_conditions[0]->city['data'];
//var_dump($xml);
echo '<div id="weather">';
	foreach($xml->weather as $item) {

		foreach($item->forecast_conditions as $new) {
		
		echo $new->day_of_week['data'];	echo '<img src="http://www.motohouston.com' .$new->icon['data'] . '"/>';
			
		
		
		}
      	//echo $item->forecast_information->city['data'];
		//echo $item->condition['data'];

	}
  echo '</div>';
  }
  
  getWeather();?>

Open in new window

techempireAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MMDeveloperCommented:
does it still give you the error? It almost appears as if the XML API was down when you ran that script (given the complaint with head/title tags in it). if you hit that URL manually, I only get an XML response, no head/title tags.
0
techempireAuthor Commented:
yep its still there
0
MMDeveloperCommented:
unless someone else chimes in I'll have to try it on my server when I get home from work to see if I can reproduce the problem so I'll have an error I can see/feel/touch and work on.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

MMDeveloperCommented:
also, can you make a phpinfo page and attach the results?
0
hieloCommented:
try:
$xml = new SimplexmlElement( stripslashes($xml_str) );
0
techempireAuthor Commented:
if it helps any, it comes and goes........right now its working........ didnt change a thing :(

0
techempireAuthor Commented:
now I cant reproduce it, it keeps working............

I was pulling the contents into vbulletin with

ob_start();
include('weather.php');
$theweather = ob_get_contents();
ob_end_clean();

Though I cant imagine how that would affect anything, as the script by itself stopped working regardless.
0
techempireAuthor Commented:
hielo, I havent tried what you suggested........ as stated above, as it works now with the default script
0
hieloCommented:
>>if it helps any, it comes and goes........right now its working........ didnt change a thing :(
Then it's not you -  it's the remote server. My suggestion was just in case you have magic_quotes enabled on your server and the request always fails. So if it works sometimes, it's not you. Furthermore, on what you posted I see:

<TITLE>302 Moved</TITLE>

When a remote request is successful, it returns 200 as the status code and the "actual/real" content as the overall result. Based on what you posted, it is returning a 302 status code and a "fake content" that tells you that the resource moved (that's why you are seeing that html markup "...<TITLE>302 Moved</TITLE>..."). What you need to do is after:
  $xml_str = file_get_contents($requestAddress,0);

see if $xml_str is the actual xml you are expecting ( see if it contains the string "xml_api_reply" since that is the root element in the xml file) and if it is NOT, then you need to examine that content to figure out where the new url for the resource you are requesting. Typically, when a server sends a 302 status code, it typically also tells you the new address/url of the resource you are requesting. So once you figure that "new url" you send the request there.
0
hieloCommented:
This will help you understand 302 better:
http://www.checkupdown.com/status/E302.html
0
techempireAuthor Commented:
http://www.google.com/ig/api?weather=77071&hl=en

xml_api_reply version="1" is at the top........

argh.......
0
hieloCommented:
>>xml_api_reply version="1" is at the top
Yes, when it IS the XML you want. I suspect that when you get the 302 status, xml_api_reply is not in the response.
From looking at your xml, there is no <TITLE> tag. So if the content contain <TITLE> then most likely it is NOT the XML you were expecting, in which case you need to look at the rest of the response to see if it tells you which is the new url.
0
techempireAuthor Commented:
didnt find a solution, sigh
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Networking

From novice to tech pro — start learning today.