从CURLed页面通过CURL将上传的文件发送到另一台服务器 [英] sending uploaded file to another server via CURL from a CURLed page

查看:101
本文介绍了从CURLed页面通过CURL将上传的文件发送到另一台服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多有关如何使用cURL上传文件的示例(使用cURL发送文件可能是最接近我需要的内容),但是出于任何原因我的脚本都无法正常工作。

I have seen a lot of examples on how upload files with cURL (Send file with cURL being probably the closest to what I need), but for whatever reason my scripts are not working.

我要实现的目标:卷曲SERVER2上的文件上传表单,在SERVER1上显示它。选择一个要通过此表单上载的文件,提交该表单,但将其提交给SERVER1,获取$ _POSTS和$ _FILES,然后将提交的URL提交到SERVER2上的表单处理脚本,并在SERVER1中显示结果。

What I am trying to achieve: curl a file upload form from SERVER2, display it on SERVER1. Select a file to upload via this form, submit the form but have it submit to SERVER1, grab the $_POSTS and the $_FILES and then cURL a submission to the form processing script on SERVER2 and display results in SERVER1.

换句话说,我在SERVER2上有一个工作文件上载和处理接口,但我需要使其看起来像在SERVER1上正在发生一切。

In other words, I have a working file upload and processing interface on SERVER2, but I need to make it look like everything is happening on SERVER1.

到目前为止,这是我在SERVER1上的相关代码:

This is what I have as far the relevant code on SERVER1:

//loop through submitted files
foreach ($_FILES as $k => $v) {
    $settings[$k] = '@'.$v['tmp_name'];
}

$settings = http_build_query($settings);
$cookie= WP_CONTENT_DIR.'/plugins/myPlugin/cookie.txt';
//SERVER1 is a WORDPRESS site

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $settings);    

curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);     
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

$response = curl_exec($ch);

当我输出$ response(包括print_r($ _ FILES))时,文件作为空数组并生成以下错误:警告:file_get_contents(@ / tmp / phpFITW6K):无法打开流:没有此类文件或目录。

When I output $response (which includes a print_r($_FILES)), files come up as an empty array and the following error is generated: Warning: file_get_contents(@/tmp/phpFITW6K): failed to open stream: No such file or directory.

我想念什么?我注意到某些cURL示例中缺少http_build_query,而是传递了一个数组,但是我收到不允许将请求数据与POST请求一起使用,或者请求中提供的数据量超出了容量限制。 http_build_query行。

What am I missing? I noticed that http_build_query is missing from some cURL examples and an array is passed instead, but I get a "does not allow request data with POST requests, or the amount of data provided in the request exceeds the capacity limit. " if I remove the http_build_query line.

任何帮助将不胜感激。

谢谢!

推荐答案

删除http_build_query()并在文件名前保留'@'即可。我不太确定解决了不允许带有POST请求的请求数据,或者请求中提供的数据量超出了容量限制错误,但是现在已在SERVER2上正确接收并解析了该文件。

Removing http_build_query() and keeping '@' in front of the file name did it. I am not quite sure what fixed the "does not allow request data with POST requests, or the amount of data provided in the request exceeds the capacity limit" error, but the file is received and parsed properly on SERVER2 now.

这篇关于从CURLed页面通过CURL将上传的文件发送到另一台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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