方案是将文件从server1(本地服务器)中的PHP应用程序发送到server2中的C#.net应用程序。
使用简单的HTML表单发送文件时,目标服务器会毫无问题地接收它。
使用cURL发送该文件时,我从目标服务器收到错误500 请注意,我无法访问目标服务器,并且由于成功提交了HTML表单,因此管理员确信一切正常。
这是我发布文件的代码:
$filedata = $fileName; // Absolute path of the file ==> 'D:\wamp\www\my_project\upload\image\201401070657414.jpg'
$headers = array("Content-Type:multipart/form-data");
$postfields = array("filedata" => "@$filedata", "filename" => $fileName);
$ch = curl_init();
$options = array(
CURLOPT_URL => 'http://server2/postphoto.aspx', //500
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_INFILESIZE => filesize($filedata),
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_getinfo($ ch)返回:
array (size=26)
'url' => string 'http://server2/postphoto.aspx' (length=41)
'content_type' => string 'text/html; charset=utf-8' (length=24)
'http_code' => int 500
'header_size' => int 265
'request_size' => int 203
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 0.032
'namelookup_time' => float 0
'connect_time' => float 0
'pretransfer_time' => float 0
'size_upload' => float 28998
'size_download' => float 7127
'speed_download' => float 222718
'speed_upload' => float 906187
'download_content_length' => float 7127
'upload_content_length' => float 28998
'starttransfer_time' => float 0.016
'redirect_time' => float 0
'certinfo' =>
array (size=0)
empty
...
我的代码出了什么问题?
变量$ response显示:
string 'HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Wed, 22 Jan 2014 08:31:54 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 6
nofile' (length=249)
Content-Type rturns text / html,而我已将其设置为标题中的multipart / form-data。
以下是server2上的Asp.net代码:
if (Request.Files.Count == 0)
{
Response.Write("nofile");
return;
}
System.Web.HttpPostedFile file = Request.Files[0];
System.IO.Stream streamObj = file.InputStream;
Byte[] buffer = new Byte[file.ContentLength];
streamObj.Read(buffer, 0, buffer.Length);
streamObj.Close();
streamObj = null;
System.IO.File.WriteAllBytes("d:\\photo\\" + file.FileName, buffer);
答案 0 :(得分:0)
尝试将变量赋值给curl_exec($ ch);像这样$response = curl_exec($ch);
并执行var_dump var_dump($response)
。告诉我们服务器的响应是什么。
答案 1 :(得分:0)
我认为CURLOPT_INFILESIZE
仅适用于PUT请求。相反,明确设置POST:
CURLOPT_CUSTOMREQUEST => POST
CURLOPT_POST => true
我也建议"filedata" => "@" . $filedata
- 不确定。
这是错误的:
$filedata = $fileName; // Absolute path of the file
它们不能相同,一个包含文件的完整绝对路径,可能是/tmp/phpcpjNeQ
,另一个包含没有路径test_file.csv
的文件名。
要验证您的本地文件是否可访问,请使用fopen()
。
assert(is_resource(fopen($filedata, 'r')));
答案 2 :(得分:0)
尝试使用php处理请求提供了一个文件数组,如下所示:
Array
(
[filedata] => Array
(
[name] => 201401070657414.jpg
[type] => application/octet-stream
[tmp_name] => D:\wamp\tmp\php519A.tmp
[error] => 0
[size] => 28619
)
)
所以这里的错误是什么!?
答案 3 :(得分:-1)
这样可以根据您的需要更改变量:
<?php
$post_data['content'] = "@/c:/xampp/htdocs/site/a.doc";
$post_data['filename'] = "a.doc";
foreach ( $post_data as $key => $value)
{
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection = curl_init('https://yoururl.com/form.php');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);
//perform our request
$result = curl_exec($curl_connection);
echo $result;
//close the connection
curl_close($curl_connection);
?>
有关详细信息,请查看此link