简体   繁体   中英

How can I use cURL to post form data in php?

I have the following command, which uses the --form/--F option, which I know to be working:

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

I need to run this command via php, but I'm having trouble, presumably with the form file data. I tried the following, however echoing or var_dumping the result seems to show nothing, just a blank page or a blank string.

<?php 
$target_url = 'https://apiprovider.com/api/v0/imports?token=[KEY]'
$file_name_with_full_path ='/home/USERNAME/import.csv';
$post = array('file'=>'@'.$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);?>

How can I get this command working in PHP?

since no answer got it right thus far (at least not with an approach that would work in php 5.6+), here goes: the equivalent php curl_ code would be:

$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 );

(i would also recommend setting CURLOPT_ENCODING to emptystring, especially if you expect the response to be compressible, that would be the equivalent of adding --compressed to the curl command line, and might speed things up)

try this:

<?php 
$target_url = 'https://apiprovider.com/api/v0/imports?token=[KEY]'
$file_name_with_full_path ='/home/USERNAME/import.csv';
$post = array('file'=>'@'.$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);
?>

One way is to use CURLFile instead of @ for PHP version above 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);

Maybe something like this could work:

<?php 
$target_url = 'https://apiprovider.com/api/v0/imports?token=[KEY]'
$file_name_with_full_path ='/home/USERNAME/import.csv';
$post = array('file'=>'@'.$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);
?>

Use this lib https://github.com/php-curl-class/php-curl-class and then

$curl = new Curl();
$curl->post('https://www.example.com/login/', array(
    'username' => 'myusername',
    'password' => 'mypassword',
));

You need to read file data and then attach data on post fields.

Try this, this should work. If doesn't work, make sure fread() can read your file. Reading can be failed due to many reason.

$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);

This worked for me. You can try it

  $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 '<br> HEADER ADDED<br><br>';
  }
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  if($method == "POST"){
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    //echo '<br> POST FIELDS ADDED<br><br>';
  } 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_VERBOSE,true);
  $result = curl_exec($ch);
curl_close($ch);
return $result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM