我试图使用以下代码将文件发送到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;
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,我得到了这样的回应:
Curl Error: Failed to open/read local data from file/application
Run Code Online (Sandbox Code Playgroud)
任何建议都会有所帮助!
更新:当我取出CURLOPT_UPLOAD时,我从目标服务器得到响应,但它说有效负载中没有文件
你传递了一个相当奇怪的论点CURLOPT_POSTFIELDS.尝试更像的东西:
<?
$postfields = array('file' => '@' . $target_path);
// ...
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
?>
Run Code Online (Sandbox Code Playgroud)
此外,你可能想CURLOPT_RETURNTRANSFER是true的,否则$结果不会得到输出,它会改为直接发送到缓冲器/浏览器.
来自php.net的这个例子也可能有用:
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
在 coreward 的回答之上:
根据如何使用curl和php上传文件,从php 5.5开始你需要使用curl_file_create($path)而不是"@$path".
经测试:确实有效。
通过这种@方式没有文件被上传。
| 归档时间: |
|
| 查看次数: |
11527 次 |
| 最近记录: |