Trước khi tìm hiểu cách đăng dữ liệu biểu mẫu và tệp bằng cURL, tôi sẽ cho bạn biết, cURL trong PHP là gì và bạn sẽ sử dụng cURL trong tập lệnh PHP như thế nào
cURL là một thư viện và công cụ dòng lệnh được sử dụng để nhận và gửi tệp bằng cú pháp URL và trở thành hệ thống mạnh mẽ
cURL được phát hành vào năm 1997 và nó được gọi là "Xem URL" khi nó được phát hành
cURL hỗ trợ các giao thức khác nhau bao gồm HTTP, HTTPS, FTP, FTPS, TFTP, SCP, SFTP, LDAP, DAP, DICT, TELNET, FILE, IMAP, POP3, SMTP và RTSP
Hầu hết chúng tôi sử dụng cURL để truy cập hệ thống API của bên thứ ba như. gửi sms qua API bằng cURL
Ngày nay, các nhà phát triển gọi nó là "cuộn tròn trong PHP"
Các bước sử dụng cURL- Đầu tiên tôi khởi tạo phiên curl bằng cách sử dụng phương thức
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }
0 - Thứ hai, tôi đặt tùy chọn khác cho phiên cuộn tròn bằng cách sử dụng phương pháp
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }
1 và một số tùy chọn - Thứ ba, tôi thực hiện phiên cuộn tròn để nhận và gửi dữ liệu qua máy chủ bằng cách sử dụng
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }
2 - Thứ tư, tôi đóng phiên cuộn tròn bằng cách sử dụng
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }
3 mà tôi đã tạo - Bây giờ bạn có thể hiển thị dữ liệu phản hồi
Ở đây bạn sẽ thấy ví dụ cơ bản về cURL
- function getContent[$url, $postdata = false]
- {
- // check if cURL installed or not in your system?
- if [!function_exists['curl_init']]{
- return 'Sorry cURL is not installed!';
- }
- $ch = curl_init[];
- curl_setopt[$ch, CURLOPT_URL, $url];
- curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1];
- curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true];
- curl_setopt[$ch, CURLOPT_MAXREDIRS, 10];
- if [$postdata]
- {
- curl_setopt[$ch, CURLOPT_POST, 1];
- curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata];
- }
- curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false];
- curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"];
- $contents = curl_exec[$ch];
- $headers = curl_getinfo[$ch];
- curl_close[$ch];
- return array[$contents, $headers];
- }
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }
Bạn có thể gửi dữ liệu bài đăng để xác định tùy chọn
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }4 và dữ liệu sẽ được chuyển với tùy chọn
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }5
Bây giờ tôi sẽ cho bạn biết một số lệnh cURL để đăng dữ liệu biểu mẫu
Sử dụng
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }6 và thêm
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }7 cho mọi tham số mà bạn muốn thêm vào POST
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }0
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }1
Bây giờ trên máy chủ, bạn có thể nhận những dữ liệu này bằng cách sử dụng
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }8 và nếu bạn muốn xem dữ liệu để chắc chắn thì bạn có thể in biến
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }8 bằng cách sử dụng phương thức
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }00
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }5
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }6
Bạn có thể gửi tiêu đề hoặc loại dữ liệu cụ thể bằng cURL, sau đó sử dụng
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }01 để thêm tiêu đề
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }8
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }9
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }02 đang sử dụng để gửi dữ liệu thô
Bạn có thể gửi tệp bằng cURL nhưng đảm bảo rằng trong khi gửi tệp bằng cURL, bạn sẽ phải thêm
function getContent[$url, $postdata = false] { // check if cURL installed or not in your system? if [!function_exists['curl_init']]{ return 'Sorry cURL is not installed!'; } $ch = curl_init[]; curl_setopt[$ch, CURLOPT_URL, $url]; curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1]; curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, true]; curl_setopt[$ch, CURLOPT_MAXREDIRS, 10]; if [$postdata] { curl_setopt[$ch, CURLOPT_POST, 1]; curl_setopt[$ch, CURLOPT_POSTFIELDS, $postdata]; } curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 ;Windows NT 6.1; WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; Chrome/39.0.2171.95 Safari/537.36"]; $contents = curl_exec[$ch]; $headers = curl_getinfo[$ch]; curl_close[$ch]; return array[$contents, $headers]; }03 trước đường dẫn tệp