我正在尝试使用php curl将表单域和文件发送到web服务。表单已经从浏览器传递到代理php客户端web应用程序,我正在尝试将其转发到web服务。
当我像这样将数组传递给curl_setopt时:
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->fields);
我得到了一个array来字符串通知,尽管它应该接受一个数组。下面是在构造函数中传递给$this->字段的数组。
$fields = array('title'=>$title,
'content'=>$content,
'category'=>$category,
'attachment'=>$_FILES['attachment']);如果我使用http_build_query传递一个字符串,我的web服务会报告没有多部分/表单数据。
如果我使用curl_setopt强制多部分/表单封装类型,我得到一个错误,说没有边界:
org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
有什么想法吗?
发布于 2010-04-15 19:07:18
使用以下代码将数组转换为字符串注意事项:
$fields = array(
'title'=>$title,
'content'=>$content,
'category'=>$category,
'attachment'=>$_FILES['attachment']
);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $fields);这不是因为您将一个数组作为第三个参数传递给curl_setopt :而是因为您正在为attachment传递一个数组。
如果你想以这种方式传递一个文件,你应该传递它的绝对路径,在它之前加上一个@:
$fields = array(
'title'=>$title,
'content'=>$content,
'category'=>$category,
'attachment'=> '@' . $_FILES['attachment']
);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $fields);(假设$_FILES['attachment']包含文件的完整路径--如果需要,您可以更改此代码,以便使用正确的数据)
作为参考,引用CURLOPT_POSTFIELDS选项的手册页:
在HTTP " post“操作中要POST的完整数据。
若要发布文件,请在文件名前面加上 @ 并使用完整路径。
它既可以作为and编码的字符串传递,如'para1=val1¶2=val2&...',也可以作为数组传递,字段名作为键,字段数据作为值。
如果value为数组,则Content-Type头部将设置为multipart/form-data。
发布于 2010-04-15 19:54:41
尝尝这个,
$filePath = "abc\\xyz.txt";
$postParams["uploadfile"] = "@" . $filePath;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, 'https://website_address');
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
if (curl_errno($ch))
{
echo curl_error($ch);
exit();
}
curl_close($ch);https://stackoverflow.com/questions/2644619
复制相似问题