Capture & Decode XOJO encoded JSON data in PHP

Hi all,

I’m trying to capture the sample data from the HTTPSocketPostExample project on an PHP page.
on var_dump($_POST); I see the uploaded data.
Using this function

function isJson($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); } echo isJson($_POST);

I see the $_POST is a JSON set.

So I know the data is arriving, but I fail to extract the data.

$data = json_decode($_POST, true); print_r($data);
Is not returning anything.

Could anyone help me with this?

thanks

Gert

Could you post what $_POST is?

I think I found the solution:

$data = json_decode(json_encode($_POST));
echo $data->Test ;
echo "\
" ;
echo $data->Value2 ;

if you have to encode then it’s not json.
Post data is Test=xxx&Value2=yyy

you can read it as $_POST[“Test”] and $_POST[“Value2”]

if you json-encode it will become {Test=“xxx” Value2=“yyy”}
then you decode it so you have
$data->Test and $data->Value2

Antonio, I see. I wrongly assumed I was sending JSON from XOJO.
When I define it like this:

Dim d As New JSONItem //This object is manipulated like a dictionary d.Value("Test") = "abc" d.Value("Value2") = "xyz" Socket.Yield=True Socket.SetRequestContent(d.ToString,"application/json; charset=utf-8") Socket.Post("http://httpbin.org/post")
Is this the right way to send it a server?

If you need to get it as json on the server this is the right way.

Then on the server you have to read the entire “input” (the post data contents)

json_decode(file_get_contents(‘php://input’), true)

and then check if decode has been successfull

Antonio,

the reason why I think I need JSON is because some of the strings I’ll be sending are quite long (> 1000 characters).
Just using POST seems to truncate what I send.

thanks for your sunday afternoon help!

gert