PHP CURL PUT从文件路径 [英] PHP CURL PUT from file path

查看:173
本文介绍了PHP CURL PUT从文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用文件执行CURL PUT,但是我遇到问题。

I'm trying to do a CURL PUT with a file but I'm having issues.

这是我的代码:

$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);

脚本保持超时,我不知道为什么。

The script keeps timing out and I'm not sure why.

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

I'd appreciate some assistance. Thanks.

推荐答案

我想出了这一点。它恰巧是fclose导致的问题。

I figured this out. It happened to be fclose that was causing the issue. I simply put it after 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);

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

Hope this is helpful to someone else later.

干杯。

这篇关于PHP CURL PUT从文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