我正在尝试使用 Mailgun api 测试发送电子邮件。我正在使用 PHP 与 api 交互。以下是我尝试过的代码(来自 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 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04696144776569746861772a69656d6863716a2a6b7663" rel="noreferrer noopener nofollow">[email protected]</a>>',
'to' => 'Baz <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3153504b715449505c415d541f525e5c" rel="noreferrer noopener nofollow">[email protected]</a>>',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomness!'));
var_dump($result);
现在,当我尝试该 API 时,我收到类似于以下内容的响应:
stdClass Object ( [http_response_body] => stdClass Object ( [message] => Queued.
Thank you. [id] => <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="67565554535251505f5e575655545349565554534956555453522714060a170b0214490a060e0b00120949081500" rel="noreferrer noopener nofollow">[email protected]</a>> )
[http_response_code] => 200 )
如何使用 PHP 将此输出分配给数组或将其转换为简单的 JSON?是否有一些内置的 PHP 函数可以将上述输出格式化为简单的 JSON,或者我需要做其他事情吗?我有初级 PHP 技能。
任何帮助将不胜感激。谢谢!
P.S.:上面使用的 mailgun api key 来自 MailGun API 文档。
更新:谢谢大家。我成功了。
$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"];
最佳答案
你可以尝试php内置函数json_encode()。
关于php - MailGun API 不输出 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22114888/