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¶2=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.
You don't need to create a custom request using cURL to make this request, a regular HTTP POST will suffice. Another part of the problem is that you are also setting the POSTFIELDS
and custom request to the same thing which was the HTTP request you constructed so the entire request consists largely of two duplicate strings.
Try this code and study it to understand how it works:
<?php
$url = "http://127.0.0.1/sensor/sens/data.php";
$xml_data = file_get_contents("/usr/local/www/data/data.xml");
$headers = array('Content-Type: text/xml',
'Content-Transfer-Encoding: text',
'Connection: close');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
$data = curl_exec($ch); // if the post is successful , the server will return
// some data.
echo $data;
// info = curl_getinfo($ch);
//
// f(!curl_errno($ch))
// echo 'It took '.$info['total_time'].'seconds to send a request
// to'.$info['url'];
//
// else
//
curl_close($ch);
echo $data;