PHP Include issue

software development

#1

Hi everyone,

I’ve recently started to use DreamHost to host all of my sites and have currently started transferring them from other companies.

I have, however, stumbled upon a problem and it is to do with the PHP includes rule that DreamHost have. First of all, please bear in mind that I am in no way a Programmer and a lot of the stuff I’ve been able to do in the past with code has been a fluke. I’ve searched the DreamHost wiki and noticed the problems of why this code isn’t working. I’ve tried to locate my file locally and in the Server Route but the piece of code I need to include is actually my forum’s RSS feed (hosted on the same space). I’m rambling on a bit so I’ll post the problem code below…

<? $doc_root = $_SERVER['DOCUMENT_ROOT']; $yabbrssf = "$doc_root/forum/YaBB.pl?action=rss"; // Path to my Forum's RSS feed $content = implode ("", file ($yabbrssf)); // The following just formats my forum's RSS feed into a nice list. preg_match_all("|(.*)|Uism",$content, $items, PREG_PATTERN_ORDER); for ($i=0;$i<count($items[1]);$i++) { preg_match_all("|(.*)(.*)(.*)|Uism",$items[1][$i], $regs, PREG_PATTERN_ORDER); echo "".$regs[1][0]."
\n"; } ?>

The two lines I’m basically having a problem with is:

$yabbrssf = “$doc_root/forum/YaBB.pl?action=rss”; // Path to my Forum’s RSS feed

$content = implode ("", file ($yabbrssf));

and from reading online it seems it is the ?action=rss of my URL. Now, this isn’t a php variable so I can’t really follow (or I didn’t understand) the http://wiki.dreamhost.com/Allow_url_fopen page.

Sorry for rambling but it is just really important I get this working as it is for a client and it is heavily featured in the design of the site!

Thanks


#2

Oh, I also just wanted to add that I did attempt to use the curl include tutorial and it did print out my RSS but I wasn’t able to format it and it printed out everything in my RSS feed. My current set up only prints the titles and has them as links.


#3

Can you post your original, unedited script (the one that used a regular include)?


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#4

Sure, it was just a php include that linked to the php file (code in original post) that outputted and formatted my forum’s RSS feed.

<?php include "http://www.mysite.com/rss.php"; ?>

#5

if i’m not wrong, you can’t include an external file from other websites. For example: you can’t include(“http://website/rss.php”); even the file is in your own webiste.

Try to use $_SERVER[‘DOCUMENT_ROOT’] or “/” to refer to your root folder. For example: include("/rss.php"); or include("…/rss.php");

Hope it works

Save $97 with promo code: [color=#CC0000]97YES Sign Up NOW[/color]


#6

patricktan, yes I have tried that already and it still comes back with an error regarding the include that is in my rss.php file, which is part of the code I’ve posted below…

$doc_root = $_SERVER[‘DOCUMENT_ROOT’];

$yabbrssf = “$doc_root/forum/YaBB.pl?action=rss”; // Path to my Forum’s RSS feed

$content = implode ("", file ($yabbrssf));


#7

When you are using $_SERVER[‘DOCUMENT_ROOT’] (which should work just fine), what is the error message you are getting?


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#8

The error I get is…

Warning: file(/home/username/mysite.com/forum/YaBB.pl?action=rss) [function.file]: failed to open stream: No such file or directory in /home/.pusshead/username/mysite.com/rsscom.php on line 12

Warning: implode() [function.implode]: Bad arguments. in /home/.pusshead/username/mysite.com/rsscom.php on line 12

Line 12 in my code is: $content = implode ("", file ($yabbrssf));

Thanks for trying to help me out with this.


#9

What happens when you browse directly to http://yoursite.com/forum/YaBB.pl?action=rss - is there an error message?


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#10

If I browse to the link it gives me the xml RSS feed but with all of the tags etc… so the link is correct. I have had this working on other web hosts with the exact same script.


#11

Okay. This definitely works with my RSS feed:

<?php $doc_root = $_SERVER['DOCUMENT_ROOT']; $yabbrssf = "$doc_root/blog/rss.xml"; $content = implode("", file($yabbrssf)); preg_match_all("|<item>(.*)</item>|Uism",$content, $items, PREG_PATTERN_ORDER); for ($i=0;$i<count($items[1]);$i++) { preg_match_all("|<title>(.*)</title>(.*)<link>(.*)</link>|Uism",$items[1][$i], $regs, PREG_PATTERN_ORDER); echo "<a href='".$regs[3][0]."' target='_blank'>".$regs[1][0]."</a><br />\n"; } ?>(see example)

I was able to reproduce the same error as you by making the path incorrect.


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#12

the ?action=rss at the end doesn’t work for a filename. the if it were php, you could set $_GET[‘action’]=‘rss’; turn output buffering on, include the file (without ?action=rss), grab the output buffering content into a variable, then turn output buffering back off and continue with what you were going to do with the output of the perl script.

i think you’re better off using curl to access the output of that script, even though it’s on your website. my reason for saying that is that you need the output of the script, and the easiest way i know to get the output of a script from a web server is to use curl.

track7 - my dream-hosted site


#13

Yes, it is the ?action=rss that is stopping the whole script from working. So annoying! Ha ha.

I have no idea how to even begin writing this in curl, do you have any ideas? I’ve looked at some tutorials online but I’m finding it tough getting to grips with them.


#14

I’ve done this a number of times with curl and have started implementing it via some bastardized attempts at OO design.

It works great for me, and the only thing I have to do now is change the blog url in my configuration when i want to use this in a new site.

Yes there is a lot of stuff here, but it’s EVERYTHING I need to tie into WordPress and MovableType blogs. YMMV.

I’m willing to offer some input if it’s confusing, but it’s all here.

[code]*****************************************
FROM MY sysconfig.php file


$config->RSSurl = “http://birchtreehr.com/blog/?feed=rss2”;


FROM THE TOP OF MY BLOG RENDER PAGE


<?php $includePath = $_SERVER["DOCUMENT_ROOT"]; $sqlConn = "$includePath/com/sysConfig.php"; $sqlUser = "$includePath/com/qryUserUDF.php"; $sqlBlog = "$includePath/com/qryBlogUDF.php"; require_once($sqlConn); require_once($sqlUser); require_once($sqlBlog); include_once("$includePath/inc_rssfeed.php"); ?>

FROM THE BODY OF MY BLOG RENDER PAGE


<?php if ($blogErrCode == 0) { xml_parse_into_struct($xml_parser, $blogResponseText, $vals, $index); if (isset($rss_channel["ITEMS"])) { if (count($rss_channel["ITEMS"]) > 0) { for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) { ?>

<?php if (isset($rss_channel["ITEMS"][$i]["LINK"])) { print ("\n" . $rss_channel["ITEMS"][$i]["TITLE"] . ""); } else { print ("\n" . $rss_channel["ITEMS"][$i]["TITLE"] ); } ?> <?php $pubDateArr = explode(" ", $rss_channel["ITEMS"][$i]["PUBDATE"]); $dateStr = $pubDateArr[2]." ".$pubDateArr[1].", ".$pubDateArr[3]; print("$dateStr

"); print(substr($rss_channel["ITEMS"][$i]["DESCRIPTION"],0,$config->blogIntroCount)); if (isset($rss_channel["ITEMS"][$i]["LINK"])) { print ("\n

Click for more...
"); } ?> <?php } } else { print(" There are currently no entries
"); } } } else { print(" There are currently no entries
"); } ?>

CONTENTS OF inc_rssfeed.php


<?php $RSSurl = $config->RSSurl; set_time_limit(0); $rss_channel = array(); $currently_writing = ""; $main = ""; $item_counter = 0; $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, $RSSurl); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $blogResponseText = curl_exec($ch); $blogErrCode = curl_errno($ch); curl_close($ch); ?>

CONTENTS OF qryBlogUDF.php


<?php function startElement($parser, $name, $attrs) { global $rss_channel, $currently_writing, $main; switch($name) { case "RSS": case "RDF:RDF": case "ITEMS": $currently_writing = ""; break; case "CHANNEL": $main = "CHANNEL"; break; case "IMAGE": $main = "IMAGE"; $rss_channel["IMAGE"] = array(); break; case "ITEM": $main = "ITEMS"; break; default: $currently_writing = $name; break; } } function endElement($parser, $name) { global $rss_channel, $currently_writing, $item_counter; $currently_writing = ""; if ($name == "ITEM") { $item_counter++; } } function characterData($parser, $data) { global $rss_channel, $currently_writing, $main, $item_counter; if ($currently_writing != "") { switch($main) { case "CHANNEL": if (isset($rss_channel[$currently_writing])) { $rss_channel[$currently_writing] .= $data; } else { $rss_channel[$currently_writing] = $data; } break; case "IMAGE": if (isset($rss_channel[$main][$currently_writing])) { $rss_channel[$main][$currently_writing] .= $data; } else { $rss_channel[$main][$currently_writing] = $data; } break; case "ITEMS": if (isset($rss_channel[$main][$item_counter][$currently_writing])) { $rss_channel[$main][$item_counter][$currently_writing] .= $data; } else { $rss_channel[$main][$item_counter][$currently_writing] = $data; } break; } } } ?>

[/code]


#15

Here’s my version with cURL:

<?php $rss_url = "http://jessey.net/blog/rss.xml"; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $rss_url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5); $rss_file = curl_exec($ch); curl_close($ch); preg_match_all("|<item>(.*)</item>|Uism",$rss_file, $items, PREG_PATTERN_ORDER); for ($i=0;$i<count($items[1]);$i++) { preg_match_all("|<title>(.*)</title>(.*)<link>(.*)</link>|Uism",$items[1][$i], $regs, PREG_PATTERN_ORDER); echo "<a href='".$regs[3][0]."' target='_blank'>".$regs[1][0]."</a><br />\n"; } ?>(see example)

If it still doesn’t work for you, it is probably an issue with it being generated by Perl. I’ll need the actual URL of your feed if you want me to look into this further (I don’t know Perl). If you want to keep the URL private, you can PM it to me or something.


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#16

Dude, you have some serious indentation issues! Thanks for making a tedious horizontal scrollbar for us to play with :smiley:


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#17

I think that indentation is important for code readability and maintainability. My IDE is set up to render a tab with only 2 spaces instead of the standard 8. It doesn’t scroll for me, but maybe you just have a substandard resolution.

Feel free to just ignore the free code snippets. :wink:


#18

I agree. However, I usually indent two spaces, and four at the most. Now this thread (in flat mode) has a considerable horizontal scrollbar.

“Substandard” is a relative term that depends on what you consider “standard” to be. My default resolution is 1280 x 1024 on most of my PCs, which is considerably better than the average: May 2007 Global Screen Resolution Statistics, TheCounter.com. I still get the horizontal scrollbar with a horizontal resolution of 1600 pixels (this PC).


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#19

Thanks for the code! People’s opinions will always vary, on almost everything, and you will never please everyone in every way.

I would far rather have code snippets indented for ease of understanding than to nothave them indented…even if it means the occasional horizontal scroll. :wink:

Again, thanks for sharing your code, in any format you choose! It’s the sharing, and the code, that is important and valuable…the formatting “on the forum” is a non-issue with me (as I think it is with most others that value the content of your posts!) :slight_smile:

–rlparker


#20

My solutions had the code indented as well, but with no-scrollbar-goodness for most users. With me, you can eat your cake and have it too. I dunno what the fuss is about - didn’t anyone see my smiley?


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]