drwf69817 2013-08-27 15:28
浏览 62
已采纳

将带有curl的xml发送到外部URL“无响应”

I'm trying to send xml to a rest server.

$url = url;
$file = 'finn/test.xml';
$post = array('name' => 'fil','file_contents'=>'@'.$file); 

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);

When I run this code I get a blank response.

I know there's nothing wrong with the server or the xml because when I use this to just upload the file to the server everything works fine.

<html>
<body>
<form name="bilupload" action="url" method="post" enctype="multipart/form-data">
partner: <input TYPE="FILE" NAME="fil" size="10">
<br>
<input type="submit" value="Send">
</form>
</body>
</html>

curl_getinfo prints this:

[url] => url
[content_type] => text/html;charset=ISO-8859-1
[http_code] => 200
[header_size] => 497
[request_size] => 144
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.062231
[namelookup_time] => 2.7E-5
[connect_time] => 0.016873
[pretransfer_time] => 0.016928
[size_upload] => 3668
[size_download] => 35
[speed_download] => 562
[speed_upload] => 58941
[download_content_length] => 35
[upload_content_length] => 3668
[starttransfer_time] => 0.034394
[redirect_time] => 0

I've been struggling with this for quite some time and I would be very grateful if anyone could point me in the right direction.

  • 写回答

1条回答 默认 最新

  • doujiang5211 2013-08-28 10:26
    关注

    Your form tells the server that it's sending form data in a given format:

    enctype="multipart/form-data"
    

    But your PHP scripts tells it's sending XML (but doesn't send XML at all):

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
    

    I suppose you want this:

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
    

    ... though you can just omit the line.

    Perhaps you had the Accept header in mind :-?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容