Configure PHP to Allow outgoing connections

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Configure PHP to Allow outgoing connections

Post by 3.14 »

How do I configure php to allow outgoing connections, and to use whatever proxies are necessary?
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: Configure PHP to Allow outgoing connections

Post by BDKR »

3.14 wrote:How do I configure php to allow outgoing connections, and to use whatever proxies are necessary?
Allowing outgoing connections isn't the kind of thing that PHP does. This is more likely a firewall thing. What is is that you need to do?

Cheers,
BDKR
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Post by 3.14 »

Code: Select all

<?php
<html>
<head>
<title>PHP Port Connection Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">

<p>This page simply attempts to connect to several well known servers. It only makes an initial
connection and then closes it, it doesn't send / receive any data. If they succeed, then your copy of
PHP is correctly configured to allow outgoing connections, and to use whatever proxies are
necessary. If they fail, then either all the hosts tested are down (not very likely) or your copy
of PHP is set to deny outgoing connections, or a proxy is in the way and PHP isn't configured to
handle it.</p>

<hr>
<?


$Connections = array("www.weather.com","www.yahoo.com","www.msn.com","www.cnet.com","www.aol.com");
$Port = 80; // http
$Timeout = 30; // seconds


$Failed = 0; $Success = 0;

foreach ($Connections as $Connection) {

  print "<p>Attempting to connect to <a href="http://$Connection" target="_blank">$Connection</a>...<br>";
  
  $socket = fsockopen ($Connection, $Port, $errno, $errstr, $Timeout);
  if ($socket == false) {
    $Failed++;
    print "<font color=red><b>Connection FAILED!</b></font><br>";
    print "Error $errno: $errstr";
    if ($errno == 0) {
      print "<br>The PHP manual says 'error 0 is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket.' (eg: you're not connected to the Net, invalid hostname, etc)<br>";
    }
    print "</p>\n";
  } else {
    $Success++;
    print "<font color=green>Success!</font></p>\n";
    fclose ($socket);
  }

}



?>
<hr>
<p><? print $Failed; ?> failed connections<br>
<? print $Success; ?> connections succeeded</p>
</body>
</html>
?>
When I run this script, it tells me "The PHP manual says 'error 0 is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket.' (eg: you're not connected to the Net, invalid hostname, etc)"

I want to set up my php initialise the socket or whatever it has to do in order to work.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Well this seems to be working for me. If there are any setting that don't allow PHP to connect to foreign addresses, it may be in the .ini file.

Cheers,
BDKR
Post Reply