...

View Full Version : Opening a file on a remote server problem



bcarl314
09-08-2003, 02:36 PM
I'm trying to read an XML file on a remote server (for an RSS newsfeed) and I keep getting the following error:

Warning: main() [function.main]: php_hostconnect: connect failed in C:\web\testsite\testRSS.php on line 2

Here's the code:


<?php
include("http://aphion.com/index.php");
?>


I'm running on a Win2K system with php 4.3.0 and I am behind a firewall. This is for an intranet site, and I'm thinking it's blocked by the firewall, but I also seem to remember that the windows build of php does not allow remote file calls. So I'm not sure which is the problem. I can access outside the intranet from here, and I know the proxy I need to use to get outside the intranet, but I don't know how to use that proxy on the server to get through.

Any ideas?

mordred
09-08-2003, 03:17 PM
You could check if allow_url_fopen is set to 1, PHP prior to 4.3.0 could not access remote files by require() and include. Could be that your version still doesn't support them.

But watch out! What you have there is a huge security hole! :eek:
Any code included/required could contain arbitrary PHP code which would be executed in your script! Better use file(), fopen() or fsockopen() to fetch the content of the RSS feed and work on that instead!

bcarl314
09-08-2003, 04:36 PM
I checked, allow_url_fopen is set to "on", so that should be ok. and I just found out about the fsockopen deal, but I can't seem to get it to fly.

Here's my code:

[code]
<html>
<head>
<title> Current SitePoint Articles </title>
</head>
<body>
<h2>Currently on SitePoint.com...</h2>
<dl>
<?php

$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";

function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}

function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link;
if ($name == "ITEM") {
if(trim($title)!="Customize this feed") {
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link),htmlspecialchars(trim($title)));
printf("<dd>%s</dd>",htmlspecialchars(trim($description)));
}
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}

function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
}
}
}


$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$proxy = "proxyIP Here";
$port = "8080";
$url = "http://p.moreover.com/cgi-local/page?feed=251&o=rss"; //newsfeed
$fp = fsockopen($proxy, $port) or die("Unable to open");
fputs($fp, "GET $url HTTP/1.1\r\nHost: $proxy\r\n\r\n");
$i=0;
while($data = fread($fp, 4096)){
if($i > 7) {//this is done, because the first lines are http response info which I don't want.
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
$i++;
}
fclose($fp);
xml_parser_free($xml_parser);

?>
</dl>
</body>
</html>

[code]

I keep getting "XML Error: the document is not well-formed" errors. But if I take the results of the fsockopen and create an XML file and call it instead of the URL above, it works ok. Any ideas?

Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum