简体   繁体   中英

how to get response in json format using php sdk from twilio sms api

I need response in json format which I am getting as

 class Services_Twilio_Rest_Message

I need the response returned after sending SMS in following json:

{
  "sid": "sid",
  "date_created": "Tue, 20 Oct 2015 06:01:14 +0000",
  "date_updated": "Tue, 20 Oct 2015 06:01:14 +0000",
  "date_sent": null,
  "account_sid": "AccountSid",
  "to": "+91999999999",
  "from": "+18989898989",
  "body": "I am trying to send a message having characters more than 160. But twilio allows concatenated messages upto 1600 characters length, i thought i should give it a try. So i wrote this weird message.",
  "status": "queued",
  "num_segments": "2",
  "num_media": "0",
  "direction": "outbound-api",
  "api_version": "2010-04-01",
  "price": null,
  "price_unit": "USD",
  "error_code": null,
  "error_message": null,
  "uri": "/2010-04-01/Accounts/AccountSid/Messages/SM745b97dcb56a4f9f82c52242ca3b5e92.json",
  "subresource_uris": {
    "media": "/2010-04-01/Accounts/AccountSid/Messages/SM745b97dcb56a4f9f82c52242ca3b5e92/Media.json"
  }
}

Ricky from Twilio here.

The Services_Twilio_Rest_Message object has an toString method that will return the json representation object. If you simply want to output the json for debugging you can just echo the response you get:

$message = $client->account->messages->create(array(
    "From" => "XXX-XXX-XXXX",
    "To" => "XXX-XXX-XXXX",
    "Body" => "Test message!",
));

echo $message;

If you need to store that string in a variable you can explicitly cast $message to a string:

$json = (string)$message;

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM