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);
脚本不断超时,我不知道为什么。
我希望得到一些帮助。谢谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我明白了这一点。碰巧是 fclose 导致了这个问题。我只是把它放在curl_exec之后。
这是修改后的代码:
希望这对以后的其他人有帮助。
干杯。
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:
Hope this is helpful to someone else later.
Cheers.
我不能 100% 确定这是问题所在,但我唯一能看到的是,当您不需要时,您正在使用
fread()
读取文件的内容,并且是不对数据做任何事情。这会将指针留在文件末尾,并且 cURL 将等待来自指针的数据,但它永远不会得到。删除这一行,看看问题是否仍然存在:
或者,如果您确实需要文件中的数据,并且稍后在脚本中使用
$file_data_str
变量,请将此行放在该行之后上面提到的:编辑
另外,我对此完全不确定:
''.$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:
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: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.如果您绝对确定连接超时并且没有其他原因..那么您可以添加以下代码行 -
If you are absolutely sure that the connection is being timed out and nothing else.. then you can add the following lines of code-