PHP Functions: Fsockopen Introduction

Source: Internet
Author: User

Open the Socket link for your network.
Syntax: Resuce fsockopen (string hostname, int port, int [errno], string[errstr], int [timeout]);
return value: resource;
Function type: network system;
Content Description:
At present, this function provides two Socket data flow interface, respectively, for Internet use Af_inet and Unix Af_unix.

In the Internet, the parameters hostname and port respectively represent the URL and port number.
In Unix, the parameter hostname represents the path to the socket, and port is configured to 0. Timeout can be omitted to indicate how long no connection
On the interrupt. The function returns a file pointer for use by the file function, including fgets (), FGETSS (), fputs (), fclose ()
Feof (). Parameters errno and ERRSTR can be omitted and used for error handling. This function is handled using the blocking pattern (blocking mode),
The set_socket_blocking () can be converted into nonblocking mode.

Instance:
<?php
$FP =fsockopen ("Php.wilson.gs", & $errno,& $errstr, 10);
if (! $fp) {
echo "$errstr ($errno) <br>\n";
} else{
Fputs ($fp, "get/http/1.0\nhost:php.wilson.gs\n\n");
while (!feof ($fp)) {
Echo fgets ($FP, 128);
}
Fclose ($FP);
}
?>
Note: the fputs () function writes to the file (which is safe for binary files). The fputs () function is an alias for the fwrite () function.

Fsockopen ()-open Internet or Unix domain socketconnection

First, how to disable Fsockopen ()
The following are two common ways to disable Fsockopen.
1, modify php.ini, will disable_functions = after adding fsockopen
2. Modify PHP.ini, change allow_url_fopen = on to Allow_url_fopen = Off

Second, how to solve the Fsockopen function is disabled
1. If the server does not disable Pfsockopen at the same time, replace the Fsockopen function with Pfsockopen directly.
What to do: Search for string Fsockopen in the program (Replace with Pfsockopen (. Examples such as the following
Before modification:
$fp = fsockopen($host, 80, $errno, $errstr, 30);

Modified:
$fp  = pfsockopen ($host, 80,  $errno,  $errstr,  30);
2, if the server is also disabled pfsockopen, then use other functions instead, such as stream_socket_client (). Note: The parameters for Stream_socket_client () and Fsockopen () are different.
What to do: Search the program for string  fsockopen (  Replace with  stream_socket_client (, then, the port parameter "80" in the original Fsockopen function is deleted and added to the $host. Example below
Modified:
$fp  = fsockopen ($host, 80,  $errno,  $errstr,  30);

Modified
$fp  = stream_socket_client ($host. " ",  $errno,  $errstr,  30);
3, if the PHP version is less than 5.0,fsockopen is disabled, and no stream_socket_client () What to do? Write a function to implement Fsockopen function, reference code:
function b_fsockopen ($host,  $port, & $errno, & $errstr,  $timeout)  {
  $ip  = gethostbyname ($host);
  $s  = socket_create (af_inet, sock_stream, 0);
 if  (Socket_set_nonblock ($s))  {
   $r  =  @socket_connect ($s,  $ip,   $port);
  if  ($r  | |  socket_last_error ()  == einprogress)  {
    $errno  = einprogress ;
   return  $s;
  }
 }
  $errno  = socket_last_error ($s);
  $ERRSTR  = socket_strerror ($errno);
 socket_close ($s);
 return false;
}

Operation: 1. First find the code snippet that uses the Fsockopen function, add the above code to its upper end, and search for the string fsockopen in the snippet (replace with B_fsockopen (.
2. Because the Fsockopen function returns a file pointer so it can be manipulated by the file function, but this b_fsockopen function does not return the file pointer, you need to continue to modify the code snippet: with Socket_read (replace Fread (, with Socket_write ( Replace the fwrite (, with Socket_close (replace the fclose (.

PHP Functions: Fsockopen Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.