Làm cách nào để lấy tiêu đề từ phản hồi cURL PHP?

cURL là phần mềm mà bạn có thể sử dụng để thực hiện các yêu cầu khác nhau bằng các giao thức khác nhau. PHP có tùy chọn sử dụng cURL và trong bài viết này, chúng tôi sẽ hiển thị một số ví dụ

Khái niệm cơ bản về cURL PHP

curl_init();      // initializes a cURL session
curl_setopt();    // changes the cURL session behavior with options
curl_exec();      // executes the started cURL session
curl_close();     // closes the cURL session and deletes the variable made by curl_init();

PHP cURL POST Yêu cầu

Yêu cầu POST thường được thực hiện để gửi dữ liệu do người dùng thu thập đến máy chủ

Làm cách nào để lấy tiêu đề từ phản hồi cURL PHP?
Làm cách nào để lấy tiêu đề từ phản hồi cURL PHP?

 'foo',
    'secondFieldData' => 'bar'
);

$cURLConnection = curl_init('http://hostname.tld/api');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse - json_decode($apiResponse);

PHP cURL NHẬN Yêu cầu

Yêu cầu GET truy xuất dữ liệu từ máy chủ. Đây có thể là HTML của trang web, phản hồi API hoặc các tài nguyên khác

Tiêu đề PHP cURL

Bạn cũng có thể đặt tiêu đề tùy chỉnh trong yêu cầu cURL của mình. Đối với điều này, chúng ta sẽ sử dụng hàm curl_setopt()

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Header-Key: Header-Value',
    'Header-Key-2: Header-Value-2'
));

Kiểm tra các lệnh liên quan đến PHP khác và cách thực hiện phổ biến. Nhận tài khoản lưu trữ chia sẻ mới với chiết khấu lên tới 40% và thành thạo các kỹ năng PHP của bạn

If you use curl option CURLOPT_NOBODY = true to test if distant url is available, any sites can send you an http code 400 like Cdiscount Wsdl :

$ch = @curl_init($wsdl);

________số 8

@curl_setopt($ch, CURLOPT_HEADER         ,true);    // we want headers
@curl_setopt($ch, CURLOPT_NOBODY         ,true);    // dont need body
@curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);    // catch output (do NOT print!)

 'foo',
    'secondFieldData' => 'bar'
);

$cURLConnection = curl_init('http://hostname.tld/api');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse - json_decode($apiResponse);
0

 'foo',
    'secondFieldData' => 'bar'
);

$cURLConnection = curl_init('http://hostname.tld/api');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse - json_decode($apiResponse);
1

 'foo',
    'secondFieldData' => 'bar'
);

$cURLConnection = curl_init('http://hostname.tld/api');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse - json_decode($apiResponse);
2

 'foo',
    'secondFieldData' => 'bar'
);

$cURLConnection = curl_init('http://hostname.tld/api');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse - json_decode($apiResponse);
3

Với phương pháp

 'foo',
    'secondFieldData' => 'bar'
);

$cURLConnection = curl_init('http://hostname.tld/api');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse - json_decode($apiResponse);
4, khi
 'foo',
    'secondFieldData' => 'bar'
);

$cURLConnection = curl_init('http://hostname.tld/api');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse - json_decode($apiResponse);
5 được đặt thành true, thì
 'foo',
    'secondFieldData' => 'bar'
);

$cURLConnection = curl_init('http://hostname.tld/api');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse - json_decode($apiResponse);
6  sẽ xuất ra tiêu đề phản hồi. Tại thời điểm này, nếu
 'foo',
    'secondFieldData' => 'bar'
);

$cURLConnection = curl_init('http://hostname.tld/api');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse - json_decode($apiResponse);
7 được đặt thành false, thì
 'foo',
    'secondFieldData' => 'bar'
);

$cURLConnection = curl_init('http://hostname.tld/api');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse - json_decode($apiResponse);
4 sẽ trả về tiêu đề phản hồi và nội dung nội dung, nếu không thì chỉ trả về tiêu đề phản hồi

Chúng ta có thể sử dụng phương thức

 'foo',
    'secondFieldData' => 'bar'
);

$cURLConnection = curl_init('http://hostname.tld/api');
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

