繁体   English   中英

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

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

我正在尝试在php中发布带有curl的文件,但是该文件从未被服务器上传/接受。 我已经搜索并尝试了几个小时,但是我找不到错误所在,其他所有人的示例和代码似乎都可以用,但不是这样。

这是代码:

<?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);

?>

我得到的结果是:

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"

那就是我得到的全部..该文件从未被服务器接受。 如果有人可以帮助我解决这个问题,将不胜感激:)谢谢!

您正在尝试通过HTTP发布上传文件,因此发送Content-type: text/xml标头是不合适的。 HTTP文件上传实际上是作为multipart/form-data ,实际上与MIME编码的电子邮件附件几乎相同。 PHP的curl将自动为您填充标题详细信息。 同样,Accept标头也不是必需的。

检查您要上传的.xml文件的路径是否正确。 您尚未指定前导/ ,因此该路径是相对于您的PHP脚本执行位置的。

更换:

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

有了这个:

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

试试看,可能会起作用,我不确定。

编辑:

试试看:

<?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