CSDN博客

img bjbs_270

fsockopen

发表于2004/11/4 10:37:00  5945人阅读

分类: PHP技术文章

fsockopen

(PHP 3, PHP 4 , PHP 5)

fsockopen --  Open Internet or Unix domain socket connection

Description

resource fsockopen ( string target, int port [, int errno [, string errstr [, float timeout]]])

Initiates a socket connection to the resource specified by target. PHP supports targets in the Internet and Unix domains as described in 附录 N. A list of supported transports can also be retrieved using stream_get_transports().

注: If you need to set a timeout for reading/writing data over the socket, use stream_set_timeout(), as the timeout parameter to fsockopen() only applies while connecting the socket.

As of PHP 4.3.0, if you have compiled in OpenSSL support, you may prefix the hostname with either 'ssl://' or 'tls://' to use an SSL or TLS client connection over TCP/IP to connect to the remote host.

fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()).

If the call fails, it will return FALSE and if the optional errno and errstr arguments are present they will be set to indicate the actual system level error that occurred in the system-level connect() call. If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket. Note that the errno and errstr arguments will always be passed by reference.

Depending on the environment, the Unix domain or the optional connect timeout may not be available.

The socket will by default be opened in blocking mode. You can switch it to non-blocking mode by using stream_set_blocking().

例子 1. fsockopen() Example

<?php
$fp
= fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!
$fp) {
   echo
"$errstr ($errno)<br />/n";
} else {
  
$out = "GET / HTTP/1.1/r/n";
  
$out .= "Host: www.example.com/r/n";
  
$out .= "Connection: Close/r/n/r/n";

  
fwrite($fp, $out);
   while (!
feof($fp)) {
       echo
fgets($fp, 128);
   }
  
fclose($fp);
}
?>
The example below shows how to retrieve the day and time from the UDP service "daytime" (port 13) in your own machine.

例子 2. Using UDP connection

<?php
$fp
= fsockopen("udp://127.0.0.1", 13, $errno, $errstr);
if (!
$fp) {
   echo
"ERROR: $errno - $errstr<br />/n";
} else {
  
fwrite($fp, "/n");
   echo
fread($fp, 26);
  
fclose($fp);
}
?>

警告

UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.

注: 当指定数字的 IPv6 地址(例如 fe80::1)时必须将 IP 地址放在方括号内。例如 tcp://[fe80::1]:80

注: The timeout parameter was introduced in PHP 3.0.9 and UDP support was added in PHP 4.

See also pfsockopen(), stream_set_blocking(), stream_set_timeout(), fgets(), fgetss(), fwrite(), fclose(), feof(), and the Curl extension.



add a note add a note User Contributed Notes
fsockopen
michiel at parse dot nl
25-Oct-2004 07:42
The following snippet allows you to retrieve the title oa page.

Great for rewriting auto-url detectors to display the actual title rather then http://...

<?
echo get_url_title("http://www.php.net/cal.php?id=409");

function
get_url_title($url, $timeout = 2)
{
  
$url = parse_url($url);

   if(!
in_array($url['scheme'],array('','http')))
       return;

  
$fp = fsockopen ($url['host'], ($url['port'] > 0 ? $url['port'] : 80), $errno, $errstr, $timeout);
   if (!
$fp)
   {
       return;
      
// echo "$errstr ($errno)<br>/n";
  
}
   else
   {
      
fputs ($fp, "GET /".$url['path'].($url['query'] ? '?'.$url['query'] : '')." HTTP/1.0/r/nHost: ".$url['host']."/r/n/r/n");
      
$d = '';
       while (!
feof($fp))
       {
          
$d .= fgets ($fp,2048);

           if(
preg_match('~(</head>|<body>|(<title>/s*(.*?)/s*</title>))~i', $d, $m))
               break;
       }
      
fclose ($fp);

       return
$m[3];
   }
}
?>
elemental21 at hotmail dot com
08-Oct-2004 10:23
Found this php class to use telnet from here:
http://cvs.adfinis.ch/cvs.php/phpStreamcast/telnet.class.php

There's no docs and a lot of it's in french though so maybe it will help
someone to have my working code. This code is used to telnet into a
pix and execute the "shun" command.

//-------telnet.class.php usage example---------
               $telnet = new telnet;
// Next line is for logging. 
//By default you need to create a folder called /log and give it the
//rights your webserver is running.
               $telnet->setLog(1,"mylog");
               $telnet->set_host("myhost.myplace.com");
//You need to set the prompt to what you know its going to be,
//then call wait_prompt...which waits for what you just set
               $telnet->set_prompt("Password: ");
               $telnet->connect();
               $telnet->wait_prompt();
               $telnet->write("mypassword");
//Have to change the prompt...in my example this is the
//prompt that a pix will change to after loggin in.
               $telnet->set_prompt("pix> ");
               $telnet->wait_prompt();
               $telnet->write("en");
               $telnet->set_prompt("Password: ");
               $telnet->wait_prompt();
               $telnet->write("enable_password");
//When you go into enable mode in a pix the prompt changes
               $telnet->set_prompt("pix# ");
               $telnet->wait_prompt();
               $telnet->write("shun " . $shun_address);
               $telnet->wait_prompt();
               $telnet->write("clear xlate");
               $telnet->wait_prompt();
               $telnet->write("write mem");
               $telnet->wait_prompt();
               $telnet->write("exit");
               $telnet->disconnect();
mikebNOSPAM at xamo dot com
30-Sep-2004 07:17
Hey, why were my comments regarding fsockopen connection timeouts taken out? I'm sure they would have been very useful to other users.

fsockopen (on FreeBSD and probably OpenBSD) will ignore the connection timeout parameter, and hang for several minutes if it can't connect for a variety of reasons (no DNS resolve, host down, extreme firewall setups). Use curl instead until a solution is found (i spent days on this issue)
ataynet at ataynet dot com
21-Aug-2004 04:01
Best url validator and parser function i have ever WRITE. ;)
Expression must be in a single line.

function explode_url($url) {
$temp["check"] = preg_match("/^(https{0,1}):////((([a-zA-Z0-9-]{1,})
(:([a-zA-Z0-9-]{0,}))@){0,1})(([a-z0-9-]{1,}/.){0,1}
([a-z0-9-]{1,}/.[a-z0-9-]{1,})(/.[a-z0-9-]{1,}){0,1})
((:([0-9]{1,})){0,1})((//([a-z0-9-]{1,}//){0,})(([a-z-0-9-]{1,}
(/.([a-z-0-9-]{1,})){0,}){0,1})((/?([^#?]{1,})){0,1})
((#([^#]{0,})){0,1})){0,1}$/", $url, $explode);
if ($temp["check"]) {
$temp["scheme"] = (!empty($explode[1])) ? $explode[1] : "";
$temp["user"] = (!empty($explode[4])) ? $explode[4] : "";
$temp["pass"] = (!empty($explode[6])) ? $explode[6] : "";
$temp["host"] = (!empty($explode[7])) ? $explode[7] : "";
$temp["port"] = (!empty($explode[13])) ? $explode[13] : "";
$temp["path"] = (!empty($explode[15])) ? $explode[15] : "";
$temp["basename"] = (!empty($explode[17])) ? $explode[17] : "";
$temp["extension"] = (!empty($explode[20])) ? $explode[20] : "";
$temp["query"] = (!empty($explode[23])) ? $explode[23] : "";
$temp["fragment"] = (!empty($explode[26])) ? $explode[26] : "";
}
return $temp;
}
bjorn (at) ahusonline dot com
21-May-2004 07:50
Modified code for telnet to work with not-so-fast connections.
The old version garbles the output and/or cuts the output before
it is finished when output is above about 100 lines.

<?php
# This is the difficult part, the Telnet header
$header1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).
chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).
chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).
chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);
$header2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);

# connecting
$fp=fsockopen("127.0.0.1",23);

# sending the Telnet header
fputs($fp,$header1);
usleep(125000);
fputs($fp,$header2);
usleep(125000);

# login
fputs($fp,"user/r");
usleep(125000);
fputs($fp,"users.pass/r");
usleep(125000);
# root looks nice
fputs($fp,"su/r");
usleep(125000); # takes some time, we had to wait
fputs($fp,"root.pass/r");

# some tests
fputs($fp,"ifconfig/r");       
fputs($fp,"echo year telnet php connect works|wall/r");

# we had to wait
usleep(125000);

# show the output
do                               

  
$output.=fread($fp, 80);    // read line by line, or at least small chunks
  
$stat=socket_get_status($fp);
}
while(
$stat["unread_bytes"]);
 
$output = str_replace("/n", "<br>", $output);
echo
$output;
fclose($fp);
?>
alex at renesource dot lv
17-Mar-2004 03:07
Set up SSL connection to server that requires client certificate:

Convert client certificate from *.pfx (pkcs12) into*.pem with openssl (if needed):

> openssl pkcs12 -in keys.pfx -out keys.pem

PHP:

<?php
$context
= stream_context_create();
$result = stream_context_set_option($context, 'ssl', 'local_cert', '/path/to/keys.pem');
$result = stream_context_set_option($context, 'ssl', 'passphrase', 'pass_to_access_keys');

$socket = fsockopen('ssl://'.$host, 443, $errno, $errstr, 30, $context);
?>
BIG_Nicky at clansnet dot de
22-Feb-2004 11:55
And here is some working code for a normal Telnet connection:

<?
# This is the difficult part, the Telnet header
$header1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).
chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).
chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).
chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);
$header2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);

# connecting
$fp=pfsockopen("127.0.0.1",23);

# sending the Telnet header
fputs($fp,$header1);
sleep(1);
fputs($fp,$header2);
sleep(1);

# login
fputs($fp,"user/r");
sleep(1);
fputs($fp,"users.pass/r");
sleep(1);
# root looks nice
fputs($fp,"su/r");
sleep(1); # takes some time, we had to wait
fputs($fp,"root.pass/r");

# some tests
fputs($fp,"ifconfig/r");       
fputs($fp,"echo year telnet php connect works|wall/r");

# we had to wait
sleep(1);

# show the output
#while (!feof($fp)) {
#$output = fgets($fp, 128)."<BR>/n";
#}
$output=fread($fp,128);
$stat=socket_get_status($fp);
$output.=fread($fp, $stat["unread_bytes"]);

# Uncomment these 3 lines to cut out the first line
#$output = explode("/n", $output);
#unset($output['0']);
#$output = implode("/n", $output);

$output = str_replace("/n", "<br>", $output);
echo
$output;
fclose($fp);
?>

The header part is the difficult thing, this is testet on win and linux, its working fine, you can also use usleep on your linux system, and a work around function on win systems instead of sleep, if you want to speed up you script. I hope I could help you! And sorry for my bad English
jack at jtr dot de
17-Feb-2004 06:05
Here is a function for testing a website/URI for availability:

<?php
  
/*
   * @return boolean
   * @param  string $link
   * @desc  躡erpr黤t die angegeben URL auf Erreichbarkeit (HTTP-Code: 200)
   */
  
function url_validate( $link )
   {       
      
$url_parts = @parse_url( $link );

       if ( empty(
$url_parts["host"] ) ) return( false );

       if ( !empty(
$url_parts["path"] ) )
       {
          
$documentpath = $url_parts["path"];
       }
       else
       {
          
$documentpath = "/";
       }

       if ( !empty(
$url_parts["query"] ) )
       {
          
$documentpath .= "?" . $url_parts["query"];
       }

      
$host = $url_parts["host"];
      
$port = $url_parts["port"];
      
// Now (HTTP-)GET $documentpath at $host";

      
if (empty( $port ) ) $port = "80";
      
$socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
       if (!
$socket)
       {
           return(
false);
       }
       else
       {
          
fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0/r/nHost: $host/r/n/r/n");
          
$http_response = fgets( $socket, 22 );
          
           if (
ereg("200 OK", $http_response, $regs ) )
           {
               return(
true);
              
fclose( $socket );
           } else
           {
//                echo "HTTP-Response: $http_response<br>";
              
return(false);
           }
       }
   }