// $apiResponse - available data from the API request
$jsonArrayResponse - json_decode($apiResponse);
9 để trả về tổng kích thước của tất cả các tiêu đề nhận được

đầu ra

HTTP/1.1 200 OK
Date: Tue, 28 Apr 2020 15:11:27 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2020-04-28-15; expires=Thu, 28-May-2020 15:11:27 GMT; path=/; domain=.google.com; Secure
Set-Cookie: NID=203=DasuCttFxUVDirSokzwSf91r3PD60lDxlFogt2-rg5m0BCbhSeCpWcIlJVAmuLiDUkXmBTXuVbKEcP8gT0ifJzTu1MW-9UfriAyIqPESXl2H6fOsb9mvDZH8ng4Nb_YQk4Xv1uMFpCMiVf6GSHZS7dje2cjq1qvgyFiQfb3bOTA; expires=Wed, 28-Oct-2020 15:11:27 GMT; path=/; domain=.google.com; HttpOnly
Alt-Svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,h3-T050=":443"; ma=2592000
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked

Với tùy chọn

0, chúng ta có thể đặt chức năng gọi lại

đầu ra

Array
(
    [date] => Array
        (
            [0] => Tue, 28 Apr 2020 15:12:50 GMT
        )

    [expires] => Array
        (
            [0] => -1
        )

    [cache-control] => Array
        (
            [0] => private, max-age=0
        )

    [content-type] => Array
        (
            [0] => text/html; charset=ISO-8859-1
        )

    [p3p] => Array
        (
            [0] => CP="This is not a P3P policy! See g.co/p3phelp for more info."
        )

    [server] => Array
        (
            [0] => gws
        )

    [x-xss-protection] => Array
        (
            [0] => 0
        )

    [x-frame-options] => Array
        (
            [0] => SAMEORIGIN
        )

    [set-cookie] => Array
        (
            [0] => 1P_JAR=2020-04-28-15; expires=Thu, 28-May-2020 15:12:50 GMT; path=/; domain=.google.com; Secure
            [1] => NID=203=ZC5X9W1OFIFk7p_y1HUQ1ZhluIAq1QMJcoiaWNvjtggga9w0By1ULn01BaSxfswmYixQ8arShOwTpHMWyDRXu6vy5xdS19FmFYLyUsdz0n5wOs9_dkb4xBPLOc4SRKdZN7QhcgS8sMVwugrM-CEyg2ENJq_t__UJlwM2cgOdyfg; expires=Wed, 28-Oct-2020 15:12:50 GMT; path=/; domain=.google.com; HttpOnly
        )

    [alt-svc] => Array
        (
            [0] => quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,h3-T050=":443"; ma=2592000
        )

    [accept-ranges] => Array
        (
            [0] => none
        )

    [vary] => Array
        (
            [0] => Accept-Encoding
        )

    [transfer-encoding] => Array
        (
            [0] => chunked
        )

)

Làm cách nào để nhận các tiêu đề yêu cầu trong PHP?

Nhận được tiêu đề yêu cầu, máy chủ web sẽ gửi lại tiêu đề phản hồi HTTP cho máy khách. Đọc bất kỳ tiêu đề yêu cầu. Có thể đạt được điều này bằng cách sử dụng hàm getallheaders() . ví dụ 2. Nó có thể đạt được bằng cách sử dụng hàm apache_request_headers().

Làm cách nào để kiểm tra tiêu đề HTTP bằng cách sử dụng curl?

Bạn có thể sử dụng đối số dòng lệnh -v hoặc --verbose để xem tiêu đề yêu cầu . Điều này sẽ in rất nhiều thông tin gỡ lỗi về cách Curl thực hiện yêu cầu, bao gồm các tiêu đề HTTP được gửi đến máy chủ và nhận được từ máy chủ.

Làm cách nào để kiểm tra phản hồi curl trong PHP?

php $ch = curl_init($url);

Làm cách nào để đặt tiêu đề yêu cầu trong PHP curl?

Để gửi tiêu đề HTTP với yêu cầu Curl, bạn có thể sử dụng tùy chọn dòng lệnh -H và chuyển tên và giá trị tiêu đề vào "Key. định dạng giá trị" . Nếu bạn không cung cấp giá trị cho tiêu đề, điều này sẽ xóa tiêu đề tiêu chuẩn mà Curl sẽ gửi.