如何使用cURL在php中发布表单数据?

时间:2017-10-17 15:21:55

标签: php curl

我有以下命令,它使用--form / - F选项,我知道它正在工作:

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

我需要通过php运行此命令,但我遇到了麻烦,可能是表单文件数据。我尝试了以下内容,但是回显或var_dumping结果似乎没有显示任何内容,只是空白页或空白字符串。

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

如何在PHP中使用此命令?

7 个答案:

答案 0 :(得分:3)

因为迄今为止没有任何答案得到解决(至少没有在php 5.6+中使用的方法),这里有:等效的php curl_代码将是:

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

(我还建议将CURLOPT_ENCODING设置为emptystring,特别是如果您希望响应是可压缩的,这相当于将--compressed添加到curl命令行,并可能加快速度)

答案 1 :(得分:1)

试试这个:

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

答案 2 :(得分:0)

也许这样的事情可行:

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

答案 3 :(得分:0)

一种方法是使用CURLFile而不是@用于高于5.5的PHP版本

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

答案 4 :(得分:0)

使用此库 https://github.com/php-curl-class/php-curl-class 然后

df<-read.csv("C:/folder/subfolder/data.csv")

答案 5 :(得分:0)

您需要读取文件数据,然后在帖子字段上附加数据。

试试这个,这应该有效。如果不起作用,请确保fread()可以读取您的文件。由于许多原因,阅读可能会失败。

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

答案 6 :(得分:0)

<块引用>

这对我有用。你可以试试

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