Home > Article > Backend Development > Detailed explanation of how to use PHP fsockopen_PHP tutorial
There is also a function starting with curl_, which can implement many functions. Do some research when you have time! The following is an introduction to fscokopen
1. PHP fsockopen function description:
Open Internet or Unix domain socket connection (open socket link )
Initiates a socket connection to the resource specified by target .
fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets() , fgetss( ), fwrite(), fclose(), and feof() ). It returns a file handle
Open PHP fsockopen function
PHP fsockopen requires the allow_url_fopen option in PHP.ini to be turned on.
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute">fp</span><span> = </span><span class="attribute-value">fsockopen</span><span>("www.example.com",<br> 80, $errno, $errstr, 30); </span></span></li> <li><span>if (!$fp) { </span></li> <li class="alt"> <span>echo "$errstr ($errno)</span><span class="tag"><</span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span>n"; </span> </li> <li><span>} else { </span></li> <li class="alt"> <span>$</span><span class="attribute">out</span><span> = </span><span class="attribute-value">"GET / HTTP/1.1rn"</span><span>; </span> </li> <li> <span>$out </span><span class="attribute">.</span><span>= </span><span class="attribute-value">"Host: www.example.comrn"</span><span>; </span> </li> <li class="alt"> <span>$out </span><span class="attribute">.</span><span>= </span><span class="attribute-value">"Connection: Closernrn"</span><span>; </span> </li> <li><span> </span></li> <li class="alt"><span>fwrite($fp, $out); </span></li> <li><span>while (!feof($fp)) { </span></li> <li class="alt"><span>echo fgets($fp, 128); </span></li> <li><span>} </span></li> <li class="alt"><span>fclose($fp); </span></li> <li><span>} </span></li> </ol>
The above is the specific method of using the PHP fsockopen function for your reference and study.