如何使用Web XML / JSON API的接口? [英] How to interface with web XML/JSON APIs?

查看:132
本文介绍了如何使用Web XML / JSON API的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习上我自己的PHP / XML / JSON和一切,我所经历的API的某些事情。他们有单证,但我还是不明白怎么API的工作。他们给你一个GET链接和API密钥,我知道,你应该把请求链接里面的API密钥

I'm learning php/xml/json and everything else on my own and I was going through API's for certain things. They have documentations but I still don't get how API's work. They give you a GET link and API key, I know that you're supposed to put the API key inside the request link

我如何把这个链接?而这是什么意思时,它给你一个样的反应?

How do I call this link? And what does it mean when it gives you a sample response?

应该出来,如果​​你得到的回应请求是否正确?

Is the response supposed to come out if you got the request correct?

我有点clueluess?

I'm a bit clueluess?

感谢您

推荐答案

在PHP你可能有这样的事情:

In PHP you might have something like this:

// EDIT: only need to use urlencode() on user supplied variables
//$url = urlencode("http://xyz.com/api?apikey=foo&v1=bar&v2=baz");
$url = "http://xyz.com/api?apikey=foo&v1=bar&v2=baz";
$response = file_get_contents($url);

$回应将包含的任何 xyz.com 输出,当你访问一个字符串 $网址(这是如果您访问 $网址直接,你会看到什么)。

The $response will contain a string of whatever xyz.com outputted when you accessed $url (it's what you would see if you visited $url directly).

您接下来的工作将是解析 $回应基于其数据结构(如XML,JSON等),因此它的可用你的$ C $其余角

Your next job would be to parse $response based on its data structure (e.g XML, JSON, etc) so that it's usable by the rest of your code.

有用于解析XML或JSON几个PHP库。就个人而言,我preFER使用的SimpleXMLElement json_de code()这是包含在PHP 5 > = 5.2.0。

There are several PHP libraries for parsing XML or JSON. Personally, I prefer to use SimpleXMLElement and json_decode() which is included with PHP 5 >= 5.2.0.

根据不同的API,它可能会送你一些错误code /响应结构如果不理解请求 $网址您可以检查您解析响应后。

Depending on the API, it will probably send you some sort of error code/response structure if it doesn't understand the request $url which you could check for after you parse the response.

如果 $回应返回false,则通常有一些错误与 $网址通信。

If $response returns false, then typically there was some error communicating with the $url.

我发现一个直观的方式来思考这些 XHR 要求是,你传递参数( GET 参数)的函数(API URL)。而从API URL的反应就像是从一个函数返回语句。

I found that an intuitive way to think about these XHR requests is that you're passing arguments (GET parameters) to a function (API URL). And the response from the API URL is like the return statement from a function.

更新:

有关 Groupon的 API例如由OP的意见建议:

API example for Groupon as suggested by OP in comments:

$apikey = "client_id=abcd1234567890";
$division = "division_id=chicago";
$url = "http://api.groupon.com/v2/deals?" . implode("&", array($apikey, $division));
$response = file_get_contents($url);
$deals = json_decode($response, true);

foreach($deals['deals'] as $deal){
    $format = 'Deal: <a href="%s">%s</a><br/>';
    echo sprintf( $format, $deal['dealURL'], $deal['announcementTitle']);
}

以上code将打印出所有的交易标题和URL为芝加哥地区的列表。如果你看看样品Groupon的API页面上JSON响应部分,它会给你会被映射到关联数组整个数据结构 $交易

The above code would print out a listing of all deal titles and urls for the Chicago area. If you look at the Sample JSON Response sections on the Groupon API page, it will give you the entire data structure that would be mapped to the associative array $deals.

如果任何 GET 参数的API由用户(例如,从一个Web表单)提供的,你会想要做类似 $师=division_id =。 urlen code($ USER_INPUT);

If any of the GET parameters to the API are provided by the user (e.g. from a web form), you will want to do something like $division = "division_id=" . urlencode($user_input);.

这篇关于如何使用Web XML / JSON API的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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