Read RSS feeds

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vidhyakrish
Forum Newbie
Posts: 10
Joined: Mon Feb 27, 2012 11:21 am

Read RSS feeds

Post by vidhyakrish »

Hi

i am trying to read this rss feed
"https://www.myjobscotland.gov.uk/rssjob ... 6&co2=1158"

using this PHP code:

<?php
$rss = new SimpleXMLElement('https://www.myjobscotland.gov.uk/rssjob ... 6&co2=1158', null, true);
foreach($rss->xpath('jobs/job') as $item)
{ ?> <p><?php print $item->PositionTitle; ?> </p>
<p> <?php echo $item->GeographicalArea;}
?></p>

But have been unsuccessful reading.
Can anybody help on how to read this feed?

Thanks in advance.

P.S: I am a PHP newbie!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Read RSS feeds

Post by Celauran »

vidhyakrish wrote:

Code: Select all

<?php
$rss = new SimpleXMLElement('https://www.myjobscotland.gov.uk/rssjobfeed/rss.aspx?sourceagencyid=11506&co2=1158', null, true);
 foreach($rss->xpath('jobs/job') as $item)
{     ?>      <p><?php          print $item->PositionTitle;   ?>   </p>
    <p>  <?php   echo $item->GeographicalArea;}
?></p>
Where are you getting jobs/job from? $item->PositionTitle? $item->GeographicalArea?
vidhyakrish
Forum Newbie
Posts: 10
Joined: Mon Feb 27, 2012 11:21 am

Re: Read RSS feeds

Post by vidhyakrish »

Hi there,

sorry it should be 'channel/item' not 'jobs/job' .

Mistake!

thanks
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Read RSS feeds

Post by Celauran »

So you fixed $item->PositionTitle and $item->GeographicalArea as well? Is it working now?
vidhyakrish
Forum Newbie
Posts: 10
Joined: Mon Feb 27, 2012 11:21 am

Re: Read RSS feeds

Post by vidhyakrish »

No,
I tried to output $item->title and $item->description but getting the following errors :

Warning: SimpleXMLElement::__construct(https://www.myjobscotland.gov.uk/rssjob ... 6&co2=1158) [function.SimpleXMLElement---construct]: failed to open stream: Connection refused in /public_html/site/scripts/rss_myjobs.php on line 2

Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: I/O warning : failed to load external entity "https://www.myjobscotland.gov.uk/rssjob ... 6&co2=1158" in /public_html/site/scripts/rss_myjobs.php on line 2

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/southlancs/public_html/site/scripts/rss_myjobs.php:2 Stack trace: #0 /public_html/site/scripts/rss_myjobs.php(2): SimpleXMLElement->__construct('https://www.myj...', 0, true) #1 {main} thrown in /public_html/site/scripts/rss_myjobs.php on line 2
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Read RSS feeds

Post by Celauran »

This is working fine for me:

Code: Select all

<?php

$rss = new SimpleXMLElement('https://www.myjobscotland.gov.uk/rssjobfeed/rss.aspx?sourceagencyid=11506&co2=1158', null, true);

foreach ($rss->channel->item as $job)
{
    echo "<div>";
    echo strip_tags($job->title) . "<br />";
    echo strip_tags($job->description);
    echo "</div>";
}
vidhyakrish
Forum Newbie
Posts: 10
Joined: Mon Feb 27, 2012 11:21 am

Re: Read RSS feeds

Post by vidhyakrish »

thanks for the code!

I just tried in another hosting account - which works fine with the same code!

But the one where i actually want it work - still displays the following error:


Warning: SimpleXMLElement::__construct(http://www.myjobscotland.gov.uk/rssjobf ... 6&co2=1158) [function.SimpleXMLElement---construct]: failed to open stream: Connection refused in /home/southlancs/public_html/site/scripts/rss_myjobs.php on line 3

Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: I/O warning : failed to load external entity "http://www.myjobscotland.gov.uk/rssjobf ... 6&co2=1158" in /public_html/site/scripts/rss_myjobs.php on line 3

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/southlancs/public_html/site/scripts/rss_myjobs.php:3 Stack trace: #0 /public_html/site/scripts/rss_myjobs.php(3): SimpleXMLElement->__construct('http://www.myjo...', 0, true) #1 {main} thrown in public_html/site/scripts/rss_myjobs.php on line 3


I dont understand these errors, can you ?

Sorry to be a pain! is there something to do with settings/configuration/infrastructure as the same code works fine in different hosting server.

Cheers
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Read RSS feeds

Post by temidayo »

vidhyakrish wrote: ...Sorry to be a pain! is there something to do with settings/configuration/infrastructure as the same code works fine in different hosting server.
Cheers
You may want to check allow_url_fopen :
http://us2.php.net/manual/en/filesystem ... -url-fopen
vidhyakrish
Forum Newbie
Posts: 10
Joined: Mon Feb 27, 2012 11:21 am

Re: Read RSS feeds

Post by vidhyakrish »

just an update, as found the solution is with our server not allowing https: // blocked by firewalls and hence the error " connection refused".

cheers
Post Reply