Forum Moderators: coopster

Message Too Old, No Replies

XML Parsing - Help!

but from an aspx xml file

         

danbhala

6:06 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



hi

i've been following this tutorial and can get it working

<snip>

but what i want to do is take the data from a dynamic aspx xml file

$xml_file = "http://www.example.com/xml/Search.aspx?make=Ford&model=Accord";

i have downloaded the source of this aspx file and saved it as an xml and I can get it to work.

the error I get when trying to parse the dynamic xml is

"Could not read file"

this tells me so far that it has successfully read this file using 'fopen' but it cannot read it using 'fread'

can anyone shine some light on me on how to successfully parse an aspx xml feed with php?

is it something to do with the values in the url?

[edited by: eelixduppy at 6:16 pm (utc) on June 13, 2007]
[edit reason] removed link - see charter [/edit]

eelixduppy

7:01 pm on Jun 13, 2007 (gmt 0)



It should work. There's a few things you can look at.

-What happens that caused your script to output "Could not read file"? If you don't know, then post just that relevant code.
-Are you getting any PHP errors in your error log?
-Do you have PHP 5 so that you can use simpleXML [php.net]?

danbhala

9:26 am on Jun 14, 2007 (gmt 0)

10+ Year Member



$xml_file = "http://mysite.com/feeds/Search.aspx?make=Honda&model=Civic";

.
.
.

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startTag", "endTag");

xml_set_character_data_handler($xml_parser, "contents");

$fp = fopen($xml_file, "r") or die("Could not open file");

$data = fread($fp, filesize($xml_file)) or die("Could not read file"); *** Script stops on this line

if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}

xml_parser_free($xml_parser);

fclose($fp);

---------

i think the problem lies with filesize($xml_file)

as when i try and echo that directly after declaring the $xml_file, it doesnt output anything...

danbhala

9:31 am on Jun 14, 2007 (gmt 0)

10+ Year Member



i dont have php 5 im afraid too

Habtom

9:34 am on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can filesize and fopen handle such a URL, having an output from a dynamic URL and having a real file could be different, as those are meant to handle files.

Interesting, will try it out myself.

Hab

danbhala

9:39 am on Jun 14, 2007 (gmt 0)

10+ Year Member



perhaps i should look into using CURL to read in the url along with its query string...

danbhala

9:43 am on Jun 14, 2007 (gmt 0)

10+ Year Member



got it :)

i have added in this function to the equation:

function extrair_url_curl ($url)
{
$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);

ob_start();

curl_exec ($ch);
curl_close ($ch);

$string = ob_get_contents();

ob_end_clean();

return $string;
}

so instead of the lines

$fp = fopen($xml_file, "r") or die("Could not open file");

$data = fread($fp, filesize($xml_file)) or die("Could not read file");

i have replaced it simply with:

$data = extrair_url_curl($xml_file);

---

woohoo :)