Neeraj Dangol
Neeraj Dangol

Reputation: 223

Return json text/msg from the php web service

I have a php web service that takes the request from android/iphone app and uses the input to find the appropriate data from the mysql database. The result is encoded back to json. How can I return the json txt/msg to the android/iphone app. Please help?????

Upvotes: 0

Views: 1023

Answers (2)

Shikiryu
Shikiryu

Reputation: 10219

short answer : Use json_encode();

echo json_encode($data);

long answer :

Don't forget headers :

header('Content-type: application/json');

Beware of errors, don't echo them, or echo them in json with json_encode() So, you need to configure PHP (or at least this page) :

  • not to show errors and to log them
  • handle errors as array or directly in json

Upvotes: 0

GordonM
GordonM

Reputation: 31730

echo (json_encode ($data_to_send));

Upvotes: 1

Related Questions