2017-08-25 50 views
0

如何將此代碼轉換爲PHP到我的腳本中?如何使用PHP對使用XAPP令牌的API進行身份驗證?

curl -v -X POST "https://api.artsy.net/api/tokens/xapp_token?client_id=...&client_secret=..." 

我有client_id和client_secret。

一旦我有了這個標記,我將如何使用它來獲取這個示例鏈接中的信息(https://api.artsy.net:443/api/artworks/4d8b92eb4eb68a1b2c000968)?如果我嘗試Postman這個URL請求測試,我得到這個:

{ 
    "type": "auth_error", 
    "message": "An application token or a user is required." 
} 

API Docs page,但它說的基本網址爲:https://api.artsy.net/api。我只是不確定如何獲取令牌,然後插入它以獲取API數據。

任何幫助將深表感謝!

回答

0

你可以只是谷歌'捲曲'。 但無論如何,這裏我們去:

<?php 

$postdata = array(); 
$postdata['client_id'] = CLIENT_ID; 
$postdata['client_secret'] = CLIENT_SECRET; 


$cc = curl_init(); 
curl_setopt($cc,CURLOPT_POST,1); // set method to POST 
curl_setopt($cc,CURLOPT_RETURNTRANSFER,1); // get the data returned 
curl_setopt($cc,CURLOPT_URL,"https://api.artsy.net/api/tokens/xapp_token"); 
curl_setopt($cc,CURLOPT_POSTFIELDS,$postdata); 
$result = curl_exec($cc); 

echo $result; 

?> 
+0

謝謝你的快速週轉!我已經看了一下php curl文檔,但我仍然在如何隔離令牌,然後用它來驗證我的請求來檢索API數據... – Sam

+0

@Sam直到令牌過期多久?如果令牌根本沒有到期,您可以手動獲取並直接使用它,而不必每次都要求它。 –

+0

令牌過期: 「expires_at」:「2017-09-01T12:29:40 + 00:00」 – Sam

相關問題