/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] XML and php curl module

I am trying to set up an xml feed on my webpage and need some direction on what to do. I am retrieving the xml from another website and displaying it in my html pages.

I posted a thread on the xml section of this website and got this answer:

The CURL Module will allow PHP to get the XML. Then you’ll need to use something to format the XML and insert it into your page.

Does anyone have any ideas how I can do this?

to post a comment
PHP

25 Comments(s)

Copy linkTweet thisAlerts:
@tracknutJul 26.2010 — Are you familiar with SimpleXML? I use it quite a bit, it's XML, and it's Simple!

So...

1) curl the file over

2) simplexml_load_file (the file)

3) foreach ($xml->item as $i) { echo $i->author; }

Okay, that's not real code, but I hope you get the idea. Do a Google on SimpleXML, there's a ton of sample code out there.

Dave
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 26.2010 — I will do some research and see what I can find. Thanks for the fast response!
Copy linkTweet thisAlerts:
@MindzaiJul 26.2010 — Also note that you may not even need curl depending on the server's config. You can often load the XML directly via its URL.
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 26.2010 — How do you go about doing that? I have a url for the xml and all I want to do is load it into my html files. If my server was set up in favor of this approach, what would be the best way to do it? I have searched and searched and still haven't found a way. I really don't know xml and the terminology too well so maybe that's why my searches end up being pointless.
Copy linkTweet thisAlerts:
@tracknutJul 26.2010 — Just try it with:

$xml = simplexml_load_file ("http://www.example.com/feed.xml");

Dave
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 26.2010 — This might sound stupid, but do I just put that into my php file and it automatically loads the xml into my php?
Copy linkTweet thisAlerts:
@tracknutJul 26.2010 — Well no, that covers step 1 and 2 from my above list of 3 things. It (assuming it works with the url instead of curl-ing over the file) has only opened the xml file. Now you need to loop through the various RSS items and display them as html, in whatever format you choose.

Dave
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 26.2010 — Thank Dave! That worked perfectly!
Copy linkTweet thisAlerts:
@tracknutJul 26.2010 — Cool. I would note that there's a bit of a trade-off with the curl-vs-no-curl options here. With the current no-curl option, every time someone hits your site, your site will hit that remote site to read the xml file. Hopefully you have permission, and/or your site isn't too busy. With the curl option, you could download the xml file, say once a day, and then you wouldn't be hitting that remote site nearly as much. But your data would be up to 24 hours old.

Just mentioning it - I'm not saying there is a right or wrong way to do it, but I thought I'd throw the issue on the table.

Dave
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 27.2010 — Thanks for the info! I will talk to my client about what he wants. As for this xml/php stuff, I am having an absolute blast learning it. One more question!!!

If I have an xml file that looks like this:

[CODE]
<response>
<query>realty</query>
<location>78745</location>

<results>

<result>
<jobtitle>Realty</jobtitle>
<company>Realty Monkey</company>
<snippet>
yoyoyoyoyoyoyoyo
</snippet>
</result>

<result>
<jobtitle>Realty2</jobtitle>
<company>Realty Monkey2</company>
<snippet>
yoyoyoyoyoyoyoyo2
</snippet>
</result>

</results>

</response>

<result>
[/CODE]


How would I go about giving these different elements styles with xslt??? I tried, but go really confused with the childNodes and all that stuff. Here is what I came up with:

XSLT:
[CODE]<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">

<body style="font-family:Arial;font-size:12pt;>
<div style="color:#cccccc; width:300pt; height:100pt; background:#000000;">
<xsl:value-of select="query"/>
<xsl:value-of select="location"/>
<xsl:value-of select="totalresults"/>
</div>

<xsl:for-each select="response/results/result">
<div style="background-color:#ccc;">
<div style="font-weight:bold;color:teal;"><xsl:value-of select="jobtitle"/><xsl:value-of select="company"/><xsl:value-of select="city"/></div>
</div>
</xsl:for-each>
</body>
</html>[/CODE]


PHP:
[CODE]<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("http://api.indeed.com/ads/apisearch?publisher=3309688785061266&q=$job&l=$zip&sort=&radius=50&st=&jt=&start=&limit=20&fromage=&#37;20&filter=&latlong=1&co=us&chnl=&userip=1.2.3.4&useragent=Mozilla/%2F4.0%28Firefox%29");

$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
print $item->nodeValue . "<br />";
}

?>[/CODE]


However, I cannot get the styles to apply to any of the elements correctly.
Copy linkTweet thisAlerts:
@tracknutJul 27.2010 — Personally I wouldn't use XSLT for this, not because I know it's the wrong thing, just because of my lack of experience with it.

