如何在Php中创建一个返回Json的Restful Web服务

I want to create a Restful Web service in Php which returns json format. So that Can be consumed from other mobile and Web Applications.

I wanted to Create a Get Method.

To send a json response you have to serialize the content. In order for doing it you can use jms/serializer, here you can find the documentation for this library. I hope it helps

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and RESTful APIs. If you want to go Vanilla try this tutorial: Building a Simple Web API with Vanilla PHP

// index.php
<?php 
  header('Content-type: application/json'); 
  echo '{"status":"restful"}'; 
?>

Put on your webserver and bam! I would argue that you now have a VERY restful web service that can be consumed from other mobile and web applications. In json format nonetheless.

Sorry, I was trying to be cheeky. But if this worked for you, then you might as well serve a file.json with { "something":"somethingelse" } directly from your webserver. No PHP needed. At least not for GET requests.

You can even put it in the same folder as your webapp and do a $.get('file.json') or $http.get or axios.get or whatever get floats your boat.