# Thread: Error when using FOPEN to open URL

1. Hiya... i'm trying to test a PHP-based XML parser to read an RDF file (original RDF: http://www.freshmeat.net/backend/fm-releases.rdf) but whenever I try to open the URL in PHP I get the following errors:

Warning: fopen() [function.fopen]: php_hostconnect: connect failed in C:\web\beta\class.RDFParser.php on line 51

Warning: fopen(http://www.freshmeat.net/backend/fm-releases.rdf) [function.fopen]: failed to create stream: Bad file descriptor in C:\web\beta\class.RDFParser.php on line 51

I guess the second error is simply a result of the first, but I dont understand why the first occurs at all! I've used the same script on another server without problems... so perhaps it's something to do with this connection..? but i'm able to read the rdf file in my web browser here, so why can't PHP?

2.

3. Nevermind... the problem was due to the proxy.

FYI, if anyone else has a similar prob, you can retrieve a page line by line via a proxy, using the method described in a user comment on the page describing the FOPEN command on php.net... sample code follows:

Code:
$myfiles = "http://www.xyz.com/cgi-bin/plz_suche/search_or.cgi?or_plz=$PLZ&or_ort=".rawurlencode($stadt);$datei = fsockopen("proxy.wdf.sap-ag.de", 8080, &$errno, &$errstr);
if( !$datei ) { echo "proxy not available !"; fclose($resultfile);
exit();
} else {
fputs($datei,"GET$myfiles/ HTTP/1.0\n\n");
while (!feof($datei)) {$zeile = fgets($datei,1000); } }$myfiles is the page/file you're trying to reach
$zeile is the line by line src of the page 4. Thanks for the cool example .... set it up quiet easily only thing is our proxyserver adds a bunch of headers to the start of the stream ... any idea how I can strip them off ... The only thing I can see is that there is an empty line after the headers ... ie .. HTTP/1.0 200 OK Date: Fri, 30 Jul 2004 09:45:10 GMT Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) mod_python/2.7.8 Python/1.5.2 mod_ssl/2.8.12 OpenSSL/0.9.6b DAV/1.0.3 mod_perl/1.26 mod_throttle/3.1.2 Content-Type: text/vnd.wap.wml X-Pad: avoid browser bug X-Cache: MISS from cache2.za.itouchnet.net X-Cache: MISS from archive.prv.dev.itouchnet.net X-Cache: MISS from cache01.prv.za.itouchnet.net Proxy-Connection: close <?xml version="1.0"?> .... rest of xml doc ... Not sure how to do this .... 5. There are easier ways to read an RDF file. I'll have to sift through some old code to find the PHP code I slopped together last year to do this. 6. Ahha ... I may have a quick win here .... let me know if you see any issue with it ...$data = strstr ($data, "<?"); should strip the header off the file up till the opening tag declaring the xml header ... seems to be working fine as I ran this over the weekend monitoring some of our content feeds ... 7. The best way to remove headers is to loop through each line of the string until you reach a line which reads "/r/n/" as headers are terminated by a blank line in HTTP: PHP Code:  while ((!feof ($fhwnd)) && (fgets (\$fhwnd) != "\r\n"));