Hướng dẫn content-disposition: form-data php curl

I'm getting confuse about this Instagram cURL in converting it into PHP see below:

   curl -F 'client_id=CLIENT_ID' \
    -F 'client_secret=CLIENT_SECRET' \
    -F 'grant_type=authorization_code' \
    -F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
    -F 'code=CODE' \
    https://api.instagram.com/oauth/access_token  

here is my current code with an error

{
  "error_type": "OAuthException",
  "code": 400,
  "error_message": "You must provide a client_id"
}

current code with sample value:

$client_id = "7d60c47cdwe249069cdb07a0f28e19cb";
$client_secret = "de5awwe3ebf1443d8dcf0fbc88e5ab1b";
$grant_type = "authorization_code";
$redirect_uri = "http://sexample.net/create-code.php";
$code ="y23d86c19b4c3b0a8b561e64c93f2b3f";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    //format URL to grant access to email and sitename passed
    $data  = '{"client_id":"'.$client_id.'","client_secret":"'.$client_secret.'","grant_type":"'.$grant_type.'","redirect_uri":"'.$redirect_uri.'","code":"'.$code.'"}';
    curl_setopt($ch, CURLOPT_URL, 'https://api.instagram.com/oauth/access_token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    //execute cURL call and get template data
    $output = curl_exec($ch);
    curl_close($ch);
    echo $output;

Hướng dẫn content-disposition: form-data php curl

  • 1. File form.php
  • 2. File curl.php
  • 3. File upload.php
  • Lời kết

Qua 4 bài vừa rồi bạn thấy CURL khá là hay phải không nào? Thật ra thì học tới bài này là bạn đã rành nó rồi đấy, nhưng mình muốn đưa ra thật nhiều ví dụ nên trong bài này chúng ta sẽ xây dựng chức năng upload file với PHP CURL nhé.

Để upload file trong php chúng ta thực hiện ba thao tác:

  • Tạo file upload.php để upload file
  • Tạo file curl.php sử dụng CURL để gọi đến file upload.php
  • Tạo file form.php trong đó có một form upload  và  có thuộc tính  enctype="multipart/form-data" và action của nó trỏ đến file curl.php

Để rõ hơn các bạn xem sơ đồ dưới đây:

Hướng dẫn content-disposition: form-data php curl

Nhìn vào sơ đồ bạn thấy hơi khác so với thông thường phải không nào, thông thường thì chúng ta chỉ cần 2 file thôi đó là file form.phpupload.php. Nhưng ở đây chúng ta đang sử cụng PHP CURL để upload file nên phải thông qua quy trình như sơ đồ vậy.

1. File form.php

<html> <head> <title>File Upload Using PHP and cURL - freetuts.net</title> </head> <body> <form action="curl.php" method="post" enctype="multipart/form-data"> <table border="1"> <tr> <td>Upload</td> <td><input name="file" type="file" id="file"/></td> </tr> <tr> <td>&nbsp;</td> <td><input name="btnUpload" type="submit" value="Upload" /></td> </tr> </table> </form> </body> </html>

Code language: HTML, XML (xml)

2. File curl.php

// Nếu submit form if (isset($_POST['btnUpload'])) { // Lấy thông tin file upload $filename = $_FILES['file']['name']; $filedata = $_FILES['file']['tmp_name']; $filesize = $_FILES['file']['size']; // Nếu file OK if ($filedata != '') { $headers = array("Content-Type:multipart/form-data"); // Đối với filedata phải có ký hiệu @ ở trước $postfields = array("filedata" => "@$filedata", "filename" => $filename); // Khởi tạo CURL // URL trỏ đến file upload.php $ch = curl_init('http://localhost/2_develop/tour/upload.php'); // Cấu hình có sử dụng header // Vì chúng ta đang gửi file nên header của nó // phải ở dạng Content-Type:multipart/form-data curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // Cấu hình sử dụng method POST curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); // Thiết lập có gửi file và thông tin file curl_setopt($ch, CURLOPT_INFILESIZE, $filesize); // Cấu hình return curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Thực thi curl_exec($ch); // Nếu không tồn tại lỗi nào trong CURL if(!curl_errno($ch)) { $info = curl_getinfo($ch); if ($info['http_code'] == 200){ echo 'Upload thành công'; } } else { echo curl_error($ch); } // Đóng CURL curl_close($ch); } else { echo 'Bạn chưa chọn file để upload'; } }

Code language: PHP (php)

3. File upload.php

// Đường dẫn upload $uploadpath = "upload/"; // Nhận thông tin $filedata = $_FILES['filedata']['tmp_name']; $filename = $_POST['filename']; if ($filedata != '' && $filename != ''){ // Dùng hàm copy để lưu vào thay vì hàm move_upload_file như thông thường copy($filedata,$uploadpath.$filename); }

Code language: PHP (php)

Trong file này các bạn thấy sự bất thường đó là chúng ta sử dụng hàm copy chứ không phải hàm move_upload_file nhé.

Lời kết

Các bạn chạy file form.php lên và thực hiện upload thử xem có được không, hy vọng là được. Cũng khá là đơn giản phải không nào, bài này chỉ mang tính chất học tập, làm bài tập để rành thư viện CURL  hơn thôi,  chúc các bạn học tốt nhé.

Nguồn: https://freetuts.net/su-dung-php-curl-de-upload-file-231.html