0

I'm trying to get proper output in json format but my output below is a bit messy. It should be like this: "{"table":"users","operation":"select","username":"inan"}"

How can I solve my problem?

Thanks

server.php

print_r($_POST);

client.php

$data = array('table'=>'users', 'operation'=>'select', 'uid'=>'yoyo');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($curl_handle);

if ($this->request_response_type == 'array')
{
echo $output;
}
else if ($this->request_response_type == 'json')
{
echo json_encode($output);
}
else if ($this->request_response_type == 'xml')
{
    //xml conversion will be done here later. not ready yet.
}

output:

"Array\n(\n    [table] => users\n    [operation] => select\n    [uid] => yoyo\n)\n"
1
  • the output you are showing is a print_r of a php array. If you want json, double check $this->request_response_type is returning 'json' and remember it is case sensitive so 'Json' != 'json'. Commented Feb 15, 2013 at 18:05

2 Answers 2

2

An array printed out with print_r cannot be parsed back into a variable.

In your server.php do echo json_encode($_POST);. Then in your client.php

<?php
//...
$output = curl_exec($curl_handle);

// and now you can output it however you like
if ($this->request_response_type == 'array')
{
    //though i don't see why you would want that as output is now useless if someone ought to be using the variable
    $outputVar = json_decode($output); // that parses the string into the variable
    print_r((array)$outputVar);
    // or even better use serialize()
    echo serialize($outputVar);
}
else if ($this->request_response_type == 'json')
{
    echo $output; //this outputs the original json
}
else if ($this->request_response_type == 'xml')
{
    // do your xml magic with $outputVar which is a variable and not a string
    //xml conversion will be done here later. not ready yet.
}
Sign up to request clarification or add additional context in comments.

Comments

-1

Take a look into JSON.stringify

1 Comment

if he was converting the array in javascript, yes. he is asking about php though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.