dongqian7545 2014-03-01 13:18
浏览 47
已采纳

MailGun API不输出JSON

I am trying to test sending emails using the Mailgun api. I am using PHP to interface with the api. Following is the code that I have tried (from here).

# Include the Autoloader (see "Libraries" for install instructions)
require 'vendor/autoload.php';
use Mailgun\Mailgun;

# Instantiate the client.
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
$domain = "samples.mailgun.org";

# Make the call to the client.
$result = $mgClient->sendMessage("$domain",
              array('from'    => 'Excited User <me@samples.mailgun.org>',
                    'to'      => 'Baz <baz@example.com>',
                    'subject' => 'Hello',
                    'text'    => 'Testing some Mailgun awesomness!'));
var_dump($result); 

Now when I try the API, I get a response similar to the following:

stdClass Object ( [http_response_body] => stdClass Object ( [message] => Queued. 
Thank you. [id] => <12345678901234.1234.12345@samples.mailgun.org> )
[http_response_code] => 200 )

How do I assign this output to an array or convert this to simple JSON using PHP? Is there some in-built PHP function which would format the above output to simple JSON or do I need to do something else. I have beginner level PHP skills.

Any help would be very appreciated. Thanks!

P.S.: The mailgun api key used above is from the MailGun API documentation.

UPDATE: Thanks guys. I got it working.

$darr=json_encode($result);
$data=  json_decode($darr,true);

# Prints out the individual elements of the array
echo $data["http_response_body"]["message"]."<br>";
echo $data["http_response_body"]["id"]."<br>";
echo $data["http_response_code"];
  • 写回答

1条回答 默认 最新

  • dtgta48604 2014-03-11 17:58
    关注

    You could try the php built-in function json_encode().

    http://us3.php.net/json_encode

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行