问题描述
我想使用PHP和卷曲通过HTTP POST发送一个文件。
该表单POST正在确定与除该文件的基本领域被贴有应用/ JSON的。这需要多部分/形式从我的理解。
错误,我越来越为 注意:数组字符串转换
在线搜索 curl_setopt($通道, CURLOPT_POSTFIELDS,$数据);
如果有人可以帮助这将是伟大的!
PHP
$ ORGID =(is_numeric($ _ POST ['ORGID'])(INT)$ _ POST ['ORGID']:0);
$ noteTitle =(使用isset($ _ POST ['noteTitle'])$ _ POST ['noteTitle']:NULL);
$ noteBody =(使用isset($ _ POST ['noteBody'])$ _ POST ['noteBody']:NULL);如果(使用isset($ _ FILES ['文件'] ['tmp_name的值'])){ $ CH = curl_init();
$的CFile =新CURLFILE($ _ FILES ['文件'] ['tmp_name的值'],$ _FILES ['文件'] ['类型'],$ _FILES ['文件'] ['名']);
$数据=阵列(); $数据[TITLE] =$ noteTitle
$数据[BODY] =$ noteBody
$数据[LINK_SUBJECT_ID] =$ ORGID
$数据[LINK_SUBJECT_TYPE] =组织;
$数据['FILE_ATTACHMENTS'] ['FILE_NAME'] = $ _FILES ['文件'] ['名'];
$数据['FILE_ATTACHMENTS'] ['CONTENT_TYPE'] = $ _FILES ['文件'] ['型'];
$数据['FILE_ATTACHMENTS'] ['URL'] = $ _FILES ['文件'] ['tmp_name的值']; $ LOCALFILE = $ _FILES ['文件'] ['tmp_name的值'];
$计划生育=的fopen($ LOCALFILE,'R'); $头=阵列(
授权:基本XXX,
缓存控制:无缓存
内容类型:多重/表单数据
邮差令牌:XXX
); curl_setopt($ CH,CURLOPT_URLhttps://api.insight.ly/v2.1/Notes);
curl_setopt($ CH,CURLOPT_UPLOAD,1);
curl_setopt($ CH,CURLOPT_TIMEOUT,86400); // 1天超时
curl_setopt($ CH,CURLOPT_INFILE,$ FP);
curl_setopt($ CH,CURLOPT_NOPROGRESS,FALSE);
curl_setopt($ CH,CURLOPT_BUFFERSIZE,128);
curl_setopt($ CH,CURLOPT_INFILESIZE,文件大小($ LOCALFILE));
curl_setopt($ CH,CURLOPT_POST,真正的);
curl_setopt($ CH,CURLOPT_POSTFIELDS,$数据);
curl_setopt($ CH,CURLOPT_HTTPHEADER,$头); $响应= curl_exec($ CH); 如果($响应===真){
$味精='文件上传成功。';
}
其他{
$味精= curl_error($ CH);
} curl_close($ CH); $收益率=阵列('味精'=> $味精); 回声json_en code($返程);
}
HTML
<形式方法=POSTACTION =formSend.phpENCTYPE =的multipart / form-data的>
<输入类型=文本值=NAME =ORGID>
<输入类型=文本值=NAME =noteTitle>
<输入类型=文本值=NAME =noteBody>
<输入名字=文件类型=文件ID =文件/>
<输入类型=提交值=提交名称=btnUpload/>
< /表及GT;
您好我想通了这个问题。
我的PARAMS没有对API端点设置正确。需要设置一个note_id(C_ID)
但我现在有问题在一旦公布的所有数据。注释已创建从而产生对我的留言ID为发布文件后,我张贴的文件。任何人都可以提供帮助的?我可以发布一个新的问题。
请参见下面更新code:
// $ ORGID =(is_numeric($ _ POST ['ORGID'])(INT)$ _ POST ['ORGID']:0);
// $ noteTitle =(使用isset($ _ POST ['noteTitle'])$ _ POST ['noteTitle']:NULL);
// $ noteBody =(使用isset($ _ POST ['noteBody'])$ _ POST ['noteBody']:NULL);$ noteID =(使用isset($ _ POST ['noteID'])$ _ POST ['noteID']:NULL);$ LOCALFILE = $ _FILES ['文件'] ['tmp_name的值'];
$计划生育=的fopen($ LOCALFILE,'R');$卷曲= curl_init();$的CFile =新CURLFILE($ _ FILES ['文件'] ['tmp_name的值'],$ _FILES ['文件'] ['类型'],$ _FILES ['文件'] ['名']);
$数据=阵列();
// $数据[TITLE] =$ noteTitle
// $数据[BODY] =$ noteBody
// $数据[LINK_SUBJECT_ID] =$ ORGID
// $数据[LINK_SUBJECT_TYPE] =组织;
$数据['FILE_ATTACHMENTS'] = $的CFile;curl_setopt_array($卷曲,阵列(
CURLOPT_UPLOAD => 1,
CURLOPT_INFILE => $ FP,
CURLOPT_NOPROGRESS =>假,
CURLOPT_BUFFERSIZE => 128,
CURLOPT_INFILESIZE =>文件大小($ LOCALFILE)
CURLOPT_URL => https://api.insight.ly/v2.1/Notes/?c_id=。 $ noteID。与&文件名=。 $ _FILES ['文件'] ['名'],
CURLOPT_RETURNTRANSFER =>真正,
CURLOPT_ENCODING => ,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => POST,
CURLOPT_POSTFIELDS => $的数据,
CURLOPT_HTTPHEADER =>阵列(
授权:基本XXX,
缓存控制:无缓存
内容类型:多重/表单数据
邮差令牌:XXX
)
));
$响应= curl_exec($卷曲);
$ ERR = curl_error($卷曲);curl_close($卷曲);如果($ ERR){
回声卷曲错误#:。 $犯错;
}其他{
回声$反应;
}
HTML
<形式方法=POSTACTION =formSend.phpENCTYPE =的multipart / form-data的>
//<输入类型=文本值=NAME =ORGID>
//<输入类型=文本值=NAME =noteTitle>
//<输入类型=文本值=NAME =noteBody>
<输入类型=文本值=NAME =noteID>
<输入名字=文件类型=文件ID =文件/>
<输入类型=提交值=提交名称=btnUpload/>
< /表及GT;
如果有人有兴趣,这是我使用FPDF来生成基于Web形式的PDF文档,然后自动发送解决方案,而不是文件上传。 FPDF文件--->通过卷曲不能与文件上传
自动发送I am wanting to send a file via http POST using PHP and cURL.
The form POST was working ok with basic fields besides the file being posted with 'application/json'. This needs to be multipart/form from what I understand.
Error I am getting is Notice: Array to string conversion
on line curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
If anyone can help that would be great!
PHP
$orgID = (is_numeric($_POST['orgID']) ? (int)$_POST['orgID'] : 0);
$noteTitle = (isset($_POST['noteTitle']) ? $_POST['noteTitle'] : null);
$noteBody = (isset($_POST['noteBody']) ? $_POST['noteBody'] : null);
if(isset($_FILES['file']['tmp_name'])){
$ch = curl_init();
$cfile = new CURLFILE($_FILES['file']['tmp_name'], $_FILES['file']['type'], $_FILES['file']['name']);
$data = array();
$data["TITLE"] = "$noteTitle";
$data["BODY"] = "$noteBody";
$data["LINK_SUBJECT_ID"] = "$orgID";
$data["LINK_SUBJECT_TYPE"] = "Organisation";
$data['FILE_ATTACHMENTS']['FILE_NAME'] = $_FILES['file']['name'];
$data['FILE_ATTACHMENTS']['CONTENT_TYPE'] = $_FILES['file']['type'];
$data['FILE_ATTACHMENTS']['URL'] = $_FILES['file']['tmp_name'];
$localFile = $_FILES['file']['tmp_name'];
$fp = fopen($localFile, 'r');
$headers = array(
"authorization: Basic xxx",
"cache-control: no-cache",
"content-type: multipart/form-data",
"postman-token: xxx"
);
curl_setopt($ch, CURLOPT_URL, "https://api.insight.ly/v2.1/Notes");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_NOPROGRESS,false);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localFile));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if ($response === true) {
$msg = 'File uploaded successfully.';
}
else {
$msg = curl_error($ch);
}
curl_close ($ch);
$return = array('msg' => $msg);
echo json_encode($return);
}
HTML
<form method="POST" action="formSend.php" enctype="multipart/form-data">
<input type="text" value="" name="orgID">
<input type="text" value="" name="noteTitle">
<input type="text" value="" name="noteBody">
<input name="file" type="file" id="file"/>
<input type="submit" value="Submit" name="btnUpload"/>
</form>
Hi I have figured out the problem.
My params were not set correct on the api endpoint. Need to set a note_id(c_id)
But issue I am having now is posting all data at once. I am posting file after the note has been created thus generating the note id for me for posting file. Can anyone help with that? I can post a new question.
See updated code below:
//$orgID = (is_numeric($_POST['orgID']) ? (int)$_POST['orgID'] : 0);
//$noteTitle = (isset($_POST['noteTitle']) ? $_POST['noteTitle'] : null);
//$noteBody = (isset($_POST['noteBody']) ? $_POST['noteBody'] : null);
$noteID = (isset($_POST['noteID']) ? $_POST['noteID'] : null);
$localFile = $_FILES['file']['tmp_name'];
$fp = fopen($localFile, 'r');
$curl = curl_init();
$cfile = new CURLFILE($_FILES['file']['tmp_name'], $_FILES['file']['type'], $_FILES['file']['name']);
$data = array();
//$data["TITLE"] = "$noteTitle";
//$data["BODY"] = "$noteBody";
//$data["LINK_SUBJECT_ID"] = "$orgID";
//$data["LINK_SUBJECT_TYPE"] = "Organisation";
$data['FILE_ATTACHMENTS'] = $cfile;
curl_setopt_array($curl, array(
CURLOPT_UPLOAD => 1,
CURLOPT_INFILE => $fp,
CURLOPT_NOPROGRESS => false,
CURLOPT_BUFFERSIZE => 128,
CURLOPT_INFILESIZE => filesize($localFile),
CURLOPT_URL => "https://api.insight.ly/v2.1/Notes/?c_id=" . $noteID . "&filename=" . $_FILES['file']['name'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
"authorization: Basic xxx",
"cache-control: no-cache",
"content-type: multipart/form-data",
"postman-token: xxx"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
HTML
<form method="POST" action="formSend.php" enctype="multipart/form-data">
//<input type="text" value="" name="orgID">
//<input type="text" value="" name="noteTitle">
//<input type="text" value="" name="noteBody">
<input type="text" value="" name="noteID">
<input name="file" type="file" id="file"/>
<input type="submit" value="Submit" name="btnUpload"/>
</form>
If anyone is interested this is my solution for using fpdf to generate a PDF document from the web form then auto send, instead of the file upload. FPDF file ---> send via CURL automatically NOT with file upload
这篇关于多重表单文件上载POST PHP卷曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!