如何与 Web XML/JSON API 交互? [英] How to interface with web XML/JSON APIs?

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

问题描述

我正在自己学习 php/xml/json 和其他所有内容,并且我正在通过 API 来处理某些事情.他们有文档,但我仍然不明白 API 是如何工作的.他们给你一个 GET 链接和 API 密钥,我知道你应该把 API 密钥放在请求链接中

如何调用此链接?当它为您提供示例响应时,这意味着什么?

如果您的请求正确,响应是否应该出来?

我有点不知所措?

谢谢

解决方案

在 PHP 中你可能有这样的情况:

//只需要在用户提供的变量上使用 urlencode()//$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);

$response 将包含您访问 $url 时输出的任何 xyz.com 字符串(如果您直接访问了 $url).

您的下一个工作是根据 $response 的数据结构(例如 XML、JSON 等)对其进行解析,以便您的其余代码可以使用它.

有几个用于解析 XML 或 JSON 的 PHP 库.就个人而言,我更喜欢使用 PHP 5 >= 5.2.0 中包含的 SimpleXMLElementjson_decode().

根据 API,它可能会向您发送某种错误代码/响应结构,如果它不理解请求 $url,您可以在解析响应后检查该请求.

如果 $response 返回 false,则通常在与 $url 通信时出现错误.

我发现考虑这些 XHR 请求的一种直观方式是将参数(GET 参数)传递给函数(API URL).来自 API URL 的响应就像来自函数的 return 语句.

更新:

OP 在评论中建议的 Groupon 的 API 示例:

$apikey = "client_id=abcd1234567890";$division = "division_id=芝加哥";$url = "http://api.groupon.com/v2/deals?".implode("&", array($apikey, $division));$response = file_get_contents($url);$deals = json_decode($response, true);foreach($deals['deals'] 作为 $deal){$format = '交易:<a href="%s">%s</a><br/>';echo sprintf( $format, $deal['dealURL'], $deal['announcementTitle']);}

以上代码将打印出芝加哥地区所有交易标题和网址的列表.如果您查看 Groupon API 页面上的 Sample JSON Response 部分,它将为您提供将映射到关联数组 $deals 的整个数据结构.>

如果用户向 API 提供了任何 GET 参数(例如从网络表单),您将需要执行类似 $division = "division_id=" 的操作.urlencode($user_input);.

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?

I'm a bit clueluess?

Thank you

解决方案

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);

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).

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.

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.

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.

If $response returns false, then typically there was some error communicating with the $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.

UPDATE:

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']);
}

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.

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天全站免登陆