【发布时间】:2016-04-24 15:52:57
【问题描述】:
我正在使用 SendGrid PHP 库 (https://sendgrid.com/docs/Integrate/Code_Examples/php.html)。
响应以 JSON 格式发送 - 例如应该是这样的:
{"message":"success"}
我可以通过以下方式发送一封简单的电子邮件:
<?php
$root="../../";
require $root . 'vendor/autoload.php';
$sendgrid = new SendGrid($SendGrid);
$email = new SendGrid\Email();
$email
//->addTo('you@me.com')
->addTo('you@me.com')
->setFrom('me@bar.com')
->setSubject('Subject goes here')
->setText('Hello World!')
->setHtml('<strong>Hello World!</strong>')
;
$res = $sendgrid->send($email);
?>
当我显示 $res 的输出时,例如使用 PHP-REF (https://github.com/digitalnature/php-ref) 我可以看到它看起来像这样:
看起来响应是一个对象 - 大概是 JSON?
但是,我无法以 JSON 格式访问数据,因为如果我尝试这样做:
$newtxt = json_decode($res);
我收到此错误:
警告:json_decode() 期望参数 1 为字符串,对象在 C:\xampp\htdocs\jim\001-jimpix\contact_old\test-send-grid.php 第 24 行中给出
如果我试试这个:
$j_array = json_decode($res, true);
我得到同样的错误。
我可以将“$res”值硬编码为:
$res = "{\"message\":\"success\"}";
然后就可以了。
但是,我不知道如何访问 SendGrid 返回的 JSON。
我尝试过各种方法,例如:
$res = json_decode(json_encode($res),TRUE);
大概有一种方法可以访问 SendGrid 返回的 JSON,因此我可以访问 JSON 数据。
但我不知道怎么做?
【问题讨论】: