Tuesday, June 26, 2012

php: return data in json

In server side, the following code can return the data in JSON format to client(Apps).

<?php

     
    $name = "Ben";
    $id = "id1234";
    $method = "get data";
    
    // build result in json format
    $arr = array("result" => "success",
                 "uid" => $id,
                 "data"=>array("name"=>$name, 
                               "id"=>$id, 
                               "method"=>$method 
                               
                               )
                 );
    
    echo json_encode($arr);
    
    
?>

No comments:

Post a Comment