pass html form data to json php string -
pass html form data to json php string -
i've been able perform parse post info api using format:
working sample:
<?php $appid = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'; $private_token = 'xxxxxxxxxxxxxxxxxxx'; $geo_post = "/api/v1/geofence/"; $name = "home"; $latitude = 38.646322; $longitude = -121.185837; $radius = 50; $data = array( "name" => $name, "location" => array( "latitude" => $latitude, "longitude" => $longitude), "matchrange" => $radius ); $data_string = json_encode($data); $ch = curl_init(); $headers = array( 'content-type:application/json', 'authorization: basic '. base64_encode($appid.":".$private_token) // <--- ); curl_setopt($ch, curlopt_httpheader, $headers); curl_setopt($ch, curlopt_url, 'https://admin.plotprojects.com' . $geo_post ); curl_setopt($ch, curlopt_customrequest, "post"); curl_setopt($ch, curlopt_postfields, $data_string); curl_setopt($ch, curlopt_returntransfer, true); $content = trim(curl_exec($ch)); curl_close($ch); //print_r($content); ?>
sample error message:
<?php if (isset($_request['add_new'])){ $appid = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $private_token = 'xxxxxxxxxxxxxxxxxxxxxxx'; $geo_post = "/api/v1/geofence/"; $name = $_get['db_geofencename']; $latitude = $_get['db_latitude']; $longitude = $_get['db_longitude']; $radius = $_get['db_radius']; $data = array( "name" => $name, "location" => array( "latitude" => $latitude, "longitude" => $longitude), "matchrange" => $radius ); $data_string = json_encode($data); $ch = curl_init(); $headers = array( 'content-type:application/json', 'authorization: basic '. base64_encode($appid.":".$private_token) // <--- ); curl_setopt($ch, curlopt_httpheader, $headers); curl_setopt($ch, curlopt_url, 'https://admin.plotprojects.com' . $geo_post ); curl_setopt($ch, curlopt_customrequest, "post"); curl_setopt($ch, curlopt_postfields, $data_string); curl_setopt($ch, curlopt_returntransfer, true); $content = trim(curl_exec($ch)); curl_close($ch); print_r($content); ?>
as can see i'm using string in $data array, i'm trying capture info form , pass $data array, if utilize $_get or $_post end error message:
{ "success": false, "errormessage": "the request content malformed:\nexpected string jsstring, got null", "errorcode": "badrequest" }
what i'm doing wrong?
so best can think of convert(type cast) $latitude , $longitude floating numbers. don't depend on php auto type casting times internally maintain string , guess api want $latitude , $lon... floating numbers not strings.
php json api post
Comments
Post a Comment