?>
sir_reality2001 at yahoo dot com
14-Feb-2004 01:45
<?
// This is a modification to the script I submitted below.
// This script is an example of posting multiple files using
// fsockopen.
// The tricky part is making sure the HTTP headers and file boundaries are acceptable to the target webserver.
// This script is for example purposes only and could/should be improved upon.

$host='targethost';
$port=80;
$path='/test/socket/file_upload/receive_files.php';

// the file you want to upload
$file_array[0] = "dingoboy.gif"; // the file
$file_array[1] = "dingoboy2.gif"; // the file
$file_array[2] = "dingoboy3.gif"; // the file
$content_type = "image/gif"; // the file mime type
//$content_type = "text/plain";
//echo "file_array[0]:$file_array[0]<br><br>";

srand((double)microtime()*1000000);
$boundary = "---------------------------".substr(md5(rand(0,32000)),0,10);

$data = "--$boundary";

for(
$i=0;$i<count($file_array);$i++){
  
$content_file = join("", file($file_array[$i]));

  
$data.="
Content-Disposition: form-data; name=/"file"
.($i+1)."/"; filename=/"$file_array[$i]/"
Content-Type: $content_type

$content_file
--$boundary"
;

}

$data.="--/r/n/r/n";

$msg =
"POST $path HTTP/1.0
Content-Type: multipart/form-data; boundary=$boundary
Content-Length: "
.strlen($data)."/r/n/r/n";

$result="";

// open the connection
$f = fsockopen($host, $port);

fputs($f,$msg.$data);

// get the response
while (!feof($f)) $result .= fread($f,32000);

fclose($f);

?>
xbiron at citenet dot net
25-Jan-2004 03:42
Header to declare cookie :

<?php
/* [other headers] */
# Syntax => Cookie: name=value; name=value
# Never finish with ';'
$header.= "Cookie: name_cookie1=value_cookie1; name_cookie2=value_cookie2/r/n";
?>

Header to create cookie :

<?php
/* [other headers] */
# Syntax => Set-Cookie: name=value[; path=PATH][; expires=DATE][; domain=DOMAIN_NAME][; secure]
# DATE format : Day, dd-Mmm-yy hh:ii:ss GMT
# Never finish with ';'
$header.= "Set-Cookie: name_cookie=value_cookie; path=/; Friday, 13-Jan-03 12:00:00 GMT/r/n";
?>
rob at robhulme dot com
24-Jan-2004 01:17
Just a note to everyone who is using fsockopen and fread / fgets for a HTTP connection.

Unless you specify "Connection: Close" in your headers you will need to wait for the socket to time out before feof($streamPointer) to return true.

This has wasted 2 days of my time, grr!

-Rob
asalamanca at redcetus dot com
19-Nov-2003 03:27
This is a very fast program for test a form or link (many times).
<?php
$repeat 
= 100// How many times repeat the test

$timeout = 100// Max time for stablish the conection
$size    = 16// Bytes will be read (and display). 0 for read all

$server  = '64.246.30.37';            // IP address
$host    = 'www.foo.com';            // Domain name
$target  = '/poll/answer.asp';        // Specific program
$referer = 'http://www.test.com/';    // Referer
$port    = 80;

// Setup an array of fields to get with then create the get string
$gets = array ( 'get_field_1' => 'somevalue',
              
'get_field_2' => 'somevalue' );

// Setup an array of fields to post with then create the post string
$posts = array ( 'post_field_1' => 'somevalue',
                
'post_field_2' => 'somevalue' );

// That's all. Now the program proccess $repeat times

$method = "GET";
if (
is_array( $gets ) ) {
  
$getValues = '?';
   foreach(
$gets AS $name => $value ){
      
$getValues .= urlencode( $name ) . "=" . urlencode( $value ) . '&';
   }
  
$getValues = substr( $getValues, 0, -1 );
} else {
  
$getValues = '';
}

if (
is_array( $posts ) ) {
   foreach(
$posts AS $name => $value ){
      
$postValues .= urlencode( $name ) . "=" . urlencode( $value ) . '&';
   }
  
$postValues = substr( $postValues, 0, -1 );
  
$method = "POST";
} else {
  
$postValues = '';
}

$request  = "$method $target$getValues HTTP/1.1/r/n";
$request .= "Host: $host/r/n";
$request .= 'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) ';
$request .= "Gecko/20021204/r/n";
$request .= 'Accept: text/xml,application/xml,application/xhtml+xml,';
$request .= 'text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,';
$request .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1/r/n";
$request .= "Accept-Language: en-us, en;q=0.50/r/n";
$request .= "Accept-Encoding: gzip, deflate, compress;q=0.9/r/n";
$request .= "Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66/r/n";
$request .= "Keep-Alive: 300/r/n";
$request .= "Connection: keep-alive/r/n";
$request .= "Referer: $referer/r/n";
$request .= "Cache-Control: max-age=0/r/n";

if (
$method == "POST" ) {
  
$lenght = strlen( $postValues );
  
$request .= "Content-Type: application/x-www-form-urlencoded/r/n";
  
$request .= "Content-Length: $lenght/r/n";
  
$request .= "/r/n";
  
$request .= $postValues;
}

for (
$i = 0; $i < $repeat; $i++ ) {
  
$socket  = fsockopen( $server, $port, $errno, $errstr, $timeout );
  
fputs( $socket, $request );
   if (
$size > 0 ) {
      
$ret = fgets( $socket, $size );
   } else {
      
$ret = '';
       while ( !
feof( $socket ) ) {
          
$ret .= fgets( $socket, 4096 );
       }
   }
  
fclose( $socket );
   echo
"<hr> $i -- $content $ret";
}
?>

Alejandro Salamanca
RedCetus.com
terminal
24-Oct-2003 03:30
Try this.
Use AUTH when necessary.
Read RFC 821 when having problems.

