Home > Article > Backend Development > What is the difference between php socket and curl
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.
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
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 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.
<?php //初始化curl $ch = curl_init (); /* * 设置curl * php手册对于curl_setopt的解释为:设置对于curl传输的操作 * curl_setopt有三个参数:资源(一般为你建立的curl句柄)、操作(你将对这个句柄作何操作)、参数(对于这个操作你给出的参数) */ //例如你想对百度进行某些操作 curl_setopt ( $ch, CURLOPT_URL, "http://www.baidu.com"); //现在看来你要向百度post数据 curl_setopt ( $ch, CURLOPT_POST, 1 ); /*给出了要post的数据:$post_string,post的数据可以是一个文件, *那么你需要以@加上文件的全路径给出,或者你要post一些数据, *那么你可以按照数组形式给出,或者按照字符串给出, *如果你想按照字符串形式给出,请把字符串urlencode,嘿嘿 */ curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_string ); /* *把curl操作的结果以字符串形式 从curl_exec ()返回,而不是直接就输出了 */ curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ); //得到操作返回结果 $result = curl_exec ( $ch ); //关闭curl句柄 curl_close ( $ch );
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
<?php //初始化curl $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, "http://www.baidu.com"); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ); curl_exec($ch); $info = curl_getinfo($ch); var_dump($info);
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!