dongyan3237 2012-10-18 21:31
浏览 115
已采纳

我的PHP curl请求有什么问题,请帮忙..我没有收到任何数据[关闭]

It says the browser sent a request the server could not understand .. I don't exactly understand what went wrong in my PHP code. Can someone please help me understand where I went wrong. Thanks !

<?php
    $url ="http://127.0.0.1/sensor/sens/data.php";
    $xml_data = file_get_contents("/usr/local/www/data/data.xml");
    $header ="POST HTTP/1.0 
";
    $header .="Content-type: text/xml 
";
    $header .="Content-length: ".strlen($xml_data)." 
";
    $header .="Content-transfer-encoding: text
";
    $header .="Connection: close 

";
    $header .= $xml_data;
    $ch = curl_init();
    curl_setopt ($ch,CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$header);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
    $data = curl_exec($ch);  // if the post is successful , the server will return some data.
    echo $data;

    #$info = curl_getinfo($ch);
    #
    #if(!curl_errno($ch))
    #   echo 'It took '.$info['total_time'].'seconds to send a request to'.$info['url'];
    #
    #   else
    #

    curl_close($ch);
    echo $data;







    ?>
  • 写回答

2条回答 默认 最新

  • dpchen2004 2012-10-18 21:39
    关注

    I think the problem is with CURLOPT_POSTFIELDS

    From the PHP Manual...

    The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix.

    http://php.net/manual/en/function.curl-setopt.php

    It should hold just the payload and not the entire header.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?