<?php

   $handle
= smtp_connect($smtp_server, 25, 30, 1, 1, 1);
   echo
smtp_command($handle, "EHLO $domain/r/n", 1, 1);
   echo
smtp_command($handle, "MAIL FROM:<$from_mail>/r/n", 1, 1);
   echo
smtp_command($handle, "RCPT TO:<$to_mail>/r/n", 1, 1);
   echo
smtp_command($handle, "DATA/r/n", 1, 1);
   echo
smtp_command($handle, "$message/r/n./r/n", 1, 1);
  
// don't do it like this - it will hang up
   // echo smtp_command($handle, "$message", 1, 1);
   // echo smtp_command($handle, "/r/n./r/n", 1, 1);
  
echo smtp_command($handle, "QUIT/r/n", 1, 1);
  
smtp_close($handle);
  
  
   function
smtp_connect($host, $port, $timeout=30, $echo_command=False, $echo_response=False, $nl2br=False)
   {
      
$errno = 0;
      
$errstr = 0;
       if(
$echo_command)
       {
           if(
$nl2br) { echo nl2br("CONNECTING TO $host/r/n"); }
           else { echo
"CONNECTING TO $host/r/n"; }
       }
      
$handle = fsockopen($host, $port, $errno, $errstr, $timeout);
       if(!
$handle)
       {
           if(
$echo_command)
           {
               if(
$nl2br) { echo nl2br("CONNECTION FAILED/r/n"); }
               else { echo
"CONNECTION FAILED/r/n"; }
           }
           return
False;
       }
       if(
$echo_command)
       {
           if(
$nl2br) { echo nl2br("SUCCESS/r/n"); }
           else { echo
"SUCCESS/r/n"; }
       }
      
$response = fgets($handle,1);
      
$bytes_left = socket_get_status($handle);
       if (
$bytes_left > 0) { $response .= fread($handle, $bytes_left["unread_bytes"]); }
       if(
$echo_response)
       {
           if(
$nl2br) { echo nl2br($response); }
           else { echo
$response; }
       }
       return
$handle;
   }

   function
smtp_command($handle, $command, $echo_command=False, $nl2br=False)
   {
       if(
$echo_command)
       {
           if(
$nl2br) { echo nl2br($command); }
           else { echo
$command; }
       }
      
fputs($handle, $command);
      
$response = fgets($handle,1);
      
$bytes_left = socket_get_status($handle);
       if (
$bytes_left > 0) { $response .= fread($handle, $bytes_left["unread_bytes"]); }
       if(
$nl2br) { return nl2br($response); }
       else { return
$response; }
   }
  
   function
smtp_close($handle)
   {
      
fclose($handle);
   }
?>
richardaburton at hotmail dot com
19-Oct-2003 11:29
Improved HTTP/1.1 chunked transfer-encoding example.

