使用 Laravel 的 HTTPful 库检索 JSON 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15720450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving JSON response using HTTPful library for Laravel
提问by Matthew Gall
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.
我目前正在使用 Mandrill 作为电子邮件发送/入站服务和 Laravel 3.x 构建电子邮件客户端(入站和出站发送)。
In order to send messages, I am using the HTTPful bundlewith the Mandrillusing the following code in my mail/compose POST method.
为了发送消息,我在邮件/撰写 POST 方法中使用以下代码将HTTPful 捆绑包与Mandrill 一起使用。
$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
链接到上述格式更好的代码: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:
现在,据我从 API 日志中可以看出,请求已正确发出(使用预期的 JSON)并发送回以下格式的响应:
[
{
"email": "[email protected]",
"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:
但是,我想要做的是访问来自请求的响应(特别是 _id 属性),这是在 JSON 中。现在据我所知,HTTPful 类应该自动执行此操作(使用 json_decode())。但是,访问:
$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)
不工作,我不完全确定如何获取此数据(这是必需的,因此我可以记录此信息以用于类似 postmaster 的功能的软退回、硬退回和拒绝消息)
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
导致抛出异常: Cannot use object of type Httpful\Response as array
采纳答案by Matthew Gall
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:
我必须说,非常感谢 Aiias 的帮助。我设法自己解决了这个问题(我一定花了几个小时看这个)。对于任何想知道的人来说,HTTPful 包有一个主体数组,其中保存了响应。因此,下面的代码有效:
$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!
再次,非常感谢 Aiias 为我解决了一些主要的困惑!