如何在 PHP 中创建一个返回 Json 的 Restful Web 服务 [英] How to Create a Restful Webservice in Php which return Json

查看:29
本文介绍了如何在 PHP 中创建一个返回 Json 的 Restful Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Php 中创建一个 Restful Web 服务,它返回 json 格式.以便可以从其他移动和网络应用程序使用.

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.

我想创建一个 Get 方法.

I wanted to Create a Get Method.

推荐答案

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

打开你的网络服务器,砰!我认为您现在拥有一个非常安静的 Web 服务,可以从其他移动和 Web 应用程序中使用.仍然是 json 格式.

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.

抱歉,我是想厚脸皮.但是,如果这对您有用,那么您不妨直接从您的网络服务器提供带有 { "something":"somethingelse" }file.json .不需要PHP.至少不是 GET 请求.

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.

您甚至可以将它与您的 web 应用程序放在同一个文件夹中并执行 $.get('file.json') 或 $http.get 或 axios.get 或任何可以让您的船漂浮的东西.

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.

这篇关于如何在 PHP 中创建一个返回 Json 的 Restful Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