The sample code given below by Jack does not function correctly when run against a recent version of Apache (I'm assuming that this did once work, but from the HTTP/1.1 spec I can only assume if it did work it was based mostly on luck).

<?php

$header
= "";
$response = "";

// connect
if (!($request=fsockopen('whatever.com',80,$errno,$errstr))) exit($errstr);
else {
  
socket_set_timeout($request,10);
  
// send request
  
fwrite($request,$post);
  
// get header
  
do $header.=fread($request,1); while (!preg_match('///r//n//r//n$/',$header));
  
// check for chunked encoding
  
if (preg_match('/Transfer//-Encoding://s+chunked//r//n/',$header))
     do {
        
$byte = "";
        
$chunk_size="";
         do {
          
$chunk_size.=$byte;
          
$byte=fread($request,1);
         } while (
$byte!="//r");      // till we match the CR
        
fread($request, 1);        // also drop off the LF
        
$chunk_size=hexdec($chunk_size); // convert to real number
        
$response.=fread($request,$chunk_size);
        
fread($request,2);          // ditch the CRLF that trails the chunk
    
} while ($chunk_size);        // till we reach the 0 length chunk (end marker)
  
else {
    
// check for specified content length
    
if (preg_match('/Content//-Length://s+([0-9]*)//r//n/',$header,$matches)) {
        
$response=fread($request,$matches[1]);
     } else {
        
// not a nice way to do it (may also result in extra CRLF which trails the real content???)
        
while (!feof($request)) $response .= fread($request, 4096);
     }
   }
  
// close connection
  
fclose($request);
}

// do something useful with the response
print($header);
print(
$response);

?>

Richard.
pulstar at ig dot com dot br
24-Sep-2003 02:24
To emulate a web browser with PHP using socket functions, there is a very good class to do this:

http://sourceforge.net/projects/snoopy/

Some of its features:
  
* easily fetch the contents of a web page
* easily fetch the text from a web page (strip html tags)
* easily fetch the the links from a web page (add a filter for "mailto:" links and you can easily fetch e-mail addresses).
* supports proxy hosts
* supports basic user/pass authentication
* supports setting user_agent, referer, cookies and header content
* supports browser redirects, and controlled depth of redirects
* expands fetched links to fully qualified URLs (default)
* easily submit form data and retrieve the results
* supports SSL and frames
joe at edwardsconsultants dot com
10-Aug-2003 05:56
just a quick note for users attempting https and thinking they must resort to curl or alternate methods -
you can use fsockopen, just read the docs closely.  basically they are saying to use 'ssl://' for a HTTPS (SSL) web request.

so this would work for authorize.net, and others; even for that paypal IPN - however I think it would be best to leave the site and deal with paypal's form:

$host = "somehost.somedomain.com";
$port = 443;
$path = "/the/url/path/file.php"; //or .dll, etc. for authnet, etc.

//you will need to setup an array of fields to post with
//then create the post string
$formdata = array ( "x_field" => "somevalue");
//build the post string
  foreach($formdata AS $key => $val){
   $poststring .= urlencode($key) . "=" . urlencode($val) . "&";
  }
// strip off trailing ampersand
$poststring = substr($poststring, 0, -1);

$fp = fsockopen("ssl://".$host, $port, $errno, $errstr, $timeout = 30);

if(!$fp){
 //error tell us
 echo "$errstr ($errno)/n";
  
}else{

  //send the server request
  fputs($fp, "POST $path HTTP/1.1/r/n");
  fputs($fp, "Host: $host/r/n");
  fputs($fp, "Content-type: application/x-www-form-urlencoded/r/n");
  fputs($fp, "Content-length: ".strlen($poststring)."/r/n");
  fputs($fp, "Connection: close/r/n/r/n");
  fputs($fp, $poststring . "/r/n/r/n");

  //loop through the response from the server
  while(!feof($fp)) {
   echo fgets($fp, 4096);
  }
  //close fp - we are done with it
  fclose($fp);
}
sergiopaternoster at tiscali dot it
31-Jul-2003 01:49
If you want to connect via Telnet, it could be useful to set also the Terminal Type (some OS requires it)

$IAC  = chr(255);  /* interpret as command: */
$SB = chr(250);    /* interpret as subnegotiation */
$SE = chr(240);    /* end sub negotiation */
$TELOPT_TTYPE = chr(24);    /* terminal type */
$TELQUAL_IS = chr(0);    /* Option is... */

//sending Terminal Type
fwrite($sock,$IAC.$SB.$TELOPT_TTYPE.$TELQUAL_IS.'vt100'.$IAC.$SE);

ciao
Sergio Paternoster
johnyu at revolutionhosting dot net
24-Jul-2003 05:45
It appears with PHP 4.3.2, on windows machines. fsockopen isn't able to use the ssl:// prefix even if you have the OpenSSL dll loaded in php.ini

On the otherhand, with PHP 4.5 CVS, it looks like SSL support for fsockopen is already compiled in... but it's missing the zip lib which I also need =(
brage (a t) jeffnappi (d.o.t) commie
10-Jul-2003 05:16
thought you guys may appreciate this function, allows you to pass an array of urls to download and does so simultaneously using non-blocking sockets, then returns the data in an array.

// function connects to an array of URLS at the same time
// and returns an array of results.

function multiHTTP ($urlArr) {
 $sockets = Array(); // socket array!
 $urlInfo = Array(); // info arr
 $retDone = Array();
 $retData = Array();
 $errno  = Array();
 $errstr  = Array();
 for ($x=0;$x<count($urlArr);$x++) {
  $urlInfo[$x] = parse_url($urlArr[$x]);
  $urlInfo[$x][port] = ($urlInfo[$x][port]) ? $urlInfo[$x][port] : 80;
  $urlInfo[$x][path] = ($urlInfo[$x][path]) ? $urlInfo[$x][path] : "/";
  $sockets[$x] = fsockopen($urlInfo[$x][host], $urlInfo[$x][port],
                           $errno[$x], $errstr[$x], 30);
  socket_set_blocking($sockets[$x],FALSE);
  $query = ($urlInfo[$x][query]) ? "?" . $urlInfo[$x][query] : "";
  fputs($sockets[$x],"GET " . $urlInfo[$x][path] . "$query HTTP/1.0/r/nHost: " .
       $urlInfo[$x][host] . "/r/n/r/n");
 }
 // ok read the data from each one
 $done = false;
 while (!$done) {
  for ($x=0; $x < count($urlArr);$x++) {
   if (!feof($sockets[$x])) {
   if ($retData[$x]) {
     $retData[$x] .= fgets($sockets[$x],128);
   } else {
     $retData[$x] = fgets($sockets[$x],128);
   }
   } else {
   $retDone[$x] = 1;
   }
  }
  $done = (array_sum($retDone) == count($urlArr));
 }
 return $retData;
}

# also if anyone has idea to improve this that would be wonderful
chris at music-server dot com
03-Jul-2003 09:53
I also needed a file upload via fsockopen and tried to work with Chris Snyders posting above:

========================
POST /path/to/script.php HTTP/1.0
Host: example.com
Content-type: multipart/form-data, boundary=AaB03x
Content-Length: $requestlen

--AaB03x
content-disposition: form-data; name="field1"

$field1
--AaB03x
content-disposition: form-data; name="field2"

$field2
--AaB03x
content-disposition: form-data; name="userfile"; filename="$filename"
Content-Type: $mimetype
Content-Transfer-Encoding: binary

$binarydata
--AaB03x--
==========================

I discovered some weird behaviour on my Linux server with PHP 4.0 and wanted to let you know :)

1) Lines needed to end with /r/n (else the last character of the value was cropped)
2) I needed to remove Content-Type and Content-Transfer-Encoding from the binary "userfile" - else the uploaded file contained these strings and was invalid!
3) The receiving script couldn't reference to the variables $field1 although register_globals was switched on! Needed to work with $HTTP_POST_VARS['field1'] and $HTTP_POST_FILES['userfile'] to get the values.

o.k., here's the script sending the header:

$f=fsockopen($server,80,$errno,$errstr,30);
fwrite($f,$header);
fclose($f);

The receiving script is as easy :) The file was uploaded to $HTTP_POST_FILES['userfile']['tmp_name'] and the variables were named as mentioned above.

Hope, it prevents spending another afternoon to figure out a file-upload this way :-)
xbensemhoun at t-systems dot fr
25-Jun-2003 05:48
To make a telnet connection with a Cisco router:

$cfgServer = "192.168.0.10";  //IP of your router
$cfgPort    = 23;                //port, 22 if SSH
$cfgTimeOut = 10;

$usenet = fsockopen($cfgServer, $cfgPort, &$errno, &$errstr, $cfgTimeOut);

if(!$usenet)
       {
       echo "Connexion failed/n";
       exit();
       }
else
       {
       echo "Connected/n<BR>";
       fputs ($usenet, "toto/r/n");
       fputs ($usenet, "en/r/n");
       fputs ($usenet, "tutu/r/n");
       fputs ($usenet, "exit/r/n");
       while (!feof($usenet))
               {
               echo ". ".fgets($usenet, 128)."<BR>/n";
               }
       }

