最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to make json api - Stack Overflow

programmeradmin0浏览0评论

Sorry if this is a basic question. But I am planning to make an api for my website. First this api will be used by me but later I will release for the developers to use it. I am confused, this website is going to be used for both mobile apps and for websites. I am planning to make android app for this.

The question is do I have to make this api in different languages i.e. in Java for android plateforms, in Objective C for iOS and javascript and php for web?

Will it work for several platforms if I only make this in javascript?

Sorry if this is a basic question. But I am planning to make an api for my website. First this api will be used by me but later I will release for the developers to use it. I am confused, this website is going to be used for both mobile apps and for websites. I am planning to make android app for this.

The question is do I have to make this api in different languages i.e. in Java for android plateforms, in Objective C for iOS and javascript and php for web?

Will it work for several platforms if I only make this in javascript?

Share Improve this question asked May 18, 2012 at 6:45 Om3gaOm3ga 32.9k45 gold badges149 silver badges230 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 7

On the server side, your JSON API would be written in one language on one platform, this could be PHP, .NET or any platform of your choice.

On the client side (iPhone, Android, etc) You would need to write a client that is capable of making requests and handling responses to your JSON API.

However, in order to enforce consistency across your client API's you can employ a pattern, such as the request-reply pattern, I use this all the time as its easy to use and to implement.

The idea is for every JSON API method, you have a Request class, and a Response class. You would also write a service client that represents your JSON API.

An example

Let's say I have a JSON service that gives me contact details from my address book, it could have these service methods:

/contact/{id}
/address_book
/save_contact/{id}

My service client (example in Java) could have this interface:

public interface AddressBookClient {
    public GetContactResponse getContact(GetContactRequest request);
    public GetAddressBookResponse getAddressBook(GetAddressBookRequest request);
    public SaveContactResponse saveContact(SaveContactRequest request);
}

Although the implementation would be different across client platforms, using the same approach or pattern would keep them consistent.

Hope that helps.

I would suggest something like a JSON based REST full API.

This makes your API language independent.

Once the API is made then you can make client libraries in popular languages.

If the API is JSON based, it is implemented for pretty much any popular language today. You can communicate between applications build in different languages with no issues.

You can see an extensive list of libraries for different languages at the official JSON site.

I'm using a URL to request the JSON:

api.example.com/api.php?action=User.login&username=admin&password=111

The JSON response is:

{
   "resultcode":100,
   "resultdata":{
      "id":"1",
      "username":"admin",
      "password":"$1$$TUPijxhaACBBZHMowbtgD.",
      "name":"chenwendong",
      "email":"[email protected]",
      "sex":"\u7537",
      "age":"11",
      "phone":"15811111111",
      "address":"\u5317\u4eac\u7231\u7231\u7231\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a\u554a",
      "province_id":"1",
      "city_id":"1",
      "last_login_ip":"127.0.0.1",
      "last_login_time":"2012-05-07 17:32:55",
      "state":"1",
      "intime":"2012-05-07 09:49:11",
      "is_deleted":"0",
      "province":"\u5317\u4eac\u5e02",
      "city":"",
      "sid":"fcd6321c4cd4748a0cee2e7cc337506b",
      "uid":"1",
      "message_count":"11"
   }
}

Then, someone can use Android JSON to decode it.

发布评论

评论列表(0)

  1. 暂无评论