2013-03-30 22 views
2

我正在使用Mandrill作爲電子郵件發送/入站服務和Laravel 3.x構建電子郵件客戶端(入站和出站發送)。使用Laravel的HTTPful庫檢索JSON響應

爲了發送消息,我在使用HTTPful bundleMandrill在我的郵件/撰寫POST方法中使用以下代碼。

$url = 'https://mandrillapp.com/api/1.0/messages/send.json'; 
$data = array( 
    'key' => '{removedAPIkey}', 
    'message' => array (
    'to' => array(array("email" => $_to)), 
    'from_name' => Auth::user()->name, 
    'from_email' => Auth::user()->email, 
    'subject' => $_subject, 
    'html' => $_body 
    ), 
    'async' => true 
     ); 

$request = Httpful::post($url)->sendsJson()->body($data)->send(); 

鏈接,以便更好地格式化代碼上面:http://paste.laravel.com/m79

現在據我可以從API日誌告訴,請求被正確地作出(與預期JSON)和以下格式的響應發回:

[ 
    { 
     "email": "[email protected]", 
     "status": "queued", 
     "_id": "longmessageID" 
    } 
] 

然而,我所要做的是從訪問請求(特別是_id屬性),這是在JSON的響應。現在據我所知,HTTPful類應該自動執行此操作(使用json_decode())。然而,訪問:

$request->_id; 

不工作,我不完全知道如何得到這個數據了(這是必需的,這樣我可以記錄這對於postmaster-軟反彈,硬反彈和拒絕郵件像功能)

任何援助,將不勝感激。

編輯

使用下面的代碼,導致郵件被髮送,但返回了一個錯誤:

$url = 'https://mandrillapp.com/api/1.0/messages/send.json'; 
$data = array( 
    'key' => '{removedAPIkey}', 
    'message' => array (
    'to' => array(array("email" => $_to)), 
    'from_name' => Auth::user()->name, 
    'from_email' => Auth::user()->email, 
    'subject' => $_subject, 
    'html' => $_body 
    ), 
    'async' => true 
     ); 

$request = Httpful::post($url)->sendsJson()->body($data)->send(); 

if ($request[0]->status == "queued") { 
    $success = true; 
} 

結果拋出異常:Cannot use object of type Httpful\Response as array

+2

請使用「回答自己的問題」編輯器將您的修復移至答案。這將幫助其他人輕鬆查看解決方案,以防他們需要它。 –

+1

同意邁克爾安東尼 –

+0

全部完成!感謝指出這個傢伙,完全忘了! –

回答

1

我必須說,非常感謝Aiias的幫助。我自己設法解決了這個問題(我必須花幾個小時看這個)。對於任何想知道的人來說,HTTPful包都有一個body數組,其中保存了響應。因此,下面的代碼工作:

$url = 'https://mandrillapp.com/api/1.0/messages/send.json'; 
$data = array( 
    'key' => '{removedAPIkey}', 
    'message' => array (
    'to' => array(array("email" => $_to)), 
    'from_name' => Auth::user()->name, 
    'from_email' => Auth::user()->email, 
    'subject' => $_subject, 
    'html' => $_body 
    ), 
    'async' => true 
     ); 

$request = Httpful::post($url)->sendsJson()->body($data)->send(); 

if ($request->body[0]->status == "queued") { 
    $success = true; 
} 

再次,非常感謝Aiias清除我的一些重大困惑!