Hướng dẫn php curl headers browser - trình duyệt tiêu đề php curl

Trong bài trước mình hướng dẫn các bạn sử dụng CURL trong php để làm một số công việc như lấy nội dung từ trang web hoặc gửi POST data tới URL.

Bài ngày hôm nay các bạn sẽ học cách sử dụng header để điều khiển nội dung của web. Thiết lập headers trong CURL bằng thuộc tính

$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;
5 với hàm
$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;
6. Danh sách Headers được chứa trong mảng , xem ví dụ minh sau:

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
 
$request_headers = array();
$request_headers[] = 'User-Agent: '. $User_Agent;
$request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
curl_setopt($ch,CURLOPT_HTTPHEADER,$request_headers);

Trong ví dụ trên chúng ta thiết lập 2 thông số header

$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;
7 và
$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;
8. Và bạn cũng có thể lưu cookie trong header.

$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;

Để thiết lập cookie chúng mình có option

$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;
9.

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);

Muốn xem thông tin header của CURL bạn thiết lập cho thuộc tính

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
0.

curl_setopt($ch, CURLOPT_HEADER, 1);

Show Curl POST Request Headers

$information = curl_getinfo($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpcode == 200)
{
    return true;
}
return false;

Nếu bạn thích bài viết này, hãy ủng hộ chúng tôi bằng cách đăng ký nhận bài viết mới ở bên dưới và đừng quên chia sẻ kiến thức này với bạn bè của bạn nhé. Bạn cũng có thể theo dõi blog này trên Twitter và Facebook

Ở phần này bạn sẽ tìm hiểu cURL là gì? Cách thiết lập cURL để gửi đi một yêu cầu Request, sử dụng cURL GET, cURL POST và cURL POST dữ liệu JSON

cCurl PHP

cURL là một thư viện dùng để truyền dữ liệu trên mạng bằng một số giao thức như HTTP, FTP, HTTPS, POP3 ... . Nó ra đời từ năm 1997 viết bởi Daniel Stenberg, cơ bản nó là một thư viện viết bằng C. Trong hệ Linux nó có giao diện dòng lệnh, có thể sử dụng ngay đó là lệnh

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
1, ví dụ có thể tải về https://xuanthulab.net bằng lệnh là một thư viện dùng để truyền dữ liệu trên mạng bằng một số giao thức như HTTP, FTP, HTTPS, POP3 ... . Nó ra đời từ năm 1997 viết bởi Daniel Stenberg, cơ bản nó là một thư viện viết bằng C. Trong hệ Linux nó có giao diện dòng lệnh, có thể sử dụng ngay đó là lệnh
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
1, ví dụ có thể tải về https://xuanthulab.net bằng lệnh

curl https://xuanthulab.net

Trong PHP có cung cấp các API để làm việc với thư viện này với các chức năng khá đầy đủ, đó là thư viện: Client URL Library, để sử dụng được các hàm Server cần có thư viện libcurl (mặc định có sẵn).

Bằng việc sử dụng cURL với PHP ta có thể truyền dữ liệu qua vài giao thức và hoạt động rất mạnh mẽ. Ví dụ gửi dữ liệu qua các website, xác thực oAuth, gọi API ...cURL với PHP ta có thể truyền dữ liệu qua vài giao thức và hoạt động rất mạnh mẽ. Ví dụ gửi dữ liệu qua các website, xác thực oAuth, gọi API ...

Cơ bản về sử dụng cURL PHP

Trước khi gửi đi một yêu cầu nào đó (request), chúng ta cần tạo ra đối tượng cURL. Thật đơn giản chỉ việc gọi hàm curl_init($url), hàm này trả về một tài nguyên cURL. Trong đó tham số

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
2 là địa chỉ gửi yêu cầu (tất nhiên có thể thiết lập lại sau khi đối tượng được tạo) cURL. Thật đơn giản chỉ việc gọi hàm curl_init($url), hàm này trả về một tài nguyên cURL. Trong đó tham số
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
2 là địa chỉ gửi yêu cầu (tất nhiên có thể thiết lập lại sau khi đối tượng được tạo)

Ví dụ

$curl = curl_init(); //url = null

Thiết lập cURL

Ngay sau khi có được tài nguyên cURL bạn cần gán các thiết lập cho nó với hàm curl_setopt, cú pháp như sau:cURL bạn cần gán các thiết lập cho nó với hàm curl_setopt, cú pháp như sau:

