简体   繁体   中英

Retrieving JSON response using HTTPful library for Laravel

I am currently building a e-mail client (inbound and outbound sending) using Mandrill as the e-mail sending / inbound service and Laravel 3.x.

In order to send messages, I am using the HTTPful bundle with the Mandrill using the following code in my mail/compose POST method.

$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();

Link to better formatted code above: http://paste.laravel.com/m79

Now as far as I can tell from the API log, the request is correctly made (with the expected JSON) and a response of the following format is sent back:

[
  {
      "email": "test@test.com",
      "status": "queued",
      "_id": "longmessageID"
  }
]

However, what I am trying to do is access the response from the request (specifically the _id attribute), which is in JSON. Now as far as I'm aware, the HTTPful class should do this automatically (using json_decode()). However, accessing:

$request->_id;

is not working and I'm not entirely sure how to get this data out (it is required so I can record this for soft-bounce, hard-bounce and rejection messages for postmaster-like functionality)

Any assistance would be appreciated.

Edit

Using the following code, results in the mail being sent but an error returned:

$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;
}

Results in an exception being thrown: Cannot use object of type Httpful\\Response as array

I must say, a huge thanks to Aiias for his assistance. I managed to fix this myself (I must have spent hours looking at this). For anyone who wants to know, the HTTPful bundle has a body array, where the response is kept. Therefore, the code below works:

$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;
}

Again, huge thanks to Aiias for clearing some major confusion up for me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM