Hướng dẫn how to pass form data in curl php? - làm cách nào để chuyển dữ liệu biểu mẫu trong curl php?

Tôi có lệnh sau, sử dụng tùy chọn-Form/-f, mà tôi biết là đang hoạt động:

curl  --form "file=@/home/USERNAME/import.csv" https://apiprovider.com/api/v0/imports\?token\=[KEY]

Tôi cần chạy lệnh này thông qua PHP, nhưng tôi gặp rắc rối, có lẽ là với dữ liệu tệp biểu mẫu. Tôi đã thử những điều sau đây, tuy nhiên lặp lại hoặc var_dumping kết quả dường như không hiển thị, chỉ là một trang trống hoặc một chuỗi trống.

'@'.$file_name_with_full_path);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_VERBOSE,true);
$result=curl_exec ($ch);
curl_close ($ch);
var_dump($result);?>

Làm thế nào tôi có thể nhận được lệnh này hoạt động trong PHP?

Hỏi ngày 17 tháng 10 năm 2017 lúc 15:21Oct 17, 2017 at 15:21

Hướng dẫn how to pass form data in curl php? - làm cách nào để chuyển dữ liệu biểu mẫu trong curl php?

KinsdotnetkinsdotnetKinsDotNet

1.5185 huy hiệu vàng24 Huy hiệu bạc49 Huy hiệu đồng5 gold badges24 silver badges49 bronze badges

3

Vì không có câu trả lời nào cho đến nay (ít nhất là không phải với một cách tiếp cận sẽ hoạt động trong PHP 5.6+), đây là: mã PHP curl_ tương đương sẽ là:

$ch = curl_init ( 'https://apiprovider.com/api/v0/imports?token=[KEY]' );
curl_setopt_array ( $ch, array (
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => array (
                'file' => new CURLFile ( '/home/USERNAME/import.csv' ) 
        ) 
) );
curl_exec ( $ch );

.

Đã trả lời ngày 22 tháng 10 năm 2017 lúc 10:04Oct 22, 2017 at 10:04

Hanshenrikhanshenrikhanshenrik

18.3k3 Huy hiệu vàng39 Huy hiệu bạc79 Huy hiệu đồng3 gold badges39 silver badges79 bronze badges

thử cái này:

'@'.$file_name_with_full_path);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_VERBOSE,true);
$result = curl_exec ($ch);

$curlresponse = json_decode($result, true);

var_dump($curlresponse);
?>

Đã trả lời ngày 20 tháng 10 năm 2017 lúc 9:22Oct 20, 2017 at 9:22

Hướng dẫn how to pass form data in curl php? - làm cách nào để chuyển dữ liệu biểu mẫu trong curl php?

Jelle Woordjelle Woordjelle woord

1832 Huy hiệu bạc16 Huy hiệu đồng2 silver badges16 bronze badges

0

Một cách là sử dụng CURLFile thay vì @ cho phiên bản PHP trên 5.5

$target_url = 'https://apiprovider.com/api/v0/imports?token=[KEY]'
$file_name_with_full_path ='/home/USERNAME/import.csv';
// Create a CURLFile object
$cfile = new CURLFile($file_name_with_full_path,mime_content_type($file_name_with_full_path),'imported_csv');    
// Assign POST data
$post = array('imported_csv_file' => $cfile);    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_VERBOSE,true);
$result=curl_exec ($ch);
curl_close ($ch);
var_dump($result);

Đã trả lời ngày 20 tháng 10 năm 2017 lúc 14:41Oct 20, 2017 at 14:41

2

Có lẽ một cái gì đó như thế này có thể hoạt động:

'@'.$file_name_with_full_path);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$target_url);
curl_setopt($curl, CURLOPT_POST,1);
curl_setopt($curl, CURLOPT_POST, count($post)
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_VERBOSE,true);
$result = curl_exec($curl)
if(!$result){
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
curl_close($curl);
var_dump($result);
?>

Đã trả lời ngày 20 tháng 10 năm 2017 lúc 13:54Oct 20, 2017 at 13:54

0

Điều này làm việc cho tôi. Bạn có thể thử nó

  $authHeader = array('Accept: application/form-data');

  $post_fields =  "client_id=" . $client_id . "&client_secret=" . $client_secret;

  send_curl_request("POST", $authHeader, $url, $post_fields);

function send_curl_request($method, $headers, $url, $post_fields){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  if(count($headers) > 0){
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    //echo '
HEADER ADDED

'; } curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); if($method == "POST"){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); //echo '
POST FIELDS ADDED

'; } curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_VERBOSE,true); $result = curl_exec($ch); curl_close($ch); return $result; }

Đã trả lời ngày 21 tháng 1 năm 2021 lúc 11:44Jan 21, 2021 at 11:44

Hướng dẫn how to pass form data in curl php? - làm cách nào để chuyển dữ liệu biểu mẫu trong curl php?

JuniorjuniorJunior

1.2013 huy hiệu vàng14 Huy hiệu bạc25 Huy hiệu đồng3 gold badges14 silver badges25 bronze badges

Bạn cần đọc dữ liệu tệp và sau đó đính kèm dữ liệu trên các trường bài.

Hãy thử điều này, điều này sẽ hoạt động. Nếu không hoạt động, hãy đảm bảo

'@'.$file_name_with_full_path);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_VERBOSE,true);
$result=curl_exec ($ch);
curl_close ($ch);
var_dump($result);?>
0 có thể đọc tệp của bạn. Đọc có thể thất bại vì nhiều lý do.

$target_url = 'https://apiprovider.com/api/v0/imports?token=[KEY]'

$file_name_with_full_path ='/home/USERNAME/import.csv';
$fileHandler = fopen($file_name_with_full_path, 'r');
$fileData = fread($fileHandler, filesize($file_name_with_full_path));

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

$headers = array('file'=>'@'.$file_name_with_full_path);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_INFILE, $fileHandler);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_name_with_full_path));

curl_setopt($ch, CURLOPT_POSTFIELDS, $fileData);

$result = curl_exec($ch);

curl_close ($ch);
var_dump($result);

Đã trả lời ngày 22 tháng 10 năm 2017 lúc 12:00Oct 22, 2017 at 12:00

Hướng dẫn how to pass form data in curl php? - làm cách nào để chuyển dữ liệu biểu mẫu trong curl php?

3

Làm thế nào để bạn đăng dữ liệu với Curl?

Làm thế nào để đăng dữ liệu biểu mẫu bằng Curl? Nếu bạn muốn đăng dữ liệu biểu mẫu với Curl, bạn có thể sử dụng các tham số dòng lệnh -F (--form) hoặc -d (-dữ liệu). Với tham số dòng lệnh -f, dữ liệu biểu mẫu được gửi dưới dạng "nhiều dữ liệu hình thức" và với tham số dòng lệnh -D, dữ liệu biểu mẫu được gửi dưới dạng "Ứng dụng/X-www-form-urlencoded".use the -F (--form) or -d (--data) command-line parameters. With the -F command-line parameter, form data is sent as "multipart/form-data", and with the -d command-line parameter, form data is sent as "application/x-www-form-urlencoded".

Tôi có thể sử dụng Curl trong PHP không?

Việc sử dụng Curl trong PHP Curl là tiện ích mở rộng PHP cho phép bạn sử dụng cú pháp URL để nhận và gửi dữ liệu.Curl làm cho nó đơn giản để kết nối giữa các trang web và tên miền khác nhau.Có được một bản sao tài liệu của một trang web.Đệ trình các biểu mẫu tự động, xác thực và sử dụng cookie.cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains. Obtaining a copy of a website's material. Submission of forms automatically, authentication and cookie use.

Là gì

Với Curl, bạn thêm từng cờ riêng biệt với một cờ một -F (hoặc -hình thức) và sau đó bạn tiếp tục và thêm một -F cho mọi trường đầu vào trong biểu mẫu bạn muốn gửi.Mẫu ví dụ nhỏ ở trên có hai phần, một phần có tên là 'người' là trường văn bản đơn giản và một phần có tên là 'bí mật' là một tệp.add each separate multipart with one -F (or --form ) flag and you then continue and add one -F for every input field in the form that you want to send. The above small example form has two parts, one named 'person' that is a plain text field and one named 'secret' that is a file.
Cookie được truyền đến Curl với tham số dòng lệnh "tên = giá trị".Curl tự động chuyển đổi tham số đã cho thành Cookie: Tên = Tiêu đề yêu cầu giá trị.Cookie có thể được gửi bằng bất kỳ phương thức HTTP nào, bao gồm GET, POST, PUT và XÓA, và với bất kỳ dữ liệu nào, bao gồm JSON, biểu mẫu web và tải lên tệp.with the --cookie "Name=Value" command line parameter. Curl automatically converts the given parameter into the Cookie: Name=Value request header. Cookies can be sent by any HTTP method, including GET, POST, PUT, and DELETE, and with any data, including JSON, web forms, and file uploads.