使用PHP创建API [英] create api using php

查看:165
本文介绍了使用PHP创建API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用PHP开发一个简单的API。

I want to develop one simple api using php.

我的功能是,如果有人输入一些所需的值,然后他们会从算法得到的计算结果的旁边我的网站上。

My functionality is that if some one enter some required values then they will get calculation result from the algorithm beside on my site.

我没有从我在哪里可以开始得到。
同时也没有使用PHP得到任何样品code的API。

I am not getting from where can i start. and also not getting any sample code for API using PHP.

推荐答案

这听起来像你想创建一个Web服务,其他人可以连接,发送答案并检索结果。

It sounds like you want to create a web-service that other people can connect to, send answers and retrieve a result.

如果是这样的话,你有三种选择,SOAP,XML-RPC和REST。如果它是一个简单的API,SOAP(大概XML-RPC)将是矫枉过正 - 你不想必须创建一个WSDL文件,安装SOAP服务器库(虽然,Zend_Soap够体面)。在另一方面REST将允许任何人可以轻松地消耗你的API。

If that's the case, you've got three options, SOAP, XML-RPC and REST. If it's a simple API, SOAP (and probably XML-RPC) will be overkill - you don't want to have to create a WSDL file, install a SOAP server library (although, Zend_Soap is decent enough). REST on the other hand will allow anyone to easily consume your API.

让我们来看一个例子,说你要提供一个简单的求和的服务(即,加几号),你可以有这样的URI方案:

Let's look at an example, say you want to provide a simple "sum" service (i.e., add a few numbers), you could have a URI scheme like this:

http://example.com/sum

总结数字5,8和9 Web服务的用户将执行的SimPy一个HTTP GET到

to sum the numbers 5, 8 and 9 your web service users would simpy execute an HTTP GET to

http://example.com/sum/5/8/9

让我们pretend的时刻,总结其实是一个计算量非常大的任务,通过使用REST,你可以利用HTTP缓存,这样,当有人发送相同的参数为您的服务器不经常打一个GET计算。

let's pretend for a moment that summing is actually a very computationally expensive task, by using REST and a GET you can take advantage of HTTP caching so that your server isn't constantly hit when someone sends the same parameters for calculation.

如果您的Web服务有没有副作用免费的资源(即它改变的东西在数据库中),你应该使用PUT,POST或DELETE(PUT更新,POST用于创建和删除删除)作为HTTP规范注明的GET应该不会有副作用。同样,PUT和DELETE应该是安全的,如果重复你得到一个错误回来或网络连接超时。

If your web service has a resource that isn't side-effect free (i.e. it changes something in a database) you should use PUT, POST or DELETE (PUT for updates, POST for creating and DELETE for delete) as the HTTP specs state the GETs shouldn't have side-effects. Similarly, PUT and DELETE should be safe to repeat if you get an error back or the network connection times out.

有一个很好的谈话(视频和幻灯片)在这里休息:<一href=\"http://www.parleys.com/display/PARLEYS/Home#talk=31817742\">http://www.parleys.com/display/PARLEYS/Home#talk=31817742

There's a good talk (video and slides) on REST here: http://www.parleys.com/display/PARLEYS/Home#talk=31817742

这篇关于使用PHP创建API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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