Messages.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. class Services_Twilio_Rest_Messages extends Services_Twilio_ListResource {
  3. /**
  4. * Create a new Message instance
  5. *
  6. * Example usage:
  7. *
  8. * .. code-block:: php
  9. *
  10. * $client->account->messages->create(array(
  11. * "Body" => "foo",
  12. * "From" => "+14105551234",
  13. * "To" => "+14105556789",
  14. * ));
  15. *
  16. * :param array $params: a single array of parameters which is serialized and
  17. * sent directly to the Twilio API. You may find it easier to use the
  18. * sendMessage helper instead of this function.
  19. *
  20. */
  21. public function create($params = array()) {
  22. return parent::_create($params);
  23. }
  24. /**
  25. * Send a message
  26. *
  27. * .. code-block:: php
  28. *
  29. * $client = new Services_Twilio('AC123', '123');
  30. * $message = $client->account->messages->sendMessage(
  31. * '+14105551234', // From a Twilio number in your account
  32. * '+14105556789', // Text any number
  33. * 'Come at the king, you best not miss.' // Message body (if any)
  34. * array('https://demo.twilio.com/owl.png'), // An array of MediaUrls
  35. * );
  36. *
  37. * :param string $from: the from number for the message, this must be a
  38. * number you purchased from Twilio
  39. * :param string $to: the message recipient's phone number
  40. * :param $mediaUrls: the URLs of images to send in this MMS
  41. * :type $mediaUrls: null (don't include media), a single URL, or an array
  42. * of URLs to send as media with this message
  43. * :param string $body: the text to include along with this MMS
  44. * :param array $params: Any additional params (callback, etc) you'd like to
  45. * send with this request, these are serialized and sent as POST
  46. * parameters
  47. *
  48. * :return: The created :class:`Services_Twilio_Rest_Message`
  49. * :raises: :class:`Services_Twilio_RestException`
  50. * An exception if the parameters are invalid (for example, the from
  51. * number is not a Twilio number registered to your account, or is
  52. * unable to send MMS)
  53. */
  54. public function sendMessage($from, $to, $body = null, $mediaUrls = null,
  55. $params = array()
  56. ) {
  57. $postParams = array(
  58. 'From' => $from,
  59. 'To' => $to,
  60. );
  61. // When the request is made, this will get serialized into MediaUrl=a&MediaUrl=b
  62. if (!is_null($mediaUrls)) {
  63. $postParams['MediaUrl'] = $mediaUrls;
  64. }
  65. if (!is_null($body)) {
  66. $postParams['Body'] = $body;
  67. }
  68. return self::create($postParams + $params);
  69. }
  70. }