我正在尝试使用以下代码将文件发送到Https URL:
$file_to_upload = array('file_contents'=>'@'.$target_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSER, FALSE);
curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'file='.$file_to_upload);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close ($ch);
echo " Server response: ".$result;
echo " Curl Error: ".$error;
但出于某种原因,我得到了这样的回应:
Curl Error: Failed to open/read local data from file/application
任何建议都会有帮助,谢谢!
更新:当我取出CURLOPT_UPLOAD时,我从目标服务器得到一个响应,但它告诉我负载中没有文件
发布于 2011-01-21 02:32:28
您正在向CURLOPT_POSTFIELDS
传递一个相当奇怪的参数。试试更多像这样的东西:
<?
$postfields = array('file' => '@' . $target_path);
// ...
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
?>
此外,您可能希望CURLOPT_RETURNTRANSFER
为true
,否则$result将不会获得输出,而是直接将其发送到缓冲区/浏览器。
来自php.net的This example可能也很有用:
<?php
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>
发布于 2017-05-09 20:47:35
在coreward的答案之上:
根据how to upload file using curl with php的说法,从PHP5.5开始,你需要使用curl_file_create($path)
而不是"@$path"
。
经过测试:它可以正常工作。
使用@
方式不会上传任何文件。
https://stackoverflow.com/questions/4750927
复制相似问题