【问题标题】:how to get response in json format using twilio sdk如何使用 twilio sdk 以 json 格式获取响应
【发布时间】:2017-04-19 03:27:03
【问题描述】:

我正在使用 Twilio 的 PHP SDK 发送短信。下面是代码:

<?php
// Required if your environment does not handle autoloading
require './autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'XXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'XXXXXXXXXXXXXXXXXXXX';
$client = new Client($sid, $token);

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    '+XXXXXXXXXX',
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => '+XXXXXXXX',
        // the body of the text message you'd like to send
        'body' => 'Hey Jenny! Good luck on the bar exam!'
    )
);

**Response:
[Twilio.Api.V2010.MessageInstance accountSid=XXXXXXXXXXXXXXXX sid=XXXXXXXXXXXXXXX]**

如何获得 JSON 格式的响应? 任何帮助表示赞赏。

【问题讨论】:

  • 任何人请..
  • 您可以自己构造 API 调用以获取 JSON 响应。我能问一下您为什么需要 JSON 以及为什么帮助程序库没有给您所需的对象吗?
  • 来自 Twilio 文档:“Json:如果您的函数返回有效的 Json,您应该能够通过 widgets.MY_WIDGET_NAME.parsed 访问它”

标签: php twilio twilio-api twilio-php


【解决方案1】:

快速回答是:

$json_string = json_encode(</POST|GET>);

使用$_POST$_GET super globals 会得到一个json 字符串格式。

例如

/*
 * Imagine this is the POST request
 *
 * $_POST = [
 *     'foo' => 'Hello',
 *     'bar' => 'World!'
 * ];
 *
 */


$json_string = json_encode($_POST); // You get → {"foo":"Hello","bar":"World!"}

通过这种方式,您可以将值编码为 JSON 表示形式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 2016-11-13
    • 2015-12-28
    • 2023-04-03
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多