简体   繁体   English

在PHP中使用curl发布文件时出现问题

[英]Problem when posting a file with curl in php

I'm trying to post a file with curl in php, but the file is never uploaded/accepted by the server. 我正在尝试在php中发布带有curl的文件,但是该文件从未被服务器上传/接受。 I have searched and tried for several hours, but I can't find whats wrong, everyone elses examples and codes seems to work, but not this one. 我已经搜索并尝试了几个小时,但是我找不到错误所在,其他所有人的示例和代码似乎都可以用,但不是这样。

Here is the code: 这是代码:

<?php

$url = "http://jpptst.ams.se/0.52/default.aspx";
$headers = array(
        "Content-Type: text/xml; charset=iso-8859-1",
    "Accept: text/xml"
);

$data = array("file" => "@documents/xmls/1298634571.xml");

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, false);

curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);

curl_close($ch);

var_dump($response);

?>

The result I get: 我得到的结果是:

string(904) "HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Date: Mon, 25 Jul 2011 19:13:41 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: 659"

Thats all I get.. the file is never accepted by the server. 那就是我得到的全部..该文件从未被服务器接受。 If anyone can help me with this problem it would be much appreciated :) Thanks! 如果有人可以帮助我解决这个问题,将不胜感激:)谢谢!

You're trying to upload a file via HTTP post, so sending a Content-type: text/xml header is inappropriate. 您正在尝试通过HTTP发布上传文件,因此发送Content-type: text/xml标头是不合适的。 An HTTP file upload is actually done as multipart/form-data , and is actually pretty much identical to a MIME-encoded email attachment. HTTP文件上传实际上是作为multipart/form-data ,实际上与MIME编码的电子邮件附件几乎相同。 PHP's curl will fill in the header details for you automatically. PHP的curl将自动为您填充标题详细信息。 As well, the Accept header is not necessary either. 同样,Accept标头也不是必需的。

Check that the path to the .xml file you're trying to upload is correct. 检查您要上传的.xml文件的路径是否正确。 You've not specified a leading / to it, so the path is relative to where your PHP script is executing from. 您尚未指定前导/ ,因此该路径是相对于您的PHP脚本执行位置的。

Replace: 更换:

$data = array("file" => "@documents/xmls/1298634571.xml");

With this: 有了这个:

$data = array("file" => "@".realpath('documents/xmls/1298634571.xml'));

Try it, might work, i'm not sure tho. 试试看,可能会起作用,我不确定。

EDIT: 编辑:

Try this out: 试试看:

<?php 

$xmldatafile="documents/xmls/1298634571.xml"; // Make sure the file path is correct

function postData($postFields,$url){ 
                $ch = curl_init(); 
                curl_setopt($ch, CURLOPT_URL, $url); 
                curl_setopt($ch, CURLOPT_POST ,1); 
                curl_setopt($ch, CURLOPT_POSTFIELDS ,$postFileds); 
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1); 
                curl_setopt($ch, CURLOPT_HEADER ,0); 
                curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); 
                $data = curl_exec($ch); 
                curl_close($ch); 
                return $data; 
} 

$xmlData = file_get_contents($xmldatafile); 

$postFileds = 'data='.$xmlData; 


$result = postData($postFields,"http://jpptst.ams.se/0.52/default.aspx"); 
?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM