我的PHP curl请求有什么问题,请帮助..我没有得到任何数据回来 [英] What's wrong with my PHP curl request, please help .. I'm not getting any data back

查看:114
本文介绍了我的PHP curl请求有什么问题,请帮助..我没有得到任何数据回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它说,浏览器发送一个请求服务器不能理解..我不完全明白我的PHP代码出了什么问题。有人可以帮助我明白我错了什么。谢谢!

 <?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 \r\\\
;
$ header。=Content-type:text / xml \r\\\
;
$ header。=Content-length:.strlen($ xml_data)。\r\\\
;
$ header。=Content-transfer-encoding:text\r\\\
;
$ header。=连接:关闭\r\\\
\r\\\
;
$ 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); //如果发布成功,服务器将返回一些数据。
echo $ data;

#$ info = curl_getinfo($ ch);

#if(!curl_errno($ ch))
#echo'发送请求到'。$ info ['total_time']。 'url'];

#else


curl_close($ ch);
echo $ data;







?>


解决方案

我认为问题是 CURLOPT_POSTFIELDS



从PHP手册...



在HTTPPOST操作中发布的完整数据。要发布文件,请使用@前缀一个文件名,并使用完整路径。文件类型可以通过跟随文件名显式指定,其类型为格式; type = mimetype。这个参数可以作为像'para1 = val1& para2 = val2& ...'这样的urlencoded字符串传递,或者作为一个数组,字段名为key,字段数据为value。如果value是一个数组,Content-Type头部将被设置为multipart / form-data。从PHP 5.2.0开始,如果文件使用@前缀传递到此选项,则值必须是一个数组。

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



它应该只包含有效载荷,而不是整个标头。


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 \r\n";
    $header .="Content-type: text/xml \r\n";
    $header .="Content-length: ".strlen($xml_data)." \r\n";
    $header .="Content-transfer-encoding: text\r\n";
    $header .="Connection: close \r\n\r\n";
    $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;







    ?>

解决方案

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.

这篇关于我的PHP curl请求有什么问题,请帮助..我没有得到任何数据回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