Send array of floats as HTTP POST request in php -
i writing script executed client generate array of floats (which understand in php supposed same doubles) , perform post request page take data , compute mean, returning data in response. plan attach send array in message body in json format. found tutorial online doing post
requests in php have taken function do_post_request()
, can't around following exception:
php fatal error: uncaught exception 'exception' message 'problem http://localhost/average.php' in /home/yak/restclient.php:20 stack trace: #0 /home/yak/restclient.php(5): do_post_request('http://localhos...', '[1.5,1.5,1.5,1....', 'content-type=ap...') #1 {main} thrown in /home/yak/restclient.php on line 20
the client code:
<?php $array = array_fill(0,5,1.5); $data = json_encode($array); echo $data; $response = do_post_request("http://localhost/average.php",$data,"content-type=application/json"); echo $response; function do_post_request($url, $data, $optional_headers = null) { $params = array('http' => array( 'method' => 'post', 'content' => $data )); if ($optional_headers !== null) { $params['http']['header'] = $optional_headers; } $ctx = stream_context_create($params); $fp = @fopen($url, 'r', false, $ctx); if (!$fp) { throw new exception("problem $url"); } $response = @stream_get_contents($fp); if ($response === false) { throw new exception("problem reading data $url); } return $response; } ?>
would able shed light on error, or better yet, suggest better way of doing i'm trying here? have tried using http_post_data()
function, after trying fixes found on so, have been unable work (php sees undefined function).
this server code:
<?php header("content-type:application/json"); if(!empty($_post['data'])) { #calculate mean of each array , send results $data = $_post['data']; $array = json_decode($data); $mean = array_sum($array)/count($array); respond(200,"ok",$mean); } else { #invalid request respond(400,"invalid request",null); } function respond($status,$message,$data) { header("http/1.1 $status $message"); $response['status']=$status; $response['message']=$message; $response['data']=$data; $json_response=json_encode($response); echo $json_response; } ?>
this adapted deal several arrays sent in 1 message, started thought quite simple - wrong.
update: using arpita's answer request going through response indicates invalid request, if not doing post.
pretty same other answer, go curl , call function you're doing already, using :
instead of =
in optional headers as array:
client side
<?php // $url string url send data // $data associative array containing each value want send through post, example: array( 'data', json_encode($array, true) ); // $optional_headers normal array containing strings (key:value, using ":" instead of "="), example: array( 'content-type: application/json' ); function do_post_request($url, $data, $optional_headers) { $ch = curl_init( $url ); curl_setopt( $ch, curlopt_post, true ); // people use count($data) or 1 instead of true here, according documentation, should true (any number greater 0 evaluate true anyway, it's personal call use 1 or another) curl_setopt( $ch, curlopt_postfields, serialize($data) ); curl_setopt( $ch, curlopt_returntransfer, true ); curl_setopt( $ch, curlopt_followlocation, true ); // not harmful, follow location: header redirects if server sends them curl_setopt( $ch, curlopt_httpheader, $optional_headers ); return curl_exec( $ch ); } $array = array_fill(0,5,1.5); $data = json_encode($array); echo $data; $response = do_post_request("http://localhost/average.php", $data, array('content-type: application/x-www-form-urlencoded', 'accept: application/json')); // note difference: content-type says sending, accept accept answer. you're sending encoded json inside usual form format, content-type html form format instead of json 1 (in other words: you're sending string represents json inside form array) echo $response; ?>
to clarify last point (the optional headers content-type), when content-type: application/json
should sending json string, mean, example:
[1, 2, 5, 3, 4]
(post url-encodes to: %5b1%2c+2%2c+5%2c+3%2c+4%5d
)
but you're sending (which content-type: application/x-www-form-urlencoded
):
data=[1, 2, 5, 3, 4]
(post url-encodes to: data=%5b1%2c+2%2c+5%2c+3%2c+4%5d
)
application/x-www-form-urlencoded
accepts key-value pairs ones in url.
Comments
Post a Comment