CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   xml feed into php/MYSQL listings site - noob question(s) (http://www.codingforums.com/showthread.php?t=282828)

Tynan 11-23-2012 11:58 AM

xml feed into php/MYSQL listings site - noob question(s)
 
Hello all

i have a listings site that runs php from mysql results, all good

but now I want to take an xml feed from a client with hundreds of live listings

all new to me and i want to be sure I have the big picture right before plunging in

am I right to think that I give them an XML template which is really just a list of the fields I need and the tags for each, so more or less what I'd get from my current MYSQL query, data and markup wise?

So when my regular MYSQL query runs, I also pull the xml data from their site (or from a cache of that feed), parse it and then merge it with the array from MYSQL, on the fly server side, and then carry on as before?

Is it that simple and are there any noob dos and don'ts?

thanks
Tynan

Redcoder 11-23-2012 12:50 PM

I'd advise you to use JSON -newer data format which is much easier to use.

But if you still want to use XML,
PHP has a very robust XML parser. It has many methods that will make XML parsing a breeze.


Check out phpfreaks' xml tutorial.

http://www.phpfreaks.com/tutorial/handling-xml-data

Tynan 11-23-2012 02:51 PM

Hi and thanks

noted but client is asking to use XML so I guess I'm stuck with what he wants to feed

Are my airey and vague assumptions correct and workable though?

Redcoder 11-23-2012 02:54 PM

Yeah. That's doable. Done that a lot before.

Although your job is also to advise your client. Make him see the light on JSON. Unless he has a very specific reason to use XML like that is what their feeds are made using.

Tynan 11-23-2012 04:08 PM

Thanks, have skimmed that tutorial on the link and it looks a doddle which is nice

I'll get this one under my belt before telling people doing it already what to do I think

Thanks for your help, I may of course be back in due course :)

Tynan 03-12-2013 01:43 PM

And here indeed I am again

I've quite happily managed to parse XML feeds and access the data and loop through it etc etc

But I'm now trying to build a php array from the xml that I can then merge with an existing array from a local MYSQL query

When my doubtless simpleton code as below runs

PHP Code:

$xml simplexml_load_file('http://www.xml2u.com/Xml/Blue%20Square_1314/2389_Default.xml');
$Properties $xml->xpath('Clients/Client/properties/Property');
//$xml = json_decode(json_encode((array) simplexml_load_file($xml)), 1);
$x=0;
foreach (
$Properties as $Property)//immoblier

{

$Ref $Property->category;

$Region $Property->Address->region;
$Department $Property->Address->subRegion;
$Town $Property->Address->location;
$ClassifiedText $Property->Description->description;
$AdTitle $Property->Description->title;
$Area $Property->Description->floorSize;
$Land $Property->PlotSize->plotSize;
$Photo1 $Property->images->image[1]->image;
$Photo2 $Property->images->image[2]->image;
$Photo3 $Property->images->image[3]->image;

$arrayBS=array(Ref=>$Ref,Region=>$Region,Department=>$Department,Town=>$Town,ClasifiedText=>$ClassifiedText,AdTitle=>$AdTitle,Area=>$Area,Land=>$Land,Photo1=>$Photo1,Photo2=>$Photo2,Photo3=>$Photo3);
$x++;
}
print_r($arrayBS); 

What on earth am I not doing right?

The xml file is as below in case it's not normal

http://www.xml2u.com/Xml/Blue%20Squa...89_Default.xml

yesterday I could create an array, today only a error

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "" in /home/rauxvsuj/public_html/ClassifiedXML.php on line 175

sorry, I'm frazzled and confused and would love someone to squint at the xml feed and point in the right direction to create a nice and easy php array, from there I can do it

Tynan 03-12-2013 02:01 PM

bah, I think I'm there after posting it here, ain't that so often the way, the below copied from somewhere gives me a clean array

PHP Code:

<?php
$countries 
= array();
$dom = new DOMDocument();
$dom->load('http://www.xml2u.com/Xml/Blue%20Square_1314/2389_Default.xml');
foreach (
$dom->getElementsByTagName('properties') as $Property) {
  
$country_name $Property->getElementsByTagName('propertyid')->item(0)->textContent;
  
$country_code $Property->getElementsByTagName('category')->item(0)->textContent;
  
$countries[$country_name] = $country_name;
  
$countries[$country_code] = $country_code;
  
print_r($countries);
}
?>


Tynan 03-12-2013 03:44 PM

so far so good, I can load everything into the php array except a list of images that are numbered and use the tag name 'image' nested inside another tag name of 'image', I can't get into there, I presume because the DOM code finds the outer image tag? Google not helping specifically so far

Code:

<images>
<image number="1">
<image>
http://www.emulis.net/shared/depot/3...ges/664567.jpg
</image>
<image number="2">
<image>http://www.emulis.net/shared/depot/3...ges/664565.jpg
</image>
</images>


Tynan 03-12-2013 04:15 PM

Quote:

Originally Posted by Tynan (Post 1319965)
so far so good, I can load everything into the php array except a list of images that are numbered and use the tag name 'image' nested inside another tag name of 'image', I can't get into there, I presume because the DOM code finds the outer image tag? Google not helping specifically so far

Code:

<images>
<image number="1">
<image>
http://www.emulis.net/shared/depot/3...ges/664567.jpg
</image>
<image number="2">
<image>http://www.emulis.net/shared/depot/3...ges/664565.jpg
</image>
</images>


hmpf, I can get the image links with the following rather obvious lines

PHP Code:

$Photo1=$Property->getElementsByTagName('image')->item(1)->textContent

but it starts going wrong when the outer image tag has a child of 'alt' as below

Code:

<images>
<image number="1">
<image>http://www.emulis.net/shared/depot/3168/biens/149506/images/627783.jpg</image><alttext/></image>
<image number="2"><image>http://www.emulis.net/shared/depot/3168/biens/149506/images/627794.jpg</image>
<alttext>Ext</alttext>
</images>

I get the alt text suffixed to the image text and I get the previous link instead of the third when I ask for


PHP Code:

$Photo3=$Property->getElementsByTagName('image')->item(3)->textContent

if you can image a third one

Tynan 03-12-2013 05:34 PM

found a way around this, apologies for anyone reading my rather frenetic and unclear posts above


All times are GMT +1. The time now is 06:12 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.