PHP socket programming using the Fsockopen () function

Source: Internet
Author: User

A socket can be understood as a channel in which two computers communicate with one Another.

Usage: Use the Fsockopen () function

See the previous article for specific usage. The parameters for the function are url, port number, A variable that holds the error number, a variable that holds the error message string, and a timeout wait Time. (only The first parameter is Required)

Common Port Tables:

Port number

Main purpose

21st

Ftp

22

Ssh

23

Telnet

25

Smtp

80

Web

110

POP

Where several parts of the URL are: protocol name (scheme), host, port number (port), file path (path), and query Parameters.

When the URL is http://www.example.com/view.php?week=1#demo:

Index

Value

Scheme

http

Host

www.example.com

Port

80

User

Pass

Path

view.php

Query

Week=1

Fragment

#demo

Common HTTP status Codes:

Code

Meaning

200

Ok

204

NO Content

400

Bad Request

401

Unauthorized

50R

Forbidden

404

Not Found

408

Time Out

5**

Server Error

"example":

<?PHPfunctionCheck_url ($url){                //parsing URLs              $url _pieces=Parse_url($url); //set the correct path and port number              $path=(isset($url _pieces[' Path '])?$url _pieces[' Path ']: '/'; $port=(isset($url _pieces[' Port '])?$url _pieces[' Port ']: ' 80 '; //try connecting with Fsockopen ()              if($fp=Fsockopen($url _pieces[' Host '],$port,$errno,$errstr, 30)){                       //after successful establishment, write data to the server                     $send= "HEAD$pathHttp/1.1\r\n "; $send. = "HOST:$url _pieces[' host ']\r\n '; $send. = "connection:close\r\n\r\n"; fwrite($fp,$send); //Retrieving HTTP status codes                     $data=fgets($fp, 128); //Close Connection                     fclose($fp); //Return status Codes and class information                     List($response,$code) =Explode(‘ ‘,$data); if(code = = 200){                              return Array($code, ' good '); }Else{                              return Array($code, ' Bad ');//the second element of the array as the CSS class name                     }                }Else{                       //No connection                     return Array($errstr, ' bad '); }                        }         //Create a list of URLs       $urls=Array(                ' http://www.sdust.com ', ' http://www.example.com '         )         //to adjust the time limit for PHP scripts:       Set_time_limit(0);//Unlimited long time to complete tasks//validate URLs individually:       foreach($urls  as $url){                List($code,$class) = Check_url ($url); Echo"<p><a href =\"$url\ ">$url</a> (<span class =\ "$class\ ">$code</span>) </p> "; }  ?>

The advantage of using the Fsockopen () function is greater than the fopen () function: fopen () will only be used if Allow_url_fopen is set to true in php, and Fsockopen () is not limited.

PHP socket programming using the Fsockopen () function

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.