Hi

Ive got a problem with the fsockopen function. I want to simulate things
like Browser-Version (user_agent) to another php-Script and read its parsed
ScourceCode. Everything I read about that used fsockopen() as the main
function to access the file. So I tried this:

$fp = fsockopen ("http://www.homepage.de", 80);

Also with /index.html in the end and with many different URLs.
This way I always get the following two errors:

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses:
gethostbyname failed in D:\Programme\Apache2\htdocs\eigene\test\read.php on
line 3

Warning: fsockopen() [function.fsockopen]: unable to connect to
http://www.homepage.de:80 in
D:\Programme\Apache2\htdocs\eigene\test\read.php on line 3
$fp does not exist!

Ive already been told to leave out the [url]http://.[/url] But in that case i got:

Fatal error: Maximum execution time of 30 seconds exceeded in
D:\Programme\Apache2\htdocs\eigene\test\read.php on line 12

Which makes me wondering, because line 12 is the end of the script and not
the one with the fsockopen function...
My first thought was that I get timeouted, because i dont have any further
issues for the opened coonnection but the whole script is:

<?php
$fp = fsockopen ("http://www.homepage.de", 80);
if(!$fp) {
 echo "\$fp does not exist!";
 }
else {
 fgets($fp, 1024);
 }
?>

Now I do have an fgets-statement to recieve sth from the server. And still
he gets timeouted.

So why does this happen?! And what can I do against it?!
OS is: Win 98SE
PHP: 4.3.0
Apache: 2.0.43

allow_url_fopen in the php.ini is activated...

Thx for help
Xadian

    First of all: No http:// in the host argument passed to fsockopen, and no path information either. period.

    That's one problem less - the timeout error indicates that the connection is established.
    But it is just a connection, something that would allow data to be transferred.

    Thus, the server sits waiting for your script to request something via http protocol (or any other protocol it can handle).
    Your script sits there waiting for the server to return something without having asked anything.
    ... and still they sit ...

    To make a request, insert

    // this is where you would specify to user agent as well
    fputs($fp, "GET / HTTP/1.0\r\nHost: www.homepage.de\r\n\r\n"); 
    // or
    fputs($fp, "GET /path/to/file/filename.ext HTTP/1.0\r\nHost: www.homepage.de\r\n\r\n");
    

    before your call to fgets (and I'd recommend doing something with the return value of fgets as well, assign it to a variable or echo it).

      Now i gt some requests... and it opens most files i want to

      but:
      if i try open to recieve data from some php files it returns 0

      <?php
      
      function  sendToHost($host,$method,$path,$data,$useragent=0)
      {
          // Supply a default method of GET if the one passed was empty
          if (empty($method))
              $method = 'GET';
          $method = strtoupper($method);
          $fp = fsockopen($host,80);
          if ($method == 'GET')
              $path .= '?' . $data;
          fputs($fp, "$method $path HTTP/1.1\n");
          fputs($fp, "Host: $host\n");
          fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
          fputs($fp, "Content-length: " . strlen($data) . "\n");
          if ($useragent)
              fputs($fp, "User-Agent: MSIE\n");
          fputs($fp, "Connection: close\n\n");
          if ($method == 'POST')
              fputs($fp, $data);
      
      while (!feof($fp))
          $buf .= fgets($fp,128);
      fclose($fp);
      return $buf;
      }
      
      $output = sendToHost('212.227.248.211', 'GET', '/login.html', '', 'MSIE');
      
      echo $output;
      
      ?>
      

      for the login.html it works quite fine...
      but the login.php (which returns the same as the .html in browser) returns 0 in my script
      so i think there is sth in the php script that notices my script t be a script and not a browser...
      any suggestions what this could be and how to aviod it?!

      thx Xadian

        4 months later

        hi this is the exact same problem I'm having and I wonder if you've worked it out or not?

        A

          Write a Reply...