Table of Contents
First let’s talk about what a socket is:
CURL
Home Backend Development PHP Problem What is the difference between php socket and curl

What is the difference between php socket and curl

Oct 27, 2021 am 10:28 AM
curl php socket

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.

<?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 );
Copy after login

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);
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles