Page 1 of 1

fsockopen need help

Posted: Mon Nov 20, 2006 9:25 pm
by realtime158
So I am using php version 4.3.9 and lets say i have a file on the server called tester.php

so i am using fsockopen to point to that file but it gives me a error below.

$fh = @fsockopen("http://www.mysite.com/teter.php", "80", $errno, $errstr, 180);

The tester.php has full permissions

111 Connection refused

This server is linux Redhat Enterprise 2.6.9 with Apache 2.x

Is there anything i need to edit in the php.ini file or apache file.

Thanks for everyone that has helped in advance

Posted: Mon Nov 20, 2006 9:28 pm
by feyd
Give fsockopen() the domain only. You have to send the other details separately.

What specifically are you trying to accomplish?

can you give me a

Posted: Tue Nov 21, 2006 6:32 am
by realtime158
Can you give me a examlple

Thanks...

Posted: Tue Nov 21, 2006 7:23 am
by feyd
...no.

The page in the manual which was linked for you gives several.

thank i got some code but still problem

Posted: Tue Nov 21, 2006 8:19 am
by realtime158
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


The problem is i can caputre any url except this one http://www.fetchads.com/login/index.php  which is on our server.  If i change the url to something else it works anything.  Would there be something i need to modify on the server.  THe server specs are

Redhat Enterprise 2.6.9 with Apache 2.x and php version  4.3.9

Code: Select all

<?php
#usage:
$r = new HTTPRequest('http://www.fetchads.com/login/index.php');
echo $r->DownloadToString();

class HTTPRequest
{
   var $_fp;        // HTTP socket
   var $_url;        // full URL
   var $_host;        // HTTP host
   var $_protocol;    // protocol (HTTP/HTTPS)
   var $_uri;        // request URI
   var $_port;        // port
   
   // scan url
   function _scan_url()
   {
       $req = $this->_url;
       
       $pos = strpos($req, '://');
       $this->_protocol = strtolower(substr($req, 0, $pos));
       
       $req = substr($req, $pos+3);
       $pos = strpos($req, '/');
       if($pos === false)
           $pos = strlen($req);
       $host = substr($req, 0, $pos);
       
       if(strpos($host, ':') !== false)
       {
           list($this->_host, $this->_port) = explode(':', $host);
       }
       else 
       {
           $this->_host = $host;
           $this->_port = ($this->_protocol == 'https') ? 443 : 80;
       }
       
       $this->_uri = substr($req, $pos);
       if($this->_uri == '')
           $this->_uri = '/';
   }
   
   // constructor
   function HTTPRequest($url)
   {
       $this->_url = $url;
       $this->_scan_url();
   }
   
   // download URL to string
   function DownloadToString()
   {
       $crlf = "\r\n";
       
       // generate request
       $req = 'GET ' . $this->_uri . ' HTTP/1.0' . $crlf
           .    'Host: ' . $this->_host . $crlf
           .    $crlf;
       
       // fetch
       $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
       fwrite($this->_fp, $req);
       while(is_resource($this->_fp) && $this->_fp && !feof($this->_fp))
           $response .= fread($this->_fp, 1024);
       fclose($this->_fp);
       
       // split header and body
       $pos = strpos($response, $crlf . $crlf);
       if($pos === false)
           return($response);
       $header = substr($response, 0, $pos);
       $body = substr($response, $pos + 2 * strlen($crlf));
       
       // parse headers
       $headers = array();
       $lines = explode($crlf, $header);
       foreach($lines as $line)
           if(($pos = strpos($line, ':')) !== false)
               $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos+1));
       
       // redirection?
       if(isset($headers['location']))
       {
           $http = new HTTPRequest($headers['location']);
           return($http->DownloadToString($http));
       }
       else 
       {
           return($body);
       }
   }
}
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Nov 21, 2006 8:46 am
by feyd
Why are you trying to remotely fetch a page that's on your own server?

the reason for this is

Posted: Tue Nov 21, 2006 8:49 am
by realtime158
I am just working on a screen capture program that will capture the page on our server and email it to one of our clients. That is all

Listen Thanks for all your help by the way. Sometimes its good to get help from the experts. We use to have the ability to do this on our other hosting company with no problems but this is a new server that has been installed and i think there needs to be some settings changed that is all.

Let me know what can i do.

Posted: Tue Nov 21, 2006 8:51 am
by feyd
Have you tried using file_get_contents() or cURL?

i have tried

Posted: Tue Nov 21, 2006 9:09 am
by realtime158
I have tried cURL and no dice any other ideas?

here is the code

Posted: Tue Nov 21, 2006 9:59 am
by realtime158
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Works fine if i try google msn anything else but any php file on my local server

Code: Select all

<?php
// create a new curl resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.fetchads.com/login/index.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL, and return output
$output = curl_exec($ch);

// close curl resource, and free up system resources
curl_close($ch);

// Replace 'Google' with 'PHPit'
//$output = str_replace('Google', 'PHPit', $output);

// Print output
echo $output;
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]