我真的不明白这个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查询字符串对所有值进行正确编码.
| 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.
Run Code Online (Sandbox Code Playgroud)
对于200响应,您将收到以下JSON内容:
{
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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
首先要尝试的是在Web浏览器中尝试一些请求.从那里,你应该很清楚你需要做什么.
从您的基本网址开始:
http://[your RepMan hostname]/api/v1/account/reputation/current.json
Run Code Online (Sandbox Code Playgroud)
显然,你必须插入你的主机名来代替[your RepMan hostname].从那里,让我们添加一个查询字符串.你之前见过这些......他们来自?URL中的问号,并包含形式的键/值对key1=value1&key2=value2.你有4个变量插件: pid,apiKey,srid,和customerId.在不知道这个Web服务的作用的情况下,很难帮助您了解要插入的值,但这是一个示例:
http://example.com/api/v1/account/reputation/current.json?pid=12345&apiKey=asdf&srid=34&customerid=98765
Run Code Online (Sandbox Code Playgroud)
使用您想要的参数手动构建自己的工作URL,并在浏览器中尝试.完成后,您将看到一些文本结构以JSON格式返回.这是与JavaScript解析器兼容的文本,但实际上与JavaScript是分开的.
现在,你如何在PHP中实现这一目标?一个快速的方法是使用file_get_contents()和json_decode().
$response = file_get_contents('plug your URL in here');
$responseObject = json_decode($response);
print_r($responseObject);
Run Code Online (Sandbox Code Playgroud)
基本上,file_get_contents()将在该URL上加载数据,json_decode()并将获取对象的文本表示并将其转换为真正的PHP对象.从那里你可以做echo $responseObject->social->checkins或类似.
现在,您应该考虑使用cURL而不是file_get_contents().它可以让您更好地控制请求,并使您更容易访问响应状态代码.当您希望稍后在该请求上设置时间限制或需要处理故障时,这将非常重要.此外,请确保使用urlencode()或http_build_query()构建查询字符串.这样,诸如空格之类的保留字符将被转换为它们的编码形式,例如%20.
| 归档时间: |
|
| 查看次数: |
165 次 |
| 最近记录: |