I would open the feed, then simply do something like:
<i>
</i>echo "&lt;div id='box'&gt;";
foreach ($xml-&gt;response-&gt;results-&gt;result as $r) {
echo "&lt;p class='job'&gt;$r-&gt;jobtitle&lt;/p&gt;";
...etc
}
echo "&lt;/div&gt;";

Then just style the #box id and .job class in css as usual.

Dave
Copy linkTweet thisAlerts:
@sohguanhJul 27.2010 — Cool. I would note that there's a bit of a trade-off with the curl-vs-no-curl options here. With the current no-curl option, every time someone hits your site, your site will hit that remote site to read the xml file. Hopefully you have permission, and/or your site isn't too busy. With the curl option, you could download the xml file, say once a day, and then you wouldn't be hitting that remote site nearly as much. But your data would be up to 24 hours old.

Just mentioning it - I'm not saying there is a right or wrong way to do it, but I thought I'd throw the issue on the table.

Dave[/QUOTE]


Actually with no-curl option, you still can mimic cURL 24 hours old concept. The first time you hit you read the contents and save somewhere. Subsequent request you read from that file instead of hit that remote site.

cURL will be a good option say you want to read/write HTTP header and do more complex things in a HTTP request/response. For simple reading of XML file, using cURL maybe overkill in my opinion.

Of cuz since cURL is free, overkill or not is not a point of contention isn't it ? ?
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 27.2010 — Thx Sohgaunh!!!! I will keep that into consideration...

Tracknut, you gave me this code:

[CODE]echo "<div id='box'>";
foreach ($xml->response->results->result as $r) {
echo "<p class='job'>$r->jobtitle</p>";
...etc
}
echo "</div>";[/CODE]


Is $xml suppose to be:

[CODE]$xml = new DOMDocument();
$xml->load("file.xml");[/CODE]


ALSO, what exactly is $r??
Copy linkTweet thisAlerts:
@tracknutJul 27.2010 — 

Is $xml suppose to be:

[CODE]$xml = new DOMDocument();
$xml->load("file.xml");[/CODE]


ALSO, what exactly is $r??[/QUOTE]


No, $xml is supposed to be:
<i>
</i>$xml = simplexml_load_file ("http://www.example.com/feed.xml");

I'm ignoring all the xslt and DOM code you posted, just giving you the PHP code in SimpleXML to read and display your feed.

$r is the shorthand for "$xml->response->results->result" to keep you from having to type it all out each time. In the "foreach" you see the creation of $r.

I don't want you to get confused (ok, *more* confused ? ) here. There are multiple ways of skinning this cat. I'm giving you the SimpleXML way, and obviously you're also looking at doing it via XSLT and lower-level XML handling code. Those two methods do not mix. So you'll want to pick one and go for it, but not mix'n'match them.

Dave
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 27.2010 — I keep getting this error message:

Warning: Invalid argument supplied for foreach() in C:Usersbradharrellxamppxampphtdocssteinyresults.php on line 121

Line 121 is the "foreach" line.

Here is my code, any ideas?

[CODE]<?php

$xml = simplexml_load_file ("example.xml");

foreach ($xml->response->results->result as $r) {
echo "<p class='jobXml'>$r->jobtitle</p>";
echo "<p class='companyXml'>$r->company</p>";
}


?>[/CODE]
Copy linkTweet thisAlerts:
@tracknutJul 27.2010 — Whoops, change that to:
<i>
</i>foreach ($xml-&gt;results-&gt;result as $r) {
echo "&lt;p class='jobXml'&gt;$r-&gt;jobtitle&lt;/p&gt;";
echo "&lt;p class='companyXml'&gt;$r-&gt;company&lt;/p&gt;";
}
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 27.2010 — One more question!!!!

Where exactly does this php go in my html code?

