来自文件路径的 PHP CURL PUT

标签 php curl put

我正在尝试对文件执行 CURL PUT,但我遇到了问题。

这是我的代码:

$url_path_str = 'http://my_url';
$file_path_str = '/my_file_path';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, ''.$url_path_str.'');
curl_setopt($ch, CURLOPT_PUT, 1);

$fh_res = fopen($file_path_str, 'r');
$file_data_str = fread($fh_res, filesize($file_path_str));

curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
fclose($fh_res);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);

脚本一直超时,我不确定为什么。

我会很感激一些帮助。谢谢。

最佳答案

我想通了。恰好是 fclose 导致了这个问题。我只是把它放在 curl_exec 之后。

修改后的代码:

$url_path_str = 'http://my_url';
$file_path_str = '/my_file_path';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, ''.$url_path_str.'');
curl_setopt($ch, CURLOPT_PUT, 1);

$fh_res = fopen($file_path_str, 'r');

curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);
fclose($fh_res);

希望这对以后的其他人有帮助。

干杯。

关于来自文件路径的 PHP CURL PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7340809/

相关文章:

curl - 当 curl 或 file_get_contents 请求 https url 时,php-fpm 崩溃

ios - 使用 AFNetworking 上传简单的 "PUT"文件

curl - 如何在Logstash中将事件标记为 “Looked At”

java - REST API 无响应,Parse.com 云错误 : Can't Form Encode an Object

javascript - 从 Ajax 将数组数组传递给 Controller

php - Mysql_real_escape_string() 警告混淆

php - 我可以使用特定参数定位 CSS 样式吗?

php - Laravel 外键保存问题

php - 尝试通过 curl 发布后从页面中获取数据

c++ - 为什么将静态 libcurl.a 添加到 xcode 7.2 示例项目将添加 libcurl.4.dylib 依赖项