sqs.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. /*
  3. * Copyright 2010-2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. /**
  17. *
  18. *
  19. * Amazon Simple Queue Service (Amazon SQS) offers a reliable, highly scalable, hosted queue for storing messages as they travel between
  20. * computers. By using Amazon SQS, developers can simply move data between distributed components of their applications that perform different
  21. * tasks, without losing messages or requiring each component to be always available. Amazon SQS makes it easy to build an automated workflow,
  22. * working in close conjunction with the Amazon Elastic Compute Cloud (Amazon EC2) and the other AWS infrastructure web services.
  23. *
  24. * Amazon SQS works by exposing Amazon's web-scale messaging infrastructure as a web service. Any computer on the Internet can add or read
  25. * messages without any installed software or special firewall configurations. Components of applications using Amazon SQS can run
  26. * independently, and do not need to be on the same network, developed with the same technologies, or running at the same time.
  27. *
  28. * Visit <a href="http://aws.amazon.com/sqs/">http://aws.amazon.com/sqs/</a> for more information.
  29. *
  30. * @version Tue Aug 23 12:51:53 PDT 2011
  31. * @license See the included NOTICE.md file for complete information.
  32. * @copyright See the included NOTICE.md file for complete information.
  33. * @link http://aws.amazon.com/sqs/Amazon Simple Queue Service
  34. * @link http://aws.amazon.com/documentation/sqs/Amazon Simple Queue Service documentation
  35. */
  36. class AmazonSQS extends CFRuntime
  37. {
  38. /*%******************************************************************************************%*/
  39. // CLASS CONSTANTS
  40. /**
  41. * Specify the default queue URL.
  42. */
  43. const DEFAULT_URL = 'sqs.us-east-1.amazonaws.com';
  44. /**
  45. * Specify the queue URL for the US-East (Northern Virginia) Region.
  46. */
  47. const REGION_US_E1 = self::DEFAULT_URL;
  48. /**
  49. * Specify the queue URL for the US-West (Northern California) Region.
  50. */
  51. const REGION_US_W1 = 'sqs.us-west-1.amazonaws.com';
  52. /**
  53. * Specify the queue URL for the EU (Ireland) Region.
  54. */
  55. const REGION_EU_W1 = 'sqs.eu-west-1.amazonaws.com';
  56. /**
  57. * Specify the queue URL for the Asia Pacific (Singapore) Region.
  58. */
  59. const REGION_APAC_SE1 = 'sqs.ap-southeast-1.amazonaws.com';
  60. /**
  61. * Specify the queue URL for the Asia Pacific (Japan) Region.
  62. */
  63. const REGION_APAC_NE1 = 'sqs.ap-northeast-1.amazonaws.com';
  64. /*%******************************************************************************************%*/
  65. // SETTERS
  66. /**
  67. * This allows you to explicitly sets the region for the service to use.
  68. *
  69. * @param string $region (Required) The region to use for subsequent Amazon S3 operations. [Allowed values: `AmazonSQS::REGION_US_E1 `, `AmazonSQS::REGION_US_W1`, `AmazonSQS::REGION_EU_W1`, `AmazonSQS::REGION_APAC_SE1`]
  70. * @return $this A reference to the current instance.
  71. */
  72. public function set_region($region)
  73. {
  74. $this->set_hostname($region);
  75. return $this;
  76. }
  77. /*%******************************************************************************************%*/
  78. // CONVENIENCE METHODS
  79. /**
  80. * Converts a queue URI into a queue ARN.
  81. *
  82. * @param string $queue_url (Required) The queue URL to perform the action on. Retrieved when the queue is first created.
  83. * @return string An ARN representation of the queue URI.
  84. */
  85. function get_queue_arn($queue_url)
  86. {
  87. return str_replace(
  88. array('http://', 'https://', '.amazonaws.com', '/', '.'),
  89. array('arn:aws:', 'arn:aws:', '', ':', ':'),
  90. $queue_url
  91. );
  92. }
  93. /**
  94. * Returns the approximate number of messages in the queue.
  95. *
  96. * @param string $queue_url (Required) The queue URL to perform the action on. Retrieved when the queue is first created.
  97. * @return mixed The Approximate number of messages in the queue as an integer. If the queue doesn't exist, it returns the entire <CFResponse> object.
  98. */
  99. public function get_queue_size($queue_url)
  100. {
  101. $response = $this->get_queue_attributes($queue_url, array(
  102. 'AttributeName' => 'ApproximateNumberOfMessages'
  103. ));
  104. if (!$response->isOK())
  105. {
  106. return $response;
  107. }
  108. return (integer) $response->body->Value(0);
  109. }
  110. /**
  111. * ONLY lists the queue URLs, as an array, on the SQS account.
  112. *
  113. * @param string $pcre (Optional) A Perl-Compatible Regular Expression (PCRE) to filter the names against.
  114. * @return array The list of matching queue names. If there are no results, the method will return an empty array.
  115. * @link http://php.net/pcre Perl-Compatible Regular Expression (PCRE) Docs
  116. */
  117. public function get_queue_list($pcre = null)
  118. {
  119. if ($this->use_batch_flow)
  120. {
  121. throw new SQS_Exception(__FUNCTION__ . '() cannot be batch requested');
  122. }
  123. // Get a list of queues.
  124. $list = $this->list_queues();
  125. if ($list = $list->body->QueueUrl())
  126. {
  127. $list = $list->map_string($pcre);
  128. return $list;
  129. }
  130. return array();
  131. }
  132. /*%******************************************************************************************%*/
  133. // CONSTRUCTOR
  134. /**
  135. * Constructs a new instance of <AmazonSQS>. If the <code>AWS_DEFAULT_CACHE_CONFIG</code> configuration
  136. * option is set, requests will be authenticated using a session token. Otherwise, requests will use
  137. * the older authentication method.
  138. *
  139. * @param string $key (Optional) Your AWS key, or a session key. If blank, it will look for the <code>AWS_KEY</code> constant.
  140. * @param string $secret_key (Optional) Your AWS secret key, or a session secret key. If blank, it will look for the <code>AWS_SECRET_KEY</code> constant.
  141. * @param string $token (optional) An AWS session token. If blank, a request will be made to the AWS Secure Token Service to fetch a set of session credentials.
  142. * @return boolean A value of <code>false</code> if no valid values are set, otherwise <code>true</code>.
  143. */
  144. public function __construct($key = null, $secret_key = null, $token = null)
  145. {
  146. $this->api_version = '2009-02-01';
  147. $this->hostname = self::DEFAULT_URL;
  148. if (!$key && !defined('AWS_KEY'))
  149. {
  150. // @codeCoverageIgnoreStart
  151. throw new SQS_Exception('No account key was passed into the constructor, nor was it set in the AWS_KEY constant.');
  152. // @codeCoverageIgnoreEnd
  153. }
  154. if (!$secret_key && !defined('AWS_SECRET_KEY'))
  155. {
  156. // @codeCoverageIgnoreStart
  157. throw new SQS_Exception('No account secret was passed into the constructor, nor was it set in the AWS_SECRET_KEY constant.');
  158. // @codeCoverageIgnoreEnd
  159. }
  160. if (defined('AWS_DEFAULT_CACHE_CONFIG') && AWS_DEFAULT_CACHE_CONFIG)
  161. {
  162. return parent::session_based_auth($key, $secret_key, $token);
  163. }
  164. return parent::__construct($key, $secret_key);
  165. }
  166. /*%******************************************************************************************%*/
  167. // SERVICE METHODS
  168. /**
  169. *
  170. * Returns a list of your queues.
  171. *
  172. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  173. * <li><code>QueueNamePrefix</code> - <code>string</code> - Optional - A string to use for filtering the list results. Only those queues whose name begins with the specified string are returned. </li>
  174. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  175. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  176. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  177. */
  178. public function list_queues($opt = null)
  179. {
  180. if (!$opt) $opt = array();
  181. return $this->authenticate('ListQueues', $opt, $this->hostname);
  182. }
  183. /**
  184. *
  185. * Sets an attribute of a queue. Currently, you can set only the <code>VisibilityTimeout</code> attribute for a queue.
  186. *
  187. * @param string $queue_url (Required) The URL of the SQS queue to take action on.
  188. * @param array $attribute (Required) A list of attributes to set. <ul>
  189. * <li><code>x</code> - <code>array</code> - This represents a simple array index. <ul>
  190. * <li><code>Name</code> - <code>string</code> - Optional - The name of the queue attribute to set a custom value for. [Allowed values: <code>Policy</code>, <code>VisibilityTimeout</code>, <code>MaximumMessageSize</code>, <code>MessageRetentionPeriod</code>, <code>ApproximateNumberOfMessages</code>, <code>ApproximateNumberOfMessagesNotVisible</code>, <code>CreatedTimestamp</code>, <code>LastModifiedTimestamp</code>]</li>
  191. * <li><code>Value</code> - <code>string</code> - Optional - The custom value to assign for the matching attribute key. </li>
  192. * </ul></li>
  193. * </ul>
  194. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  195. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  196. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  197. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  198. */
  199. public function set_queue_attributes($queue_url, $attribute, $opt = null)
  200. {
  201. if (!$opt) $opt = array();
  202. // Required parameter
  203. $opt = array_merge($opt, CFComplexType::map(array(
  204. 'Attribute' => (is_array($attribute) ? $attribute : array($attribute))
  205. )));
  206. return $this->authenticate('SetQueueAttributes', $opt, $queue_url);
  207. }
  208. /**
  209. *
  210. * The <code>ChangeMessageVisibility</code> action changes the visibility timeout of a specified message in a queue to a new value. The maximum
  211. * allowed timeout value you can set the value to is 12 hours. This means you can't extend the timeout of a message in an existing queue to
  212. * more than a total visibility timeout of 12 hours. (For more information visibility timeout, see <a
  213. * href="http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/AboutVT.html">Visibility Timeout</a> in the Amazon
  214. * SQS Developer Guide.)
  215. *
  216. * For example, let's say you have a message and its default message visibility timeout is 30 minutes. You could call
  217. * <code>ChangeMessageVisiblity</code> with a value of two hours and the effective timeout would be two hours and 30 minutes. When that time
  218. * comes near you could again extend the time out by calling ChangeMessageVisiblity, but this time the maximum allowed timeout would be 9 hours
  219. * and 30 minutes.
  220. *
  221. * If you attempt to set the <code>VisibilityTimeout</code> to an amount more than the maximum time left, Amazon SQS returns an error. It will
  222. * not automatically recalculate and increase the timeout to the maximum time remaining.
  223. *
  224. * Unlike with a queue, when you change the visibility timeout for a specific message, that timeout value is applied immediately but is not
  225. * saved in memory for that message. If you don't delete a message after it is received, the visibility timeout for the message the next time
  226. * it is received reverts to the original timeout value, not the value you set with the ChangeMessageVisibility action.
  227. *
  228. * @param string $queue_url (Required) The URL of the SQS queue to take action on.
  229. * @param string $receipt_handle (Required) The receipt handle associated with the message whose visibility timeout should be changed.
  230. * @param integer $visibility_timeout (Required) The new value (in seconds) for the message's visibility timeout.
  231. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  232. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  233. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  234. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  235. */
  236. public function change_message_visibility($queue_url, $receipt_handle, $visibility_timeout, $opt = null)
  237. {
  238. if (!$opt) $opt = array();
  239. $opt['ReceiptHandle'] = $receipt_handle;
  240. $opt['VisibilityTimeout'] = $visibility_timeout;
  241. return $this->authenticate('ChangeMessageVisibility', $opt, $queue_url);
  242. }
  243. /**
  244. *
  245. * The <code>CreateQueue</code> action creates a new queue, or returns the URL of an existing one. When you request <code>CreateQueue</code>,
  246. * you provide a name for the queue. To successfully create a new queue, you must provide a name that is unique within the scope of your own
  247. * queues. If you provide the name of an existing queue, a new queue isn't created and an error isn't returned. Instead, the request succeeds
  248. * and the queue URL for the existing queue is returned.
  249. *
  250. * If you provide a value for <code>DefaultVisibilityTimeout</code> that is different from the value for the existing queue, you receive an
  251. * error.
  252. *
  253. * @param string $queue_name (Required) The name for the queue to be created.
  254. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  255. * <li><code>DefaultVisibilityTimeout</code> - <code>integer</code> - Optional - The visibility timeout (in seconds) to use for the created queue. </li>
  256. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  257. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  258. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  259. */
  260. public function create_queue($queue_name, $opt = null)
  261. {
  262. if (!$opt) $opt = array();
  263. $opt['QueueName'] = $queue_name;
  264. return $this->authenticate('CreateQueue', $opt, $this->hostname);
  265. }
  266. /**
  267. *
  268. * The <code>RemovePermission</code> action revokes any permissions in the queue policy that matches the specified <code>Label</code>
  269. * parameter. Only the owner of the queue can remove permissions.
  270. *
  271. * @param string $queue_url (Required) The URL of the SQS queue to take action on.
  272. * @param string $label (Required) The identfication of the permission to remove. This is the label added with the AddPermission operation.
  273. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  274. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  275. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  276. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  277. */
  278. public function remove_permission($queue_url, $label, $opt = null)
  279. {
  280. if (!$opt) $opt = array();
  281. $opt['Label'] = $label;
  282. return $this->authenticate('RemovePermission', $opt, $queue_url);
  283. }
  284. /**
  285. *
  286. * Gets one or all attributes of a queue. Queues currently have two attributes you can get: <code>ApproximateNumberOfMessages</code> and
  287. * <code>VisibilityTimeout</code>.
  288. *
  289. * @param string $queue_url (Required) The URL of the SQS queue to take action on.
  290. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  291. * <li><code>AttributeName</code> - <code>string|array</code> - Optional - A list of attributes to retrieve information for. Pass a string for a single value, or an indexed array for multiple values. </li>
  292. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  293. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  294. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  295. */
  296. public function get_queue_attributes($queue_url, $opt = null)
  297. {
  298. if (!$opt) $opt = array();
  299. // Optional parameter
  300. if (isset($opt['AttributeName']))
  301. {
  302. $opt = array_merge($opt, CFComplexType::map(array(
  303. 'AttributeName' => (is_array($opt['AttributeName']) ? $opt['AttributeName'] : array($opt['AttributeName']))
  304. )));
  305. unset($opt['AttributeName']);
  306. }
  307. return $this->authenticate('GetQueueAttributes', $opt, $queue_url);
  308. }
  309. /**
  310. *
  311. * The AddPermission action adds a permission to a queue for a specific <a
  312. * href="http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/APIReference/Glossary.html#d0e3892">principal</a>. This allows for
  313. * sharing access to the queue.
  314. *
  315. * When you create a queue, you have full control access rights for the queue. Only you (as owner of the queue) can grant or deny permissions
  316. * to the queue. For more information about these permissions, see <a
  317. * href="http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/?acp-overview.html">Shared Queues</a> in the Amazon
  318. * SQS Developer Guide.
  319. *
  320. * <code>AddPermission</code> writes an SQS-generated policy. If you want to write your own policy, use SetQueueAttributes to upload your
  321. * policy. For more information about writing your own policy, see <a
  322. * href="http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/?AccessPolicyLanguage.html">Appendix: The Access
  323. * Policy Language</a> in the Amazon SQS Developer Guide.
  324. *
  325. * @param string $queue_url (Required) The URL of the SQS queue to take action on.
  326. * @param string $label (Required) The unique identification of the permission you're setting (e.g., <code>AliceSendMessage</code>). Constraints: Maximum 80 characters; alphanumeric characters, hyphens (-), and underscores (_) are allowed.
  327. * @param string|array $account_id (Required) The AWS account number of the principal who will be given permission. The principal must have an AWS account, but does not need to be signed up for Amazon SQS. Pass a string for a single value, or an indexed array for multiple values.
  328. * @param string|array $action_name (Required) The action the client wants to allow for the specified principal. Pass a string for a single value, or an indexed array for multiple values.
  329. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  330. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  331. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  332. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  333. */
  334. public function add_permission($queue_url, $label, $account_id, $action_name, $opt = null)
  335. {
  336. if (!$opt) $opt = array();
  337. $opt['Label'] = $label;
  338. // Required parameter
  339. $opt = array_merge($opt, CFComplexType::map(array(
  340. 'AWSAccountId' => (is_array($account_id) ? $account_id : array($account_id))
  341. )));
  342. // Required parameter
  343. $opt = array_merge($opt, CFComplexType::map(array(
  344. 'ActionName' => (is_array($action_name) ? $action_name : array($action_name))
  345. )));
  346. return $this->authenticate('AddPermission', $opt, $queue_url);
  347. }
  348. /**
  349. *
  350. * This action unconditionally deletes the queue specified by the queue URL. Use this operation WITH CARE! The queue is deleted even if it is
  351. * NOT empty.
  352. *
  353. * Once a queue has been deleted, the queue name is unavailable for use with new queues for 60 seconds.
  354. *
  355. * @param string $queue_url (Required) The URL of the SQS queue to take action on.
  356. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  357. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  358. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  359. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  360. */
  361. public function delete_queue($queue_url, $opt = null)
  362. {
  363. if (!$opt) $opt = array();
  364. return $this->authenticate('DeleteQueue', $opt, $queue_url);
  365. }
  366. /**
  367. *
  368. * The <code>DeleteMessage</code> action unconditionally removes the specified message from the specified queue. Even if the message is locked
  369. * by another reader due to the visibility timeout setting, it is still deleted from the queue.
  370. *
  371. * @param string $queue_url (Required) The URL of the SQS queue to take action on.
  372. * @param string $receipt_handle (Required) The receipt handle associated with the message to delete.
  373. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  374. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  375. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  376. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  377. */
  378. public function delete_message($queue_url, $receipt_handle, $opt = null)
  379. {
  380. if (!$opt) $opt = array();
  381. $opt['ReceiptHandle'] = $receipt_handle;
  382. return $this->authenticate('DeleteMessage', $opt, $queue_url);
  383. }
  384. /**
  385. *
  386. * The <code>SendMessage</code> action delivers a message to the specified queue.
  387. *
  388. * @param string $queue_url (Required) The URL of the SQS queue to take action on.
  389. * @param string $message_body (Required) The message to send.
  390. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  391. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  392. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  393. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  394. */
  395. public function send_message($queue_url, $message_body, $opt = null)
  396. {
  397. if (!$opt) $opt = array();
  398. $opt['MessageBody'] = $message_body;
  399. return $this->authenticate('SendMessage', $opt, $queue_url);
  400. }
  401. /**
  402. *
  403. * Retrieves one or more messages from the specified queue, including the message body and message ID of each message. Messages returned by
  404. * this action stay in the queue until you delete them. However, once a message is returned to a <code>ReceiveMessage</code> request, it is not
  405. * returned on subsequent <code>ReceiveMessage</code> requests for the duration of the <code>VisibilityTimeout</code>. If you do not specify a
  406. * <code>VisibilityTimeout</code> in the request, the overall visibility timeout for the queue is used for the returned messages.
  407. *
  408. * @param string $queue_url (Required) The URL of the SQS queue to take action on.
  409. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  410. * <li><code>AttributeName</code> - <code>string|array</code> - Optional - A list of attributes to retrieve information for. Pass a string for a single value, or an indexed array for multiple values. </li>
  411. * <li><code>MaxNumberOfMessages</code> - <code>integer</code> - Optional - The maximum number of messages to return. Amazon SQS never returns more messages than this value but may return fewer. All of the messages are not necessarily returned. </li>
  412. * <li><code>VisibilityTimeout</code> - <code>integer</code> - Optional - The duration (in seconds) that the received messages are hidden from subsequent retrieve requests after being retrieved by a <code>ReceiveMessage</code> request. </li>
  413. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  414. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  415. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  416. */
  417. public function receive_message($queue_url, $opt = null)
  418. {
  419. if (!$opt) $opt = array();
  420. // Optional parameter
  421. if (isset($opt['AttributeName']))
  422. {
  423. $opt = array_merge($opt, CFComplexType::map(array(
  424. 'AttributeName' => (is_array($opt['AttributeName']) ? $opt['AttributeName'] : array($opt['AttributeName']))
  425. )));
  426. unset($opt['AttributeName']);
  427. }
  428. return $this->authenticate('ReceiveMessage', $opt, $queue_url);
  429. }
  430. }
  431. /*%******************************************************************************************%*/
  432. // EXCEPTIONS
  433. /**
  434. * Default SQS Exception.
  435. */
  436. class SQS_Exception extends Exception {}