[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head></head>
<body>

<?php

$xml = simplexml_load_file ("http://api.indeed.com/ads/apisearch?publisher=3309688785061266&q=$job&l=$zip&sort=&radius=50&st=&jt=&start=&limit=20&fromage=%20&filter=&latlong=1&co=us&chnl=&userip=1.2.3.4&useragent=Mozilla/%2F4.0%28Firefox%29");

foreach ($xml->results->result as $r) {
echo "<a href='$r->url' class='jobXml'>$r->jobtitle</a>";
echo "<p class='snippetXml'>$r->snippet</p>";
echo "<a href='$r->url' class='companyXml'>$r->company</a>";
}


?>

</body>
</html>[/CODE]


Obviously, this doesn't work because it doesn't recognize the xml function. How do I get it to do so? Does <?xml version="1.0" encoding="ISO-8859-1"?> need to be placed somewhere?
Copy linkTweet thisAlerts:
@tracknutJul 27.2010 — If you named that file "test.php" it should work just fine (I'm assuming we're way past wondering if you have php on your server). If you call it "test.html" then the php parser never looks at it, since you're saying this is html, and not php.

As to where the code should go within the file, it should go wherever you want that output to be. For example you might have a whole page designed and some little div in the corner reserved for this xml feed - then you'd stick that chunk of php code right inside that div.

I hope that makes sense?

Dave
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 27.2010 — Sorry. I am placing this inside a .php file, however, that html included was included in the file. If I place the php we went over in my .php file, I get the error:

Fatal error: Call to undefined function: simplexml_load_file() in /home3/circlexm/public_html/mylocaljoblisting/results.php on line 125

Does this mean I am going to have to curl the xml in?
Copy linkTweet thisAlerts:
@tracknutJul 27.2010 — Hmmm... something's not right with what you've got, and I'm not clear on what you did. I just took your code from post #18, made a file called "test.php" for it, and ran it. It works fine, pulling a whole bunch of job descriptions from somewhere. So the curl is not needed, opening the file directly works.

Obviously you have more in your file than just that, since it's flagging an error on line 125 - can you post the actual file "results.php" that you've got?

Dave
Copy linkTweet thisAlerts:
@MindzaiJul 27.2010 — Are you running PHP5? You need to be to use simplexml unless you install the module separately.
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 28.2010 — Would I set that in my php.ini file or what?
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 28.2010 — It works on my computer when i am running it through apache localhost. However, when I load it to the client's server, it doesn't work. Would I make this change in the php.ini file? I contacted the hosting company and they said that they are running php5. They told me to check the .htaccess files and make sure I don't have it set on php4. The problem is, I don't know where this file is. I looked on all levels. Maybe I need to create a new one?
Copy linkTweet thisAlerts:
@bradleybebadauthorJul 28.2010 — I found my .config file and changed it. PHP4 was definitely the problem. Thanks for all of y'alls help!!!!! Couldn't have done it without you.
Copy linkTweet thisAlerts:
@ehimeJul 28.2010 — I did something similar just the other day, just make sure to switch

the CSV processing for XML. Very easy.

[code=php]
<?php

<?php

#street cred
define('AUTHOR', 'JD (Jon Daniel)');
define('MODDATE', '07232010||12:51');
define('VERSION', '0.2c');
define('OPTION', 'display');


$uri = NULL;
$uri .= 'http://feedpull.godatafeed.com/product.aspx'
$uri .= '?spud=4HQdUqLqR05943LbmAh&#37;2b%2bIoM%2btPmtGWVEB4sF672EO8%3d';

if (($handle = fopen("$uri", "r")) !== FALSE) {

$int = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$c = count($data);
for ($x=0; $x < $c; $x++) {
$c_dim[$int][$x] = $data[$x];
}
$int++;
}
fclose($handle);
}

array_shift($c_dim); //delete head

foreach($c_dim AS $key) {
foreach($key AS $sub_key => $val) {
if ($sub_key == 6) {$n = $val;}
if ($sub_key == 2) {$m = $val;}
} $tmp[$n] = $m;
}


$tmp = array_unique($tmp);

if (OPTION == 'curl') { #CycleTime 6min
$payload = '<br />Missing Files List:';
foreach ($tmp AS $key => $val) {
$ch = curl_init($val . '?wid=171&hei=205');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$data = curl_exec($ch);
curl_close($ch);

if ($data === false) {
echo "cURL Failed on: $key";
exit;
}

$contentLength = 'unknown';

if (preg_match('/Content-Length: (d+)/', $data, $matches)) {
$contentLength = (int)$matches[1];
}

if ($contentLength == 4626) {
$payload .= "<br />$key : $val";
}
}
}

if (OPTION == 'display') { #CycleTime 4min
$payload = NULL; $payload .= "<div style='width:950px;'>
foreach ($tmp AS $key => $val) {
$payload .= "<div style='padding:10px 5px 0;border:solid 1px #000'>";
$payload .= "<img src='$val?wid=171&hei=205' alt='$val' />";
$payload .= "<span>$key</span></div>";
} $payload .= "</div>";
}

echo "<html><body>$payload</body></html>"; ?>
[/code]
×

Success!

Help @bradleybebad spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...