Hướng dẫn PHP cURL chỉ ra cách làm việc với thư viện cURL trong PHP. cURL là một trình bao bọc trên thư viện libcurl
$ php -v php -v PHP 8.1.2 [cli] [built: Aug 8 2022 07:28:23] [NTS] ...
Chúng tôi sử dụng phiên bản PHP 8. 1. 2
curl_exec[$ch];5 là một công cụ dòng lệnh và thư viện để truyền dữ liệu bằng URL. Nó hỗ trợ nhiều giao thức bao gồm HTTP, HTTPS, FTP, GOPHER, MQTT hoặc SMTP.
curl_exec[$ch];6 là trình bao bọc PHP trên thư viện
cURL phải được cài đặt. Chẳng hạn, trên Debian tên gói là
curl_exec[$ch];7
PHP cURL NHẬN yêu cầu
Trong các ví dụ sau, chúng tôi tạo các yêu cầu GET đơn giản
In the example, we send a GET request to a small website. The output is directly shown in the standard output.
$ch = curl_init['//webcode.me'];Hàm
curl_exec[$ch];8 khởi tạo một phiên mới và trả về một điều khiển cURL để sử dụng vớicurl_exec[$ch];9,curl_close[$ch];0, . Chúng tôi cung cấp một URL mà chúng tôi đã gửi yêu cầucurl_exec[$ch];curl_close[$ch];0 thực thi phiên cURL đã chocurl_close[$ch];curl_close[$ch];1 đóng phiên cURL$ php get_req.php My html pageToday is a beautiful day. We go swimming and fishing.
Hello there. How are you?
Trong ví dụ tiếp theo, chúng tôi gửi đầu ra của quá trình truyền tới một biến
With the
curl_setopt
we set options for the cURL transfer. TheCURLOPT_RETURNTRANSFER
returns the transfer as a string of the return value ofcurl_exec
instead of outputting it directly.The
CURLOPT_FILE
option specifies where the transfer should be written to; the default is the standard output.In the example, we set
CURLOPT_FILE
option to a file handle, that we have created. With theCURLOPT_HEADER
, we disable the header.PHP cURL HEAD request
A HEAD request is a GET request without the body.
true, CURLOPT_NOBODY => true, CURLOPT_RETURNTRANSFER => true ]; curl_setopt_array[$ch, $options]; $data = curl_exec[$ch]; echo $data; curl_close[$ch];Để tạo yêu cầu HEAD, chúng tôi đặt
curl_close[$ch];4 thành true vàcurl_close[$ch];5 thành false. Chúng tôi đặt tất cả các tùy chọn cùng một lúc vớicurl_close[$ch];6$ php head_req.php HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Mon, 08 Feb 2021 16:00:24 GMT Content-Type: text/html Content-Length: 348 Last-Modified: Sat, 20 Jul 2019 11:49:25 GMT Connection: keep-alive ETag: "5d32ffc5-15c" Accept-Ranges: bytesVới chức năng
curl_close[$ch];7, chúng tôi nhận được thông tin liên quan đến chuyển khoản cụ thểWe send a HEAD reqeust to a website. After executing the request, we get the status by passing the
CURLINFO_RESPONSE_CODE
option to thecurl_getinfo
function.$ php status.php 200Biểu mẫu POST cURL PHP
Yêu cầu biểu mẫu POST đưa ra một POST tới URL đã chỉ định, với các khóa và giá trị của dữ liệu được mã hóa URL dưới dạng nội dung yêu cầu. Tiêu đề Loại nội dung được đặt thành ứng dụng/x-www-form-urlencoded. Dữ liệu được gửi trong phần thân của yêu cầu;
'John Doe', 'occupation' => 'gardener']; $options = [CURLOPT_POST => true, CURLOPT_POSTFIELDS => $fields, CURLOPT_RETURNTRANSFER => true]; curl_setopt_array[$ch, $options]; $data = curl_exec[$ch]; curl_close[$ch]; echo $data;Yêu cầu POST được đặt với tùy chọn
curl_close[$ch];8. Các trường POST được đặt với tùy chọncurl_close[$ch];9$ php post_form.php { "args": {}, "data": "", "files": {}, "form": { "name": "John Doe", "occupation": "gardener" }, "headers": { "Accept": "*/*", "Content-Length": "254", "Content-Type": "multipart/form-data; ... "Host": "httpbin.org", "X-Amzn-Trace-Id": "Root=1-602162bf-3d24fe793b7403de54ad250f" }, "json": null, ... "url": "//httpbin.org/post" }Trong ví dụ sau, chúng tôi POST dữ liệu JSON
In the example, we send a GET request to a small website. The output is directly shown in the standard output.
$ch = curl_init['//webcode.me'];0Chúng tôi mã hóa dữ liệu JSON bằng hàm
$ php get_req.php My html page0. Chúng tôi đặt tiêu đề phù hợp với tùy chọnToday is a beautiful day. We go swimming and fishing.
Hello there. How are you?
$ php get_req.php My html page1Today is a beautiful day. We go swimming and fishing.
Hello there. How are you?
In the example, we send a GET request to a small website. The output is directly shown in the standard output.
$ch = curl_init['//webcode.me'];1PHP cURL nhiều yêu cầu không đồng bộ
Hàm
$ php get_req.php My html page2 tạo một đa xử lý mới, cho phép xử lý nhiều xử lý cURL một cách không đồng bộToday is a beautiful day. We go swimming and fishing.
Hello there. How are you?
In the example, we send a GET request to a small website. The output is directly shown in the standard output.
$ch = curl_init['//webcode.me'];2Trong ví dụ này, chúng tôi tạo yêu cầu không đồng bộ cho bốn trang web. Chúng tôi in mã trạng thái và tiêu đề của họ
In the example, we send a GET request to a small website. The output is directly shown in the standard output.
$ch = curl_init['//webcode.me'];3Chúng tôi bắt đầu xử lý đa
In the example, we send a GET request to a small website. The output is directly shown in the standard output.
$ch = curl_init['//webcode.me'];4Chúng tôi tạo các bộ điều khiển tiêu chuẩn cho từng URL và thêm chúng vào bộ điều khiển đa năng với
$ php get_req.php My html page3Today is a beautiful day. We go swimming and fishing.
Hello there. How are you?
In the example, we send a GET request to a small website. The output is directly shown in the standard output.
$ch = curl_init['//webcode.me'];5Chúng tôi thực hiện tất cả các truy vấn không đồng bộ và tiếp tục khi tất cả đã hoàn tất
In the example, we send a GET request to a small website. The output is directly shown in the standard output.
$ch = curl_init['//webcode.me'];6Chúng tôi đóng tay cầm
In the example, we send a GET request to a small website. The output is directly shown in the standard output.
$ch = curl_init['//webcode.me'];7Chúng tôi nhận được mã trạng thái
In the example, we send a GET request to a small website. The output is directly shown in the standard output.
$ch = curl_init['//webcode.me'];8Chúng tôi nhận được các tiêu đề
In the example, we send a GET request to a small website. The output is directly shown in the standard output.
$ch = curl_init['//webcode.me'];9Chúng tôi xây dựng một yêu cầu tùy chỉnh với tùy chọn
$ php get_req.php My html page4Today is a beautiful day. We go swimming and fishing.
Hello there. How are you?
curl_exec[$ch];0Ví dụ gửi email đến máy tính trong mạng cục bộ
curl_exec[$ch];1$ php get_req.php My html page5 là tên của máy tính chạy máy chủ email trong mạng LANToday is a beautiful day. We go swimming and fishing.
Hello there. How are you?
curl_exec[$ch];2Chúng tôi chỉ định số cổng với
$ php get_req.php My html page6.Today is a beautiful day. We go swimming and fishing.
Hello there. How are you?
$ php get_req.php My html page7 dịch các dòng Unix mới thànhToday is a beautiful day. We go swimming and fishing.
Hello there. How are you?
$ php get_req.php My html page8,Today is a beautiful day. We go swimming and fishing.
Hello there. How are you?
Làm cách nào để gửi yêu cầu cuộn tròn trong PHP?
PHP cURL NHẬN yêu cầu . // mã web. tôi']; . Đầu ra được hiển thị trực tiếp trong đầu ra tiêu chuẩn. php $ch = curl_init['//webcode.me']; curl_exec[$ch]; curl_close[$ch]; In the example, we send a GET request to a small website. The output is directly shown in the standard output.PHP curl trả về cái gì?
Giá trị trả về ¶ . Tuy nhiên, nếu tùy chọn CURLOPT_RETURNTRANSFER được đặt, nó sẽ trả về kết quả nếu thành công, sai nếu thất bại. Hàm này có thể trả về giá trị Boolean false nhưng cũng có thể trả về giá trị không phải Boolean được đánh giá là false. true on success or false on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .Làm cách nào để sử dụng API cuộn tròn trong PHP?
// tạo và khởi tạo một phiên curl $curl = curl_init[]; . thí dụ. com"]; // trả lại quá trình truyền dưới dạng một chuỗi, cũng với setopt[] curl_setopt[$curl, CURLOPT_RETURNTRANSFER, 1]; // curl_exec[] thực thi phiên curl đã bắt đầu // $output chứaCurl_init[] trong PHP là gì?
Hàm curl_init[] sẽ khởi tạo một phiên mới và trả về một điều khiển cURL . hàm curl_exec[$ch] nên được gọi sau khi khởi tạo phiên cURL và tất cả các tùy chọn cho phiên được đặt. Mục đích của nó chỉ đơn giản là thực thi phiên CURL được xác định trước [được cung cấp bởi ch].