2014-01-20 36 views
0

我使用用PHP的tumblr API V2,它的工作,但這樣的代碼:tumblr JSON元用PHP

<?php 
require 'vendor/autoload.php'; 
// Authenticate via OAuth 
$client = new Tumblr\API\Client(
    'my', 
    'key', 
    'is', 
    'good' 
); 
// Make the request 
$post=$client->getUserInfo(); 
echo(json_encode($post)); 
?> 

會是這樣(https://api.tumblr.com/console

{ 
    "meta": { 
     "status": 200, 
     "msg": "OK" 
    }, 
    "response": { ... } 
} 

和我有隻有沒有meta的響應部分:

{"user":{"name":"my name","likes":0,… }} 

如何獲得元?我嘗試$ post-> meta和$ client-> meta但是這是愚蠢的測試...

回答

0

的關鍵是線383和Client.php

private function getRequest($path, $options, $addApiKey) 
{ 
    $response = $this->makeRequest('GET', $path, $options, $addApiKey); 

    return $this->parseResponse($response); 
} 

421和

private function parseResponse($response) 
{ 
    $response->json = json_decode($response->body); 
    if ($response->status < 400) { 
     return $response->json->response; 
    } else { 
     throw new RequestException($response); 
    } 
} 

每次給你打電話讓你得到它之前被解析。除非出現錯誤,否則你只會得到迴應,從來沒有迴應,在這種情況下,你將會得到一個例外。

所以,在說「這是愚蠢的」之前閱讀代碼。

+0

謝謝蒂姆......不,我不談論代碼,但我的愚蠢測試。 –

1

根據this行你不應該擔心元數據。 PHP客戶端負責處理任何400以上的響應代碼,並拋出一個Tumblr\API\RequestException。所以,你真正想要做的就是:

<?php 
require 'vendor/autoload.php'; 
try { 
    // Authenticate via OAuth 
    $client = new Tumblr\API\Client(
    'my', 
    'key', 
    'is', 
    'good' 
); 
    $post=$client->getUserInfo(); 
    echo(json_encode($post)); 
} catch(Tumblr\API\RequestException $e) { 
    //handle na exception here 
} 
?> 
+0

謝謝vooD。 –

+0

如果您能接受我的回答,我將不勝感激。 – vooD

+0

我不能接受2個答案... –