Forum Moderators: coopster

Message Too Old, No Replies

fsockopen problem

         

maliskoleather

2:48 am on Nov 9, 2006 (gmt 0)

10+ Year Member



Im using a script to load a XML file to be parsed... however the XML file itself is part of a cgi script which filters out useragents. I figured I could use fsockopen and set the useragent in the header. However when I tried this, all i get is "Fatal error: Maximum execution time of 30 seconds exceeded in..." :/

I'm not really experienced with using sockets.. so I dont know if its something im doing in the script or of the page itself is causing more problems. cURL isnt an option for me at the moment. *sigh* I've been messing with this for a week almost and cant seem to get it to work right.


<?php

$host = 'rs2.example.co.uk';
$service_uri = 'http://rs2.example.co.uk:8052/admin.cgi?pass=pass&mode=viewxml';

# compose HTTP request header
$header = "GET ".$service_uri." HTTP/1.0\r\n";
$header .= "Host: ".$host."\r\n";
$header .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20041013 Firefox/1.0.0\r\n";
$header .= "Connection: Close\r\n";

$fp = fsockopen($host, 8052, $errno, $errstr);
if (!$fp) {
echo "$errstr ($errno)<br/>\n";
echo $fp;
} else {
fputs($fp, $header);
$data = fgets($fp, 200);
fclose($fp);
}

if(isset($data)){
echo $data;
}

?>

note: i changed the password in the url... so if you try and pull it up directly, its not gonna work.

[edited by: coopster at 3:13 am (utc) on Nov. 9, 2006]
[edit reason] generalized url TOS [webmasterworld.com] [/edit]

coopster

3:17 am on Nov 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hi maliskoleather, welcome to WebmasterWorld.

The timeout error is from the script execution. I would start there I guess. Have a look at the set_time_limit [php.net] function and it's associated configuration directives.

mcavic

6:24 am on Nov 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The HTTP header needs to have a blank line after it. So:

$header .= "Connection: Close\r\n";
$header .= "\r\n";

Also, you need to use HTTP/1.1 instead of 1.0.

$header = "GET ".$service_uri." HTTP/1.1\r\n";

However, if the server uses chunked transfer encoding, the data you're reading will be garbled a bit, which is why cURL is much preferred.