2013-10-13 51 views
-1
{ "status_code": 200, "status_txt": "OK", "data": { "long_url": "http:\/\/google.com\/", "url": "http:\/\/bit.ly\/17A2GVj", "hash": "17A2GVj", "global_hash": "3j4ir4", "new_hash": 0 } } 

我得到這樣的事情我做了這是什麼數據類型(GET請求)bit.ly的oauth2

$curl = curl_init(); 
// Set some options - we are passing in a useragent too here 
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_URL => 'https://api-ssl.bitly.com/v3/shorten?access_token=xxx&longUrl=http%3A%2F%2Fgoogle.com%2F', 
    CURLOPT_USERAGENT => 'Mozilla/4.0' 
)); 
// Send the request & save response to $resp 
$resp = curl_exec($curl); 
// Close request to clear up some resources 
curl_close($curl); 

echo $resp; 

後如何閱讀 「哈希」

+1

['json_decode'](http://php.net/manual/en/function.json-decode.php)on'$ resp' – pNre

+0

@pNre我該如何閱讀該項目?我必須首先喜歡循環對象(stdClass)嗎? – CodeGuru

+2

'$ object-> data-> hash' – pNre

回答

2
$respObj = json_decode($resp); 
echo $respObj->data->hash; 

json_decode ,默認情況下,返回stdClass的實例。訪問stdClass'會員->

+0

數據是否代表任何形式的[「something」],無論它在哪個對象中? – CodeGuru

+0

@RainbowHat如果我正確地理解了你的問題,那麼在這種情況下''data'特定於'$ respObj'中的對象結構。你也可以通過'$ respObj-> status_code'訪問200的狀態碼,這與'data'無關。你也在混合數組和對象表示法,'[「something」]'與' - > something'(分別)不同。 – zamnuts