PHP CURL PUT 从文件路径

发布于 2024-12-03 08:36:55 字数 602 浏览 0 评论 0原文

我正在尝试对文件执行 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);

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

我希望得到一些帮助。谢谢。

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

Here is my code:

$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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟若柳尘 2024-12-10 08:36:55

我明白了这一点。碰巧是 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);

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

干杯。

I figured this out. It happened to be fclose that was causing the issue. I simply put it after curl_exec.

Here's the amended code:

$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.

Cheers.

秋日私语 2024-12-10 08:36:55

我不能 100% 确定这是问题所在,但我唯一能看到的是,当您不需要时,您正在使用 fread() 读取文件的内容,并且是不对数据做任何事情。这会将指针留在文件末尾,并且 cURL 将等待来自指针的数据,但它永远不会得到。

删除这一行,看看问题是否仍然存在:

$file_data_str = fread($fh_res, filesize($file_path_str));

或者,如果您确实需要文件中的数据,并且稍后在脚本中使用 $file_data_str 变量,请将此行放在该行之后上面提到的:

rewind($fh_res);

编辑

另外,我对此完全不确定:''.$url_path_str.'' - 你应该能够做到这一点$url_path_str 和它将具有相同的效果,但资源效率(稍微)更高。

I'm not 100% sure this is the problem, but the only one I can see is that you are reading the contents of the file with fread() when you don't need to, and are not doing anything with the data. This will leave the pointer at the end of the file, and cURL will be waiting for data from the pointer that it will never get.

Remove this line and see if your still have the problem:

$file_data_str = fread($fh_res, filesize($file_path_str));

Alternatively, if you do actually need the data from the file and are using the $file_data_str variable later in the script, place this line immediately after the one mentioned above:

rewind($fh_res);

EDIT

Also, I'm not sure about this at all: ''.$url_path_str.'' - you should just be able to do this $url_path_str and it will have the same effect but be (slightly) more resource-efficient.

无法回应 2024-12-10 08:36:55

如果您绝对确定连接超时并且没有其他原因..那么您可以添加以下代码行 -

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000);
curl_setopt($ch, CURLOPT_TIMEOUT, 10000 );

If you are absolutely sure that the connection is being timed out and nothing else.. then you can add the following lines of code-

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000);
curl_setopt($ch, CURLOPT_TIMEOUT, 10000 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文