帮助将 JSON 用于 API [英] Helping using JSON for an API

查看:16
本文介绍了帮助将 JSON 用于 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白这个 API 应该如何工作,因为我以前从未使用过 JSON.

文档没有给出任何例子,但它说这个 API 中的端点支持 POST 和 GET 操作,返回 JSON.

我的问题是,我不确定如何实现这一点,假设我只想将所有数据提取到一个简单的页面中,例如:

城市:塞勒姆

邮政编码: 97302

等等...

我不太确定从哪里开始:

<块引用>

POST http://[您的 RepMan 主机名]/api/v1/account/reputation/current.json

获取 http://[您的 RepMan 主机名]/api/v1/account/reputation/current.json

以下是 POST 正文或 GET 查询字符串的参数列表.所有值都应按照正常的 POST 正文或 GET 查询字符串进行正确编码.

<代码>|领域 |序数 |数据类型 |描述|pid |1 |字符串 |这是我们提供的用于访问 API 的合作伙伴 ID.|apiKey |1 |字符串 |这是用于访问 API 的用户提供的 API 密钥.|srid |?|字符串 |这是帐户的唯一 RepMan ID.必须指定 this 或 customerId.|客户 ID |?|字符串 |这是您帐户的唯一客户 ID.必须指定 this 或 srid.

<块引用>

对于 200 响应,您将收到以下 JSON 内容:

<代码>{帐户 : {srid:DW5SRB36",姓氏:森本",pid : SRP",客户 ID:空,名字:正治"},公司 : {城市:纽约",邮政邮编:10011",provState : 纽约",名称:森本",地址:88 10th Ave"},可见性:{发现 : 18,失踪:9},评论:{一星 : 5,4星:37,三星级 : 44,5 星 : 66,2星:5},竞赛 : {纽约的餐馆:{美古 : 1.82,森本:52.95,祭 : 18.13,佛陀坎:0.93,信 : 26.17}},社会的 : {签到:5015,twitter_followers : 8154,facebook_likes : 1134},提到:{07-09-2011 : {正:0,中性:0,负数:0},07-07-2011:{阳性:2,中性 : 3,负数:0},07-05-2011:{阳性:1,中性 : 2,负数:0},07-11-2011:{阳性:2,中性 : 2,负数:0},07-06-2011:{阳性 : 5,中性 : 2,负数:0},07-10-2011:{阳性:3,中性 : 4,负数:0},07-08-2011:{阳性:1,中性 : 5,负数:0}}}}

解决方案

首先要尝试的是在 Web 浏览器中尝试一些请求.从那里开始,您应该很清楚您需要做什么.

从您的基本网址开始:

http://[您的 RepMan 主机名]/api/v1/account/reputation/current.json

显然,您必须插入您的主机名来代替 [您的 RepMan 主机名].从那里,让我们添加一个查询字符串.您以前见过这些...它们位于 URL 中的问号 ? 之后,并包含 key1=value1&key2=value2 形式的键/值对>.您有 4 个要插入的变量:pidapiKeysridcustomerId.在不知道此 Web 服务做什么的情况下,很难帮助您了解要插入哪些值,但这里有一个示例:

http://example.com/api/v1/account/reputation/current.json?pid=12345&apiKey=asdf&srid=34&customerid=98765

使用您想要的参数手动构建自己的工作 URL,然后在浏览器中尝试.完成此操作后,您将看到一些文本结构以 JSON 格式返回.这是与 JavaScript 解析器兼容的文本,但实际上与 JavaScript 是分开的.

现在,您如何在 PHP 中实现这一点?一个快速的方法是使用 file_get_contents()json_decode().

$response = file_get_contents('把你的网址插入这里');$responseObject = json_decode($response);print_r($responseObject);

基本上,file_get_contents() 将加载该 URL 处的数据,而 json_decode() 将获取对象的文本表示并将其转换为真正的 PHP 对象.从那里你可以做 echo $responseObject->social->checkins 或类似的.

现在,您应该考虑使用 cURL 而不是 file_get_contents().它将让您更好地控制请求,并让您更轻松地访问响应状态代码.当您想稍后对该请求设置时间限制或需要处理失败时,这将很重要.另外,请确保您使用 urlencode()http_build_query() 构建您的查询字符串.这样,保留字符(例如空格)将转换为其编码形式,例如 %20.

I don't really understand how this API is supposed to work, as I've never worked with JSON before.

The documentation doesn't give any examples, but it says it the end-points in this API support both POST and GET operations, returning JSON.

My question is, I'm not sure exactly how to implement this, let's say I just want to pull all the data into a simple page such as this:

City: Salem

Zip Code: 97302

etc...

I'm not quite sure where to start with this:

POST http://[your RepMan hostname]/api/v1/account/reputation/current.json

GET http://[your RepMan hostname]/api/v1/account/reputation/current.json

Following is a list of arguments for the POST body or GET query string. All values should be properly encoded as per a normal POST body or GET query string.

| Field      | Ordinality | Datatype | Description
| pid        | 1          | string   | This is your partner ID as provided by us to access the API.
| apiKey     | 1          | string   | This is your API Key as provided by use to access the API.
| srid       | ?          | string   | This is the unique RepMan ID for the account. Either this or customerId must be specified.
| customerId | ?          | string   | This is your unique customer id for the account. Either this or srid must be specified.

For a 200 response, you will receive the following JSON content:

{
account : {
    srid        : "DW5SRB36",
    lastName    : "Morimoto",
    pid         : "SRP",
    customerId  : null,
    firstName   : "Masaharu"
},
company : {
    city        : "New York",
    postalZip   : "10011",
    provState   : "NY",
    name        : "Morimoto",
    address     : "88 10th Ave"
},
visibility : {
    found       : 18,
    missing     : 9
},
reviews : {
    1star       : 5,
    4star       : 37,
    3star       : 44,
    5star       : 66,
    2star       : 5
},
competition : {
    Restaurants in New York : {
        Megu    : 1.82,
        Morimoto: 52.95,
        Matsuri : 18.13,
        Buddakan: 0.93,
        Nobu    : 26.17
    }
},
social : {
    checkins            : 5015,
    twitter_followers   : 8154,
    facebook_likes      : 1134
},
mentions : {
    07-09-2011 : {
        positive    : 0,
        neutral     : 0,
        negative    : 0
    },
    07-07-2011: {
        positive    : 2,
        neutral     : 3,
        negative    : 0
    },
    07-05-2011: {
        positive    : 1,
        neutral     : 2,
         negative   : 0
    },
    07-11-2011: {
        positive    : 2,
        neutral     : 2,
        negative    : 0
    },
    07-06-2011: {
        positive    : 5,
        neutral     : 2,
        negative    : 0
    },
    07-10-2011: {
        positive    : 3,
        neutral     : 4,
        negative    : 0
    },
    07-08-2011: {
        positive    : 1,
        neutral     : 5,
        negative    : 0
    }
}
}
}

解决方案

The first thing to try is experiment with a few requests in your web browser. From there, it should be pretty clear what you need to do.

Start with your base URL:

http://[your RepMan hostname]/api/v1/account/reputation/current.json

Obviously, you'll have to plug in your hostname in place of [your RepMan hostname]. From there, let's add a query string. You've seen these before... they come after the question mark ? in the URL, and contain key/value pairs in the form of key1=value1&key2=value2. You have 4 variables to plugin: pid, apiKey, srid, and customerId. Without knowing what this web service does, it's hard to help you know what values to plug in, but here is an example:

http://example.com/api/v1/account/reputation/current.json?pid=12345&apiKey=asdf&srid=34&customerid=98765

Manually build yourself a working URL with the parameters you want, and try it in your browser. Once you have done that, you will see some text structure come back in JSON format. This is text that is compatible with JavaScript parsers, but is in fact separate from JavaScript.

Now, how do you get this going in PHP? A quick way is to use file_get_contents() and json_decode().

$response = file_get_contents('plug your URL in here');
$responseObject = json_decode($response);
print_r($responseObject);

Basically, file_get_contents() will load the data at that URL, and json_decode() will take the text representation of the object and turn it into a real PHP object. From there you could do echo $responseObject->social->checkins or similar.

Now, you should look into using cURL instead of file_get_contents(). It will give you more control over the request, and gives you easier access to response status codes. This will be important when you want to set a time limit on that request later on, or need to handle failures. Also, make sure that you use urlencode() or http_build_query() to build your query string. That way, reserved characters such as spaces will be converted to their encoded form, such as %20.

这篇关于帮助将 JSON 用于 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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