简体   繁体   中英

create api using php

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.

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

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

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.

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

http://example.com/sum

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

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.

If your web service has a resource that isn't side-effect free (ie 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.

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

API is some method which can be called from another application. 'philjohn' stated the simple one API is by using the REST technique.

This is the simple example for creating API using PHP, lets says you create new API called 'sum' using this URL: http://localhost/v1/api.php?method=sum&var1=4&var2=5

in the file api.php you can code like this:

<?php
if($_GET['method'] == 'sum')
{
 return $_GET['var1'] + $_GET['var2'];
}

that is just very simple how to create API. the advanced API will involve variable checking, connect to database, outputting with xml or json format, etc

HAi avinash,看看这个创建自己的自定义API

An API is usually something you would create to allow other developers to access your program more easily.

But by the sounds of what you are doing it seems like you just need to make a simple page that performs some calculations and an API may not be necessary for you to do this.

I suggest learning some general PHP tutorials first as well as some for processing forms .

The best and easiest way is to use frameworks and tools.

I use APIATO ( https://github.com/apiato/apiato ) it's a flawless framework for building scalable and testable API-Centric Apps with PHP and Laravel.

Check it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM