View Full Version : PHP with XML problem.
Hosho
05-16-2007, 03:49 PM
Hello all you clever people.
I have been using XML for ooh.. about 5 hours now, and I'm a little confused as to why this isn't working. I've spent a while now pouring over documentation and I find nothing that covers this, I assume it's so simple no one thought it worth writting.
$url="http://armory.wow-europe.com/guild-info.xml?r=The+Venture+Co&n=Legacy";
if (!($fp=@fopen($url, "r"))) die ("Couldn't open XML.");
For some reason, the above PHP code doesn't correctly open the XML file defined in $url. While playing with it, I downloaded the file, renamed it "test.xml", placed it in the same dir as the PHP file I'm playing with and altered the code..
//$url="http://armory.wow-europe.com/guild-info.xml?r=The+Venture+Co&n=Legacy";
$url="test.xml";
if (!($fp=@fopen($url, "r"))) die ("Couldn't open XML.");
But that, of course, defeats the purpose, since I'd have to manually download the XML document every time.
Does anyone know what my problem is? Or can anyone suggest a possible work-around?
shyam
05-16-2007, 04:47 PM
take a look at this
http://in.php.net/manual/en/ref.filesystem.php#ini.allow-url-fopen
Hosho
05-16-2007, 06:22 PM
phpinfo() (http://www.legacy-guild.co.uk/sandbox/test2.php)
Nope, that's turned on. Thanks for the suggestion, though.
I'll keep playing with it, anyone got any other ideas? =/
Hosho
05-16-2007, 06:26 PM
In case it helps anyone..
<?php
$url="http://armory.wow-europe.com/guild-info.xml?r=The+Venture+Co&n=Legacy";
//$url="test.xml";
if (!($fp=@fopen($url, "r"))) die ("Couldn't open XML.");
$usercount=0;
$userdata=array();
$state='';
$memberCount;
function startElHandler ($parser,$name,$attrib){
global $usercount;
global $userdata;
global $state;
switch ($name) {
case $name=="MEMBERS" : {
$membercount = $attrib["MEMBERCOUNT"]; // Load the number of pages into a variable.
break;
}
case $name=="CHARACTER" : { // Using the same order used in the XML. Why? Why not?
$userdata[$usercount]["class"] = $attrib["CLASS"];
$userdata[$usercount]["classId"] = $attrib["CLASSID"]; // No idea what this is.
$userdata[$usercount]["gender"] = $attrib["GENDER"];
$userdata[$usercount]["genderId"] = $attrib["GENDERID"]; // 0 = male, 1 = female. (assumed)
$userdata[$usercount]["level"] = $attrib["LEVEL"];
$userdata[$usercount]["name"] = $attrib["NAME"]; // should be key, imo?
$userdata[$usercount]["race"] = $attrib["RACE"];
$userdata[$usercount]["raceId"] = $attrib["RACEID"]; // order seems random to me.
$userdata[$usercount]["rank"] = $attrib["RANK"]; // Guild rank, not PvP rank. Replace with rank names later on?
$userdata[$usercount]["url"] = $attrib["URL"]; // GET data of the character's Armory page. example: "r=The+Venture+Co&n=Hosho" So "http://armory.wow-europe.com/character-sheet.xml?".$userdata[i]["url"]; would work.
break;
}
default : {$state=$name;break;}
}
}
function endElHandler ($parser,$name){
global $usercount;
global $userdata;
global $state;
$state='';
if($name=="CHARACTER"){
$usercount++;
}
}
if (!($xml_parser = xml_parser_create())) die("Couldn't create parser.");
xml_set_element_handler( $xml_parser, "startElHandler", "endElHandler");
while( $data = fread($fp, 100000)){
if(!xml_parse($xml_parser, $data, feof($fp))) {
break;
}
}
xml_parser_free($xml_parser);
?>
<html>
<head><title>Testing random crap</title>
</head>
<body>
<table border=1>
<tr>
<td>Name</td>
<td>Class</td>
<td>Level</td>
<tr>
<?
for ($i=0;$i<$usercount; $i++){
echo " <tr>\n";
echo " <td>".$userdata[$i]["name"]."</td>\n";
echo " <td>".$userdata[$i]["class"]."</td>\n";
echo " <td>".$userdata[$i]["level"]."</td>\n";
echo " </tr>\n";
}
echo "</table>\n";
?>
</body>
<html>
I'll keep looking for an answer.. =/
When I go to that url, I get an HTML webpage, and not XML. Are you perhaps logged in and seeing something different?
Hosho
05-16-2007, 08:38 PM
I'm starting to think the problem is somewhere in the header, but once again I don't understand the subject well enough to be sure.
If you point your browser to http://armory.wow-europe.com/guild-info.xml?r=The+Venture+Co&n=Legacy you will see an HTML page, but if you attempt to view the source code of the page, you'll find an XML document.
I found this nifty little tool (http://web-sniffer.net/?url=http%3A%2F%2Farmory.wow-europe.com%2Fguild-info.xml%3Fr%3DThe%2BVenture%2BCo%26n%3DLegacy&submit=Submit&http=1.1&gzip=yes&type=GET&ua=Mozilla%2F5.0+%28Windows%3B+U%3B+Windows+NT+5.1%3B+en-US%3B+rv%3A1.8.1.3%29+Gecko%2F20070309+Firefox%2F2.0.0.3+Web-Sniffer%2F1.0.24) to get a little more information if that helps anyone. It doesn't really help me much i'm afraid. :(
Getting close to the source of the problem, i think!
it might have an XML tag near the top, but it isn't a valid XML document, and parsers are going to struggle.
Is it provided as a web-service, or are you doing this on your own initiative?
Hosho
05-17-2007, 12:07 AM
Bits of both.
The "XML" is something I have absolutely no control over. It's very.. uhm.. unorthodox, but it's what's there, and it's what I have to work with.
The plan is simply to take the data in the XML document and load it into a local database. The data is stored in attributes, don't ask me why.
Parsing the XML hasn't proven too difficult. The exact same content copied to another location works exactly as I expect it to. But there seem to be other forces at work here, some kind of server-side application that makes some kind of decision based on (i assume) request headers.
Hosho
05-17-2007, 12:09 AM
http://tivoli.xtr.dk/Reputation/Functions.phps
Some other bright spark elsewhere answered the problem for me. Huzzah! Makes me glad there's such nice people in the world. :D
The function getCharacterData() answers my problem. It was, indeed, an issue with headers. Don't ask me to explain it, though.
Thanks to everyone who helped! I'll spend some time tomorrow sorting this out!
frott
05-24-2007, 08:53 PM
http://tivoli.xtr.dk/Reputation/Functions.phps
Some other bright spark elsewhere answered the problem for me. Huzzah! Makes me glad there's such nice people in the world. :D
The function getCharacterData() answers my problem. It was, indeed, an issue with headers. Don't ask me to explain it, though.
Thanks to everyone who helped! I'll spend some time tomorrow sorting this out!
Hi!
The solution you've linked to utilises php5, specifically simplexml_load_string(). I notice that your code uses php4 implementation.
Did you end up scrapping your code for the php5, or did you use the header building portion only? Unfortunately my server only has 4.4.6 so I cannot use the streamlined load_string, I have to use the older parsing methods.
Would you mind posting your final solution?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.