bool curl_setopt(resource $resource_cURL,int $option, $value)

Trong đó $option là số nguyên chỉ ra loại thiết lập và $value là giá trị thiết lập, $resource_cURL là tài nguyên cURL tạo ra ban đầu. Dưới đây là danh sách các $option quan trọng nhất$option là số nguyên chỉ ra loại thiết lập và $value là giá trị thiết lập, $resource_cURL là tài nguyên cURL tạo ra ban đầu. Dưới đây là danh sách các $option quan trọng nhất

Thiết lậpVí dụ/Diễn giải
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
3
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
4 để khi gửi yêu cầu bằng hàm
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
5 trả về chuỗi chứ không xuất thẳng ra màn hình.
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
6
Thời gian cho phép cURL cố gắng kết nối (giây), 0 vô tận
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
7
Nội dung của dòng User-Agent: trong header của HTTP khi gửi yêu cầu User-Agent: trong header của HTTP khi gửi yêu cầu
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
8
URL mà cCURL gửi yêu cầu
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
9
TRUE - Thiết lập yêu cẩu gửi theo phương thức POST, kiểu POST application/x-www-form-urlencoded sử dụng POST FORM thông thường trong HTML POST, kiểu POST application/x-www-form-urlencoded sử dụng POST FORM thông thường trong HTML
curl_setopt($ch, CURLOPT_HEADER, 1);
0
Mảng dữ liệu để POST

Ngoài ra còn vô số thiết lập khác, có thể tham khảo: curl-setopt

Ví dụ

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://xuanthulab.net/apitest/');

Thiết lập cURLcurl_setopt_array() ví dụ:

//Dữ liệu để POST
$data = array(
    'name' = 'foo',
    'data' = array(1,2,3,4),
    'value' = 'bar'
);

//Có thể phải dùng thêm hàm chuyển tham số mảng thành chuỗi URL encode
//$data = http_build_query($data) nếu POST

$curl = curl_init();
$options =array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://xuanthulab.net/apitest/',
    CURLOPT_POST => true,
    CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
    CURLOPT_POSTFIELDS => $data
 );
curl_setopt_array($curl, $options);

Ngay sau khi có được tài nguyên cURL bạn cần gán các thiết lập cho nó với hàm curl_setopt, cú pháp như sau:

Trong đó $option là số nguyên chỉ ra loại thiết lập và $value là giá trị thiết lập, $resource_cURL là tài nguyên cURL tạo ra ban đầu. Dưới đây là danh sách các $option quan trọng nhấtcurl_exec(). Kết quả trả về

curl_setopt($ch, CURLOPT_HEADER, 1);
1 nếu thất bại,
curl_setopt($ch, CURLOPT_HEADER, 1);
2 nếu thành công hoặc giá trị là chuỗi trả về khi CURLOPT_RETURNTRANSFER thiết lập true.

$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;
0

Thiết lập

Ví dụ/Diễn giải

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
3 curl_close($curl);

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
6

$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;
1

Thời gian cho phép cURL cố gắng kết nối (giây), 0 vô tận

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
7

$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;
2

Nội dung của dòng User-Agent: trong header của HTTP khi gửi yêu cầu

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
8

  • URL mà cCURL gửi yêu cầu json_encode
  • $User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
    $cookies = 'CookieName1=Value;CookieName2=Value';
    curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
    curl_setopt($ch, CURLOPT_COOKIE, $cookies);
    9
  • TRUE - Thiết lập yêu cẩu gửi theo phương thức POST, kiểu POST application/x-www-form-urlencoded sử dụng POST FORM thông thường trong HTML

Ví dụ

$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;
3

Thiết lập cURL

Ngay sau khi có được tài nguyên cURL bạn cần gán các thiết lập cho nó với hàm curl_setopt, cú pháp như sau:

Trước tiên bạn vào Google Cloud Console tạo một ứng dụng riêng để truy cập các dịch vụ Google.

Sau đó vào Google URL Shortener bấm vào GET KEY để lấy key truy cập dịch vụ này.

Dịch vụ Google URL Shortener đã bị dừng cung cấp bởi Google, nên thời điểm này nó không còn hoạt động, tuy nhiên ý nghĩa code bạn vẫn có thể tham khảo

Đoạn mã đầy đủ

$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;
4