2013-05-28 146 views
0

我真的不明白這個API應該如何工作,因爲我以前從未使用過JSON。幫助使用JSON的API

該文檔沒有給出任何示例,但它表示它在此API中的端點支持POST和GET操作,並返回JSON。

我的問題是,我不知道究竟是如何實現這一點,讓我們說我只想把所有的數據到一個簡單的頁面,比如這個:

市:塞勒姆

郵編:

等等

我不太肯定這個開始:

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

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

對於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 
    } 
} 
} 
} 
+0

什麼你用來做這個請求的語言? –

+0

[如何與Web XML/JSON APIs進行交互?](http://stackoverflow.com/questions/5503604/how-to-interface-with-web-xml-json-apis) – PleaseStand

回答

5

的第一件事是嘗試實驗在Web瀏覽器中的幾個請求。從那裏,應該很清楚你需要做什麼。

開始用自己的基本網址:

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

很明顯,你必須在你的主機名,以堵塞的地方[your RepMan hostname]。從那裏,讓我們添加一個查詢字符串。你以前見過這些...他們在URL中的問號?之後,並且包含key1=value1&key2=value2形式的鍵/值對。你有4個變量插件:pidapiKey,sridcustomerId。不知道這是什麼網絡服務呢,這是很難幫助你知道什麼值插上,但這裏有一個例子:

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

手工打造自己與你想要的參數工作URL,並嘗試你的瀏覽器。一旦你完成了,你會看到一些文本結構在JSON format回來。這是與JavaScript解析器兼容的文本,但實際上它與JavaScript分開。

現在,你如何得到這在PHP?快速的方法是使用file_get_contents()json_decode()

$response = file_get_contents('plug your URL in here'); 
$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()來構建您的查詢字符串。這樣,reserved characters(如空格)將被轉換爲其編碼形式,如%20

+1

您正在做上帝的工作,謝謝!這正是我需要的:) – Xhynk

+1

如果你喜歡用你的數據作爲關聯數組,只需將'json_decode($ response);'改爲'json_decode($ response,true);' – TecBrat

2

的URL描述實體:

http://[your RepMan主機名]/API/v1/account/reputation/current.json - 用戶當前聲譽

使用HTTP GET您可以檢索(獲取)有關實體的數據。

使用HTTP POST 可以提交(後)數據關於實體的API(更新或創建一個新的實體)。

要做到這一點只需使用捲曲在其他許多地方這樣解釋: php: Get url content (json) with cURL

0

這看起來像一個基於REST的web服務。 有兩種HTTP方法的API說,你可以使用:

  • GET
  • POST

GET請求是在例如可以從這樣的網絡瀏覽器稱爲網址:

http://host//api/v1/account/reputation/current.json?pid={some partner ID}&nextkey={next value}... 

問號(?)表示參數列表開始。 &符(&)分隔參數。參數是key =值格式。

POST請求使用相同的URL,但不是參數,你捆起來的內容:

http://host//api/v1/account/reputation/current.json 

然後設置HTTP標頭 「content-type」 爲 「application/json」。 然後將HTTP標頭「accept」設置爲「application/json」。

然後,使用的一些軟件(Apache的HTTP客戶端等),建立和POST JSON格式消息爲:

{ 
    pid: "my partner ID" 
    ... 
} 

參考文獻:

http://en.wikipedia.org/wiki/Query_string

http://en.wikipedia.org/wiki/JSON

What is the difference between a HTTP-Get and HTTP-POST and why is HTTP-POST weaker in terms of security