Then you will have:
Connected
. ????
.
. User Access Verification
.
. Password:
. testXB>en
. Password:
. testXB#exit
.
blazely at removetoemail netspace net au
09-Jun-2003 12:55
Here's a quick function to establish a connection to a web server that will time out if the connection is lost after a user definable amount of time or if the server can't be reached.

Also supports Basic authentication if a username/password is specified. Any improvements or criticisms, please email me! :-)

Returns either a resource ID, an error code or 0 if the server can't be reached at all. Returns -1 in the event that something really wierd happens like a non-standard http response or something. Hope it helps someone.

Cheers,

Ben Blazely

function connectToURL($addr, $port, $path, $user="", $pass="", $timeout="30")
{
       $urlHandle = fsockopen($addr, $port, $errno, $errstr, $timeout);

       if ($urlHandle)
       {
               socket_set_timeout($urlHandle, $timeout);

               if ($path)
               {
                       $urlString = "GET $path HTTP/1.0/r/nHost: $addr/r/nConnection: Keep-Alive/r/nUser-Agent: MyURLGrabber/r/n";
                       if ($user)
                               $urlString .= "Authorization: Basic ".base64_encode("$user:$pass")."/r/n";
                       $urlString .= "/r/n";

                       fputs($urlHandle, $urlString);

                       $response = fgets($urlHandle);

                       if (substr_count($response, "200 OK") > 0)      // Check the status of the link
                       {
                               $endHeader = false;                    // Strip initial header information
                               while ( !$endHeader)
                               {
                                       if (fgets($urlHandle) == "/r/n")
                                               $endHeader = true;
                               }

                               return $urlHandle;                      // All OK, return the file handle
                       }
                       else if (strlen($response) < 15)                // Cope with wierd non standard responses
                       {
                               fclose($urlHandle);
                               return -1;
                       }
                       else                                            // Cope with a standard error response
                       {
                               fclose($urlHandle);
                               return substr($response,9,3);
                       }
               }

               return $urlHandle;
       }
       else
               return 0;
}
annettetruong at yahoo dot com
28-May-2003 07:23
I need to use Paypal IPN and can't use fsockopen() or cURL. I'm trying the following as a workaround to using fsockopen() which creates a hidden form that is automatically submitted upon page load, with the final verification step from Paypal caught in the iframe. I would appreciate comments on the pitfalls to this method.

About Paypal IPN:
http://www.paypal.com/cgi-bin/webscr?cmd=p/xcl/rec/ipn-intro-outside

Thanks, Annette.

<?php
  
// Create one additional form element called 'cmd'
  
$hiddenForm = "<input type='hidden' name='cmd' value='_notify-validate'>/n";

  
// Assemble page structure
  
$pageBody = "<body onLoad='document.all.myForm.submit();'>";
  
$pageBody .= "<h2>IPN Results</h2>";

  
// Look at values sent via POST.
   // Add to hidden form in same order.
  
foreach ($_POST as $key => $value) {
    
$hiddenForm .= "<input type='hidden' name='".$key."' value='".$value."'>/n";
    
// Display everything on the page for inspection.
    
echo $key.": ".$value."<br>/n";
   }
  
// Assemble Paypal IPN hidden form.
  
$hiddenForm = "<form name='myForm' method='POST' action='https://www.paypal.com/cgi-bin/webscr' target='myFrame'>/n".$hiddenForm."</form>/n<br>";

  
// Display helpful message about iframe.
  
$pageBody .= "The frame below will contain the IPN response: VERIFIED or INVALID.<br>/n";

  
// Create iframe to catch results of automatic
   // form submission. This frame should
   // display 'VERIFIED' or 'INVALID'. If it shows
   // a paypal page then something went wrong.
  
$iFrame = "<iframe name='myFrame'></iframe><br>/n";
  
$pageBody .= $hiddenForm;
  
$pageBody .= $iFrame;
  
$pageBody .= "</body></html>/n";

  
// Display page that contains automatic form
   // submission with embedded iframe.
  
echo $pageBody;

?>
Ignacio Sbampato
05-May-2003 01:49
You can know from which country are your visitors sending a query to ip-to-country online tool using code below:

****************************************

$ip = $REMOTE_ADDR;

$doc = "/get-country/?ip=" . $ip . "&user=guest&pass=guest";
$url = "ip-to-country.com";

$fp = fsockopen ($url, 80, $errno, $errstr, 30);
if (!$fp) {
   echo "$errstr ($errno)<br/>/n";
} else {
   fputs ($fp, "GET $doc HTTP/1.0/r/nHost: " . $url. "/r/n/r/n");
   while (!feof($fp)) {
       $httpresult = fgets ($fp,1024);
   }
   fclose ($fp);
}

if (isset($httpresult))
{
   echo "Ud. est?accediendo desde: <b>$httpresult</b>";
}

****************************************

NOTE: I had to modify the code provided by ip-to-country in its own Tools section because is buggy.
csnyder at chxo dot com
29-Mar-2003 11:04
Need to create a POST request that uploads a file?
I spent this afternoon trying to figure out what one looks like, and here is a template that has been successful for me:

========================
POST /path/to/script.php HTTP/1.0
Host: example.com
Content-type: multipart/form-data, boundary=AaB03x
Content-Length: $requestlen

--AaB03x
content-disposition: form-data; name="field1"

$field1
--AaB03x
content-disposition: form-data; name="field2"

$field2
--AaB03x
content-disposition: form-data; name="userfile"; filename="$filename"
Content-Type: $mimetype
Content-Transfer-Encoding: binary

$binarydata
--AaB03x--
==========================

Note that you have to generate the Content-Length header after you generate the body of the request (from the first --AaB03x down, including the file data), so the thing to do in practice is to build the body first, get the strlen(), and then build the headers onto the front of the body.

See RFC1867 - Form-based File Upload in HTML (http://www.faqs.org/rfcs/rfc1867.html) for more info on the format of "multipart/form-data" posts.
Good luck!
admin at php dot kz
28-Mar-2003 09:19
[quote]have seen in lot of places ppl asking on how to post data to another server using a php script...so here is what is required to do that...

//create a string with all the posted data...

foreach ($HTTP_POST_VARS as $key => $value) {
$value = urlencode(stripslashes($value));
 $req .= "&$key=$value";
}
.....
[/quote]

it is better to move '&' sign to the end of string

$req .= "$key=$value&";
nospam at phppatterns dot com
10-Mar-2003 04:20
The second half of the sample chapter (chptr 2) from Pro PHP Web Services is an in depth look at the HTTP protocol and how to build PHP clients for it.

http://www.wrox.com/books/sample-chapters/SampleChapter_1861008074.pdf [ free ]
matthijs at rohs dot nl
18-Feb-2003 06:38
Here's a function that finds the Server Software of a server and returns it. The timeout is at 1 second, but I would prefer, if you use this function many times on a page, to set it lower.

<?php
  
function get_server_software($domain) {
      
$fp = fsockopen($domain, 80, $errno, $errstr, 1);
      
       if (!
$fp) {
           return(
"");
       }
      
       else {
          
fputs($fp, "HEAD / HTTP/1.1/r/nHost: " . $domain . "/r/n/r/n");
          
           while (!
feof($fp)) {
               if (
preg_match("//bServer:/", $server = fgets($fp, 256))) {
                  
fclose($fp);
                   return(
substr($server, 8, -2));
               }
           }
          
          
fclose($fp);
       }
   }
?>
Sherif Gayed
30-Jan-2003 02:00
Here is how to connect to the web from behind a proxy server:
/*************start code**************/
/*your proxy server address*/
$proxy = "192.168.10.1";
/*your proxy server port*/
$port = 8080;
/*the url you want to connect to*/
$url = "http://www.php.net/";
$fp = fsockopen($proxy, $port);
fputs($fp, "GET $url HTTP/1.0/r/nHost: $proxy/r/n/r/n");
while(!feof($fp)){
  $line = fgets($fp, 4000);
  print($line);
}
fclose($fp);
/*************end code**************/
bart at mediawave dot nl
06-Jan-2003 07:44
Here is a good article on some of the differences between HTTP 1.0 and HTTP 1.1.

http://wireless.java.sun.com/midp/questions/chunking/
g dot bashi at ntlworld dot com
04-Jan-2003 01:55
The timeout parameter was not supported under windows until PHP 4.3.0, previously it was fixed at 30sec.
dan at lovepond dot com
18-Dec-2002 05:38
<?
$port
=4000;
$host="localhost";
$message="test";
$status=senddata($host,$port,$message);
print
"$status";

function
senddata($host,$port,$message) {

#takes in account servers that do not return EOF character
#send data to server and get back input

#function globals
$linenumber="2"; #amount of lines to get rid of before we give input
$lineamount="1"; #amount of lines to read after we give input

$fp = fsockopen("$host", $port, $errno, $errstr, 30);
if (!
$fp) {
   echo
"$errstr ($errno)";
}
else {
   for (
$i = 1; $i < $linenumber+1; $i++) {
    
fread ($fp,1);
    
$bytes_left = socket_get_status($fp);
     if (
$bytes_left > 0) { fread($fp, $bytes_left[unread_bytes]); }
   }
  
fputs($fp, "$message/r/n");
   for (
$i = 1; $i < $lineamount+1; $i++) {
    
$status.=fread($fp,1);
    
$bytes_left = socket_get_status($fp);
     if (
$bytes_left > 0) { $status.=fread($fp, $bytes_left[unread_bytes]); }
   }
  
fclose ($fp);
}

return
$status;
}

?>

Here is some code to help out abit more with the EOF problem.
I had a problem where I needed to strip out so many lines of server input to get back right data i wanted. UPDATE
verran at descent-rangers dot com
17-Oct-2002 10:28
I was tearing my hair out for a week trying to figure out how to do this.

If you use fsockopen with a service that doesn't have an EOF, or you try to read beyond EOF or line break, PHP can hang completely.

In my case, I was trying to write a class that talks to Kali servers (www.kali.net) to get a list of people on the chat server. To keep PHP from hanging due to the above, I discovered this:

   class kali_utils {
       function games_list($kali_server_ip, $kali_server_port) {
           $result = array();
           $fp = fsockopen($kali_server_ip, $kali_server_port, $errno, $error, 30);
           if (!$fp) {
               $result["errno"] = $errno;
               $result["error"] = $error;
           }
           else {
               fputs($fp, "KALIQ");
               $header = fread($fp, 5);
               $bytes_left = socket_get_status($fp);
               if ($bytes_left > 0) {
                   $result["results"] = fread($fp, $bytes_left["unread_bytes"]);
               }
               else {
                   $result["results"] = "";
               }
               fclose($fp);
           }
           return $result;
       }
   }

When I send the request packet, I get a response packet of length 5. Then I call socket_get_status() and use the unread_bytes key from it to know how far to fread from the socket. Works very good.

I've only used this on PHP 4.2.1 so far.
iain at monitormedia dot co dot uk
19-Jul-2002 08:48
Here's how to send an email using SMTP. This includes rudimentary checking on server responses during the process of sending an email. Could be improved by more comprehensive processing of the result codes...or going on to the next mail exchanger when you fail after connecting to the first.

<?

function another_mail($to,$subject,$headers,$message)
{
 
// Could get this from the php ini?
 
$from="me@here.com";
 list(
$me,$mydomain) = split("@",$from);

 
// Now look up the mail exchangers for the recipient
 
list($user,$domain) = split("@",$to,2);
 if(
getmxrr($domain,$mx,$weight) == 0)  return FALSE;

 
// Try them in order of lowest weight first
 
array_multisort($mx,$weight);
 
$success=0;

 foreach(
$mx as $host) {
 
// Open an SMTP connection
 
$connection = fsockopen ($host, 25, &$errno, &$errstr, 1);
  if (!
$connection)
   continue;
 
$res=fgets($connection,256);
  if(
substr($res,0,3) != "220") break;

 
// Introduce ourselves
 
fputs($connection, "HELO $mydomain/n");
 
$res=fgets($connection,256);
  if(
substr($res,0,3) != "250") break;

 
// Envelope from
 
fputs($connection, "MAIL FROM: $from/n");
 
$res=fgets($connection,256);
  if(
substr($res,0,3) != "250") break;

 
// Envelope to
 
fputs($connection, "RCPT TO: $to/n");
 
$res=fgets($connection,256);
  if(
substr($res,0,3) != "250") break;

 
// The message
 
fputs($connection, "DATA/n");
 
$res=fgets($connection,256);
  if(
substr($res,0,3) != "354") break;

 
// Send To:, From:, Subject:, other headers, blank line, message, and finish
  // with a period on its own line.
 
fputs($connection, "To: $to/nFrom: $from/nSubject: $subject/n$headers/n/n$message/n./n");
 
$res=fgets($connection,256);
  if(
substr($res,0,3) != "250") break;

 
// Say bye bye
 
fputs($connection,"QUIT/n");
 
$res=fgets($connection,256);
  if(
substr($res,0,3) != "221") break;

 
// It worked! So break out of the loop which tries all the mail exchangers.
 
$success=1;
  break;
 }
 
// Debug for if we fall over - uncomment as desired
 // print $success?"Mail sent":"Failure: $res/n";
 
if($connection) {
  if(
$success==0) fputs($connection, "QUIT/n");
 
fclose ($connection);
 }
 return
$success?TRUE:FALSE;
}

another_mail("recipient@some.domain","My Subject","X-mailer: PHP Script/nX-another-header: Whatever","Test email body./n/nNote if you actually put a period on a line/nby itself, the function will terminate prematurely./n/nYou will get a partial email sent though./n");
?>
t dot hodder at globalgold dot co dot uk
26-Jun-2002 11:52
You can post to a script that uses basic authentication if you create the
header like this and insert it into the relevant script above;

// Build the header
$header = "POST /path/to/script.cgi HTTP/1.0/r/nAuthorization: Basic ";
$header .= base64_encode("$username:$password") . "/r/n";
$header .= "Content-type: application/x-www-form-urlencoded/r/n";
$header .= "Content-length: " . strlen($request) . "/r/n";
$header .= "Connection: close/r/n/r/n";
info at free-dev dot com
01-Apr-2002 11:59
I'v seen how does a GET request works [sample 1] and a POST request works like that :

<?php
$fp
= fsockopen ("192.168.0.3", 80);
if (
$fp) {

 
fputs ($fp, "
POST /localhost/test/p.php HTTP/1.1
Host: 192.168.0.3
Content-Length: 7
Content-Type: application/x-www-form-urlencoded
Connection: Close

a=1&b=2

"
);
 while (!
feof($fp)) echo fgets ($fp,128);
 
fclose ($fp);
}
?>

Host: must be the same as the server else the POST isn't accepted
Content-lenght: is th lenght of the line of the values [a=1&b=2]
a=1&b=2 values are writted like a GET but without ? at beginning...

you can use POST and GET together with this line :
POST /localhost/test/p.php?val=3 HTTP/1.1

Have fun ! it's just 4 fun, dont try to hack ! Special thanks to GNOX : http://come.to/gnox
philip at cornado dot com
17-Mar-2002 07:14
An example to send POST data using fsockopen can be seen here:
http://www.faqts.com/knowledge_base/view.phtml/aid/12039/fid/51
Arif dot Budiman at no dot spam dot please
17-Jan-2002 12:43
This example queries Network Solutions whois database:

<?
$domain
= "arifbudiman.net";

if (
trim($domain) <> "") {
  
$domain = trim($domain);
  
$fp = fsockopen("whois.networksolutions.com", 43, $errno, $errstr, 30);
   if (!
$fp) {
     echo
"$errstr ($errno)";
   } else {
    
fputs($fp, "$domain/r/n");
     print
"<pre>/r/n";
     while (!
feof($fp)) {
         echo
fread($fp,128);
     }
     print
"</pre>";
    
fclose ($fp);
   }
}
?>
info at TAKETHISOUT dot ski-info-online dot com
08-Jan-2002 10:30
This is a "gotcha" that "got me" and discusses the careful use of HTTP/1.1 over HTTP/1.0
 
I had a script that suffered dreadful performance and return Hex values amongst the correct data. This was eventually traced to my inclusion of HTTP/1.1 in the line which read:

-- CODE (watch wrap) --
  $request = "GET $document" . "?" . "$query" . " HTTP/1.1/r/n";
  $request .= "Host: $myServer/r/n/r/n";
-- CODE --

By sending a HTTP/1.1 request it declares that it is willing to 'speak' HTTP/1.1, of course, but there are some aspects of HTTP/1.1 that make it necessary to handle the socket differently from HTTP/1.0.

In the RFC 2616, Section 3.6 defines:

[...]
All HTTP/1.1 applications MUST be able to receive and decode the "chunked" transfer-coding,
[...]

This was the cause of the extraneous HEX values being displayed.

Regards the loss of performance, this is also due to using HTTP/1.1, which defaults to having keepalive on until you tell it to close the connection, or it times out. Therefore the socket was being kept open by the script.

Simply changing the line in the script to HTTP/1.0 completely fixed the problem.

Speaking with one of the members in PHP-dev his words were:

[Comment from Hartmut Holzgraefe]
"I stumbled over the same 'chunked' problem not to long ago as a rule of thumb: never use HTTP/1.1 unless you really know that you have to, claiming to be a HTTP/1.0 client is no problem."

I have posted this as it was something I found very difficult to debug as there is actually nothing wrong with the script. This sort of problem often requires an in depth knowledge of an area that most developers would not have or consider. I would doubt that many, in any, who are reading this have ever read the HTTP RFC 2616 (I doubt also that it is a rivetting read :))  I hope this helps any future developers who are considering the use of high level socket connections with HTTP/1.1.
阅读全文
0 0

相关文章推荐

img
取 消
img