Page 1 of 2

How to solve: XML Parsing Error: no element found

Posted: Wed Jun 02, 2010 10:02 pm
by plusnplus
Hi all,
I try to get google weather info into my website.
when i try this code:

Code: Select all

<?php
header("content-type: text/xml");
$xmlData = file_get_contents("http://www.google.com/ig/api?weather=new%20york");
echo $xmlData;
?>
i got error : XML Parsing Error: no element found

but when i directly type http://www.google.com/ig/api?weather=new%20york into the browser, it give me the xml file.
or.. it that possible the error because, my company block something in firewall?

Any idea how to solve this problem?

Thanks for any reply/ help

Re: How to solve: XML Parsing Error: no element found

Posted: Thu Jun 03, 2010 12:49 am
by Zyxist
This is a browser error, not PHP. What browser are you testing it? Do you have allow_url_fopen enabled to use HTTP in file_get_contents()?

Re: How to solve: XML Parsing Error: no element found

Posted: Thu Jun 03, 2010 1:27 am
by plusnplus
Zyxist wrote:This is a browser error, not PHP. What browser are you testing it? Do you have allow_url_fopen enabled to use HTTP in file_get_contents()?
Hi Zyxist,
I user firefox 3.6
when i test with phpinfo(), allow_url_fopen is both on (local value and master value).

if this one is browser error, why when i directly type http://www.google.com/ig/api?weather=new%20york into the browser, it display me the xml file?

is that something todo with my company router/ firewall policy/ rule?

Re: How to solve: XML Parsing Error: no element found

Posted: Thu Jun 03, 2010 2:12 am
by Zyxist
This is a browser error, because the code you have provided has nothing to do with XML, so it is impossible for it to generate XML errors. Change the Content-type to "text/plain" and check, what the browser shows.

Re: How to solve: XML Parsing Error: no element found

Posted: Thu Jun 03, 2010 2:58 am
by plusnplus
Zyxist wrote:This is a browser error, because the code you have provided has nothing to do with XML, so it is impossible for it to generate XML errors. Change the Content-type to "text/plain" and check, what the browser shows.
Hi Zyxist,
It show nothing, try on firefox 3.6 and ie 7
when i use text/xml, in both browser give same error message.

Re: How to solve: XML Parsing Error: no element found

Posted: Thu Jun 03, 2010 6:14 am
by markusn00b
The problem is that it isn't valid XML. You might try to prepend the XML declaration to the string.

Re: How to solve: XML Parsing Error: no element found

Posted: Thu Jun 03, 2010 7:25 am
by phdatabase
Try it without the header. You're just doing a client-server file transfer here, not creating an HTTP connection.

Re: How to solve: XML Parsing Error: no element found

Posted: Thu Jun 03, 2010 8:42 am
by Zyxist
If it shows nothing, it means that your script sends nothing (because it retrieves nothing). And this is why the browser complains about missing XML elements. You can check it by doing for example

Code: Select all

var_dump(strlen($xmlData));
It will show you the length of the data received from Google.

Re: How to solve: XML Parsing Error: no element found

Posted: Thu Jun 03, 2010 10:11 am
by phdatabase
Zyxist wrote:If it shows nothing, it means that your script sends nothing (because it retrieves nothing). And this is why the browser complains about missing XML elements. You can check it by doing for example

It will show you the length of the data received from Google.
NOT necessarily true. If you see nothing after running the script, check the page source.

Re: How to solve: XML Parsing Error: no element found

Posted: Thu Jun 03, 2010 7:14 pm
by plusnplus
phdatabase wrote:Try it without the header. You're just doing a client-server file transfer here, not creating an HTTP connection.
Hi phdatabase,
Without header, the screen show nothing/ empty.

Re: How to solve: XML Parsing Error: no element found

Posted: Thu Jun 03, 2010 7:20 pm
by plusnplus
Zyxist wrote:If it shows nothing, it means that your script sends nothing (because it retrieves nothing). And this is why the browser complains about missing XML elements. You can check it by doing for example

Code: Select all

var_dump(strlen($xmlData));
It will show you the length of the data received from Google.
Hi Zyxist,
With var_dump(strlen($xmlData));, the screen show "int(0)"

but again, why when i type direct in url browser: http://www.google.com/ig/api?weather=new%20york
it show something like:

Code: Select all

<xml_api_reply version="1">
−
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
−
<forecast_information>
<city data="New York, NY"/>
<postal_code data="new york"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2010-06-03"/>
<current_date_time data="2010-06-04 00:05:31 +0000"/>
<unit_system data="US"/>
</forecast_information> ...
in my test php page, it is really nothing beside this code:

Code: Select all

<?php
header("content-type: text/xml");
$xmlData = file_get_contents("http://www.google.com/ig/api?weather=new%20york");
echo $xmlData;
?>
or.. should i put something html tag(head, title, body, etc..)?

Thanks for everyone who try to help me.

Re: How to solve: XML Parsing Error: no element found

Posted: Fri Jun 04, 2010 10:20 am
by Jonah Bron
plusnplus wrote:
phdatabase wrote:Try it without the header. You're just doing a client-server file transfer here, not creating an HTTP connection.
Hi phdatabase,
Without header, the screen show nothing/ empty.
Did you check the page source?

Re: How to solve: XML Parsing Error: no element found

Posted: Sun Jun 06, 2010 11:15 pm
by plusnplus
Jonah Bron wrote:
plusnplus wrote:
phdatabase wrote:Try it without the header. You're just doing a client-server file transfer here, not creating an HTTP connection.
Hi phdatabase,
Without header, the screen show nothing/ empty.
Did you check the page source?

Hi Jonah Bron,
I did check, it is empty.

hm.., i really need weather info into my webpage.
Anyone can help :)

Re: How to solve: XML Parsing Error: no element found

Posted: Mon Jun 07, 2010 1:39 am
by Zyxist
phdatabase -> but in this case it must be true. It's just about connecting three simple facts about what he sees and what Content-type he used.

plusnplus -> try this script locally. If the problem appears on server only, you must contact the administrator. Maybe outgoing HTTP connections are not allowed there. Anyway, does the problem still exists there, if you use cURL?

Re: How to solve: XML Parsing Error: no element found

Posted: Mon Jun 07, 2010 1:49 am
by plusnplus
Zyxist wrote:phdatabase -> but in this case it must be true. It's just about connecting three simple facts about what he sees and what Content-type he used.

plusnplus -> try this script locally. If the problem appears on server only, you must contact the administrator. Maybe outgoing HTTP connections are not allowed there. Anyway, does the problem still exists there, if you use cURL?
Hi Zyxist,
1. What you mean by try it locally?
i test the scrip in my test server(win xp+apache).
when i try in my live server(ubuntu 8.04+apache), it also give the same error.

2. what you mean by http connection are not allowed?
like i meantion before i can get xml file if i type directly in browser:http://www.google.com/ig/api?weather=new%20york

3. how to use cURL?

yes, the problem still exist :(

anyway. thanks for your help/ reply