[英]How can I use cURL to post form data in php?
我有以下命令,它使用 --form/--F 选项,我知道它正在工作:
curl --form "file=@/home/USERNAME/import.csv" https://apiprovider.com/api/v0/imports\?token\=[KEY]
我需要通过 php 运行这个命令,但我遇到了问题,大概是表单文件数据。 我尝试了以下操作,但是 echo 或 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 中工作?
由于到目前为止没有答案是正确的(至少不是在 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 设置为空字符串,特别是如果您希望响应是可压缩的,这相当于将--compressed
添加到 curl 命令行,并且可能会加快速度)
试试这个:
<?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);
?>
一种方法是对 5.5 以上的 PHP 版本使用CURLFile
而不是 @
$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);
也许这样的事情可以工作:
<?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);
?>
使用这个库https://github.com/php-curl-class/php-curl-class然后
$curl = new Curl();
$curl->post('https://www.example.com/login/', array(
'username' => 'myusername',
'password' => 'mypassword',
));
您需要读取文件数据,然后在帖子字段上附加数据。
试试这个,这应该有效。 如果不起作用,请确保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);
这对我有用。 你可以试试
$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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.