Home>Article>Backend Development> What is the difference between php socket and curl

What is the difference between php socket and curl

藏色散人
藏色散人 Original
2021-10-27 10:28:12 2067browse

The difference between socket and curl: 1. Socket is a data structure that can be used to communicate between the server and the client; 2. Curl uses URL syntax to transfer files and data. , supports many protocols, such as FTP, HTTP, TELNET, etc.

What is the difference between php socket and curl

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

What is the difference between php socket and curl?

The difference between curl and socket in PHP

First let’s talk about what a socket is:

PHP uses berkely’s socket library To create his connection, the socket is a data result. You can use this socket to open a session between the server and the client. The server is always in a listening state. When a client connects to the server, it opens a port that the server is listening on for a session. At this time, the server receives the client's connection request and then performs a cycle. Now the client can send information to the server, and the server can send information to the client.

To generate a socket, you need a total of three variables:

1, a protocol
2, a socket type
3, a public protocol type

The following It is a detailed explanation of these three variables, give it a rough understanding (I hope you can remember to chuckle)
Protocol: There are three protocols to choose from when generating a socket:

1, AF_INET This is used A relatively widespread protocol for generating sockets, using tcp or udp protocol transmission, using ipv4 address
2, AF_INET6 Obviously, the same as above, the difference is that ipv6 address
3, AF_UNIX is used on a unix or Linux machine, this Rarely used, only used on Unix or Linux systems where both the server and client are Unix or Linux systems.

Socket type:
1. SOCK_STREAM This protocol is a sequential, reliable, data-complete byte stream-based connection. This is the most commonly used socket type. This socket uses TCP for transmission.
2. SOCK_DGRAM This protocol is a connectionless, fixed-length transfer call. This protocol is unreliable and uses UDP for its connections.
3. SOCK_SEQPACKET This protocol is a dual-line, reliable connection that sends fixed-length data packets for transmission. This packet must be accepted completely before it can be read.
4. SOCK_RAW This socket type provides single network access. This socket type uses the ICMP public protocol. (ping and traceroute use this protocol)
5. SOCK_RDM This type is rarely used and is not implemented on most operating systems. It is provided for the data link layer and does not guarantee the order of data packets

Public protocol type:
1. ICMP (Internet Control Message Protocol) Internet Control Message Protocol, mainly used on gateways and hosts to detect network conditions and report error messages
2. TCP ( Transmission Control Protocol) Transmission Control Protocol, which is the most widely used protocol, can ensure that the data packet reaches the receiver. If an error occurs midway, then this protocol will resend the data packet.
3. UDP (User Datagram Protocol) User Datagram Protocol, which is a connectionless and unreliable data transmission protocol.

Okay, you now know that generating a socket requires three elements, so socket_create() in php requires three parameters, a protocol, a socket type, and a public protocol. If the creation is successful, socket_create() returns a socket resource type. If it is unsuccessful, hey, then you will receive a false.

CURL

cURL uses URL syntax to transfer files and Data tools. It supports HTTP, FTP, and TELNET.

Why use cURL?

Because, if we sometimes want to flexibly obtain the content on the web page, such as processing cookies, verification, form submission, file upload, etc. Then you need to use cURL. It is said that PHP has a powerful cURL library (because I can't tell where the power is, so I use "it is said" lol).

The basic steps for php to use cURL options are as follows:
1. Initialization
2. Parameter setting
3. Page content acquisition or operation
4. Release handle

Look at the simple example below.


      

Because PHP curl has many operations, it is probably very difficult to remember them all. Anyway, I can’t remember them all. Let’s talk about some things that everyone may use.

Get some information about the server


      

Through the above example, you will get the following information:

"url" //Resource network address
"content_type" //Content encoding
"http_code" //HTTP status code
"header_size" //Header size
"request_size" / /Requested size
"filetime" //File creation time
"ssl_verify_result" //SSL verification result
"redirect_count" //Jump technology
"total_time" //Total time spent
"namelookup_time" //DNS query time consumption
"connect_time" //Waiting for connection time consumption
"pretransfer_time" //Preparation time before transmission
"size_upload" //Size of uploaded data
"size_download" //Size of download data
"speed_download" //Download speed
"speed_upload" //Upload speed
"download_content_length" //Length of download content
"upload_content_length" //Upload The length of the content
"starttransfer_time" //The time to start the transfer
"redirect_time" //The redirection time

Using curl, you can also do the following operations:

1. Simulate the post operation of the page
2.File upload
3.HTTP authentication
4.FTP upload
5.FQ technique
6.Callback function
ps:
About the above The main premise of curl is that --with-curlwrappers was added when your PHP was installed and compiled. You can use the phpinfo() operation to check whether you added this extension library during compilation.
If this extension is loaded, you will see something like:

If you do not have this extension, you need to change the php.ini file and remove extension=php_curl. The semicolon in front of dll.

Okay, now we know what socket and curl are about. Socket is a data structure that can be used to communicate between the server and the client. Curl uses URL syntax rules to transfer files and data, and supports many protocols, such as FTP, HTTP, TELNET, etc.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the difference between php socket and curl. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn