使用JSON一个API帮助 [英] Helping using JSON for an API

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

问题描述

我真的不明白这个API是如何工作的,因为我从来没有使用JSON前的工作。

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

我的问题是,我不知道到底如何实现这一点,让我们说我只是想拉所有的数据到一个简单的页面,如这样的:

城市: 塞勒姆

邮编code: 97302

等等......

我不太知道从哪里开始与此:


  

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


  
  

GET HTTP:// [您RepMan主机] /api/v1/account/reputation/current.json


  
  

以下是为POST身体参数列表或GET查询字符串。所有值应适当带codeD按照常规的后体或GET查询字符串。


  |现场|序数|数据类型|描述
| PID | 1 |串|这是由我们提供访问API您的合作伙伴ID。
| apiKey | 1 |串|这是你的API密钥的利用提供访问API。
| SRID | ? |串|这是该帐户的唯一RepMan标识。这个或客户ID必须指定。
|客户ID | ? |串|这是你唯一的客户编号的帐户。这个或SRID必须指定。


  

有关200响应,您会收到以下内容的JSON


  {
账号:{
    SRID:DW5SRB36
    名字:森,
    PID:SRP
    客户ID:空,
    姓:雅治
},
公司:{
    城市:纽约,
    postalZip:10011,
    provState:NY,
    名称:森,
    地址:88 10大道
},
知名度:{
    发现:18,
    丢失:9
},
评论:{
    1star:5,
    4星级:37,
    星级:44,
    五星:66,
    2星级:5
},
竞争:{
    餐厅在纽约:{
        Megu餐厅:1.82,
        森本:52.95,
        祭:18.13,
        的Buddakan:0.93,
        延生:26.17
    }
},
社会:{
    签入:5015,
    twitter_followers:8154,
    facebook_likes:1134
},
提到:{
    2011年7月9日:{
        正:0,
        中性:0,
        负:0
    },
    07-07-2011:{
        正面:2,
        中性:3,
        负:0
    },
    2011年7月5日:{
        正面:1,
        中性:2,
         负:0
    },
    2011年7月11日:{
        正面:2,
        中性:2,
        负:0
    },
    07-06-2011:{
        正:5,
        中性:2,
        负:0
    },
    2011年7月10日:{
        正:3,
        中性:4,
        负:0
    },
    07-08-2011:{
        正面:1,
        中性:5,
        负:0
    }
}
}
}


解决方案

要尝试的第一件事就是实验在Web浏览器中的几个请求。从那里,它应该是pretty清楚你需要做什么。

开始用自己的基本网址:

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

显然,你必须在你的主机插上到位的[您RepMan主机] 。从那里,让我们添加一个查询字符串。你以前见过这些......他们来的问号在URL后,并包含在键1的形式键/值对=值1&放大器;键2 =值。你有4变量插件: PID apiKey SRID 客户ID 。不知道这是什么web服务呢,这是很难帮助你知道什么值插上,但这里有一个例子:

<$p$p><$c$c>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_de code()

  $ =响应的file_get_contents('插入您的网址在这里');
$ responseObject = json_de code($响应);
的print_r($ responseObject);

基本上,的file_get_contents()将在该URL加载数据和 json_de code()将采取对象的文本再presentation并把它变成一个真正的PHP对象。从那里,你可以做回声$ responseObject-&GT;社会 - 方式&gt;签入或类似

现在,你应该考虑使用卷曲,而不是的file_get_contents()。它会给你在要求更多的控制,使您能够响应状态codeS更容易获得。当你想以后设置上要求的时间限制,或者需要处理的故障,这将是非常重要的。此外,请确保您使用 urlen code() http_build_query() 来建立你的查询字符串。这样一来,保留字符如空间将被转换为自己的EN codeD的形式, 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天全站免登陆