我应该知道的API是什么? [英] What should I know about API's?

查看:143
本文介绍了我应该知道的API是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要什么,并有

我愿意创建一个 API ,我已经知道做什么的API表示。
我使用PHP作为一个服务器端语言。

I'm willing to create an API, I already know what does an API stands for. I use PHP as a server side language.

不过,我真的不明白一个API从头如何工作的,以及如何使用户能够使用我的 API 后,我创造了它。

However I don't really understand how an API works from scratch, and how can I enable users to use my API after I created it.

我想什么,知道

那么现在我想用我的API使开发人员能够达到我的数据库,并要求查询。但后来我为什么要为它创建一个完整的语言?他们为什么不能使用 PHP 或任何其他服务器端语言?

Well right now I think that with an API I enable developers to reach my database and request queries. But then why should I create a full language for it? Why can't they use PHP or any other server side language?

正如我所看到的,谁包装箱的API为他们的网站的公司,他们需要注册,给开发者API密钥。难道开发商登录到数据库低谷本 API KEY ?如果是的话,为什么他们没有密码吗?如果我知道别人的API KEY我可以用它还是什么?

As I saw, companies who crates API's for their site, they require a registration to give to developers an API KEY. Does the developers log in to database trough this API KEY? And if yes, then why they don't have a password too? If I know somebody's API KEY I can use it or what?

和我读/听过 XML 的主题相关的API的,但为什么又如何?

And as I read/heard XML is related somehow to API's, but why and how?

另外,请考虑您的回答,我不希望使用任何框架或类似的东西,我希望把它刮中,所以我想了解它从一个如何工作到Z

Also please consider in your answers that I don't want to use any frameworks or something like that, I want to make it in scratch so I want to understand how it works from A to Z.

推荐答案

这是API实际上只是方法,您公开暴露。说,例如,你有一个博客网站。您希望您的会员能够发布,而无需实际来到你的网站博客条目 - 登录,填写表格等。
也许他们想创建一个Android / IPhone应用程序张贴到您的酷博客网站。您可以允许他们这样做。

An API is really just methods that you publicly expose. Say, for example, you have a blog site. You want your members to be able to post a blog entry without actually coming to your site - logging in, filling out a form, etc.. Perhaps they want to create an Android/IPhone app to post to your cool blog site. You can allow them to do that.

此创建一个API:

    <?php
        function createBlogEntry(memberEmailAddress, memberPassWord, BlogTitle, BlogBody)
        {

           //Here you would usually call an internal (or not publicly exposed) method that
           //contains the actual logic to insert. Like Jim's answer states, you just want 
           //people to be able to performan action in your system, you don't want them to see HOW you do it.

          encapsulatedBlogCreation(memberEmailAddress, memberPassWord, BlogTitle, BlogBody);  


           //This is where API's get tricky. I need to return to the user the result of
           // the save so that my user knows what happens. In my API documentation, I 
           //tellthe user 0 = Created Successfully. If this is a READ operation
           //just return the result. Most modern programming languages also have
           //a way to capture an HTTP response - so dealing with the return is their responsibility

            return 0;
        }
 ?>

有两件事情:


  • 在我的API,我并不需要一个开发重点,我允许用户在他们的登录凭据传递。 API密钥做同样的事情,它唯一标识一个请求,并将其链接到系统的用户。

  • 这个API不使用XML。这是作为一个API得到一样简单。这可以通过发送其可以与任何现代编程语言来实现的HTTP请求被调用。说我把上面的code在一个名为blog.php的,并把它放在我的博客上的根源。我把它叫做这样的:

*http://myblogsite.com/blog.php?memberEmailAddress=myEmal@example.com&memberPassWord=myPass&BlogTitle=Title-of-post&BlogBody=the_blog_post_is_here*

*http://myblogsite.com/blog.php?memberEmailAddress=myEmal@example.com&memberPassWord=myPass&BlogTitle=Title-of-post&BlogBody=the_blog_post_is_here*

你要明白一件事,就是必须有需要的API。您可以暴露在创建博客条目的方法,但如果没有人知道它或使用它 - 这是毫无价值的。 API的需要进行记录,公布,以及潜在的用户提供一个好处。

One thing you need to understand, is that there has to be a need for an API. You can expose a method to create a blog entry, but if no one knows about it or uses it - it's worthless. API's need to be documented, advertised, and provide a benefit to potential users.

这篇关于我应该知道的API是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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