閱讀239 返回首頁    go 阿裏雲


PHP 收發消息__HTTP 接入(簡單)_消息隊列 MQ-阿裏雲

本文描述如何在 PHP 環境下用 HTTP 協議收發消息。


運行環境準備

用 HTTP 協議發送或者接收消息,請完成以下準備工作。

Windows

  1. 從 IntelliJ 官網下載並安裝 phpStorm 試用版:https://www.jetbrains.com/phpstorm/download/index.html
  2. 安裝完畢之後,打開 phpStorm 開發環境。

其他 IDE 開發環境安裝步驟與此類似。

Linux/Unix

  1. 從官網上下載 phpStorm 的 Linux 版本:https://www.jetbrains.com/phpstorm/download/index.html

  2. 解壓下載成功的 phpStorm 安裝包: tar xfz PhpStorm-2016.1.tar.gz

  3. 進入 phpStorm 的 bin 目錄並執行安裝腳本: cd phpStorm-2016.1;./phpStorm.sh

  4. 在輸入注冊碼頁麵直接單擊試用版

  5. 單擊確定直到出現安裝完成界麵。


運行示例代碼

在Windows/Linux/Unix環境下,請按照以下步驟運行示例代碼。

  1. 在 phpStorm 中創建 PHP 工程(工程名無特殊要求)。

  2. 將下文具體示例程序中所提供的配置文件(config.ini)以及示例程序(httpProducer.php, httpConsumer.php, Util.php)拷貝到當前的工程中,如圖:

  3. 根據示例代碼裏的說明修改相關配置信息。

  4. 右鍵點擊創建的 PHP 文件,選擇 Run 執行。

    注意:請先執行 httpProducer.php 代碼,再執行 httpConsumer.php 代碼。

  5. 觀察執行結果,如果執行結果有問題,請檢查 config.ini 配置是否正確。


具體示例程序

以下是配置文件(config.ini)、發送消息(httpProducer.php)和接收消息(httpConsumer.php)以及所用工具方法(Util.php)的示例代碼。

1. 配置文件

您需要設置配置文件(config.ini)的相關內容,具體請參考申請 MQ 資源

  1. [section]
  2. #您在控製台創建的Topic
  3. Topic = "xxx"
  4. #公測環境的URL
  5. URL = "https://publictest-rest.ons.aliyun.com"
  6. #阿裏雲身份驗證碼
  7. Ak = "xxx"
  8. #阿裏雲身份驗證密鑰
  9. Sk = "xxx"
  10. #MQ控製台創建的Producer ID
  11. ProducerID = "xxx"
  12. #MQ控製台創建的Consumer ID
  13. ConsumerID = "xxx"

說明: URL 中的 Key,Tag 以及 POST Content-Type 沒有任何的限製,隻要確保 Key 和 Tag 相同唯一即可,可以放在 user.properties 裏麵。

2. 發送消息示例程序(httpProducer.php)

通過 HTTP 協議發送消息,請參考以下示例代碼。

  1. <?php
  2. //包含工具類
  3. include("Util.php");
  4. /*
  5. * 消息發布者者
  6. */
  7. class HttpProducer
  8. {
  9. //簽名
  10. private static $signature = "Signature";
  11. //在MQ控製台創建的Producer ID
  12. private static $producerid = "ProducerID";
  13. //阿裏雲身份驗證碼
  14. private static $aks = "AccessKey";
  15. //配置信息
  16. private static $configs = null;
  17. //構造函數
  18. function __construct()
  19. {
  20. //讀取配置信息
  21. $this::$configs = parse_ini_file("config.ini");
  22. }
  23. //計算md5
  24. private function md5($str)
  25. {
  26. return md5($str);
  27. }
  28. //發布消息流程
  29. public function process()
  30. {
  31. //打印配置信息
  32. var_dump($this::$configs);
  33. //獲取Topic
  34. $topic = $this::$configs["Topic"];
  35. //獲取保存Topic的URL路徑
  36. $url = $this::$configs["URL"];
  37. //讀取阿裏雲訪問碼
  38. $ak = $this::$configs["Ak"];
  39. //讀取阿裏雲密鑰
  40. $sk = $this::$configs["Sk"];
  41. //讀取Producer ID
  42. $pid = $this::$configs["ProducerID"];
  43. //HTTP請求體內容
  44. $body = utf8_encode("阿裏巴巴");
  45. $newline = "n";
  46. //構造工具對象
  47. $util = new Util();
  48. for ($i = 0; $i<500; $i++) {
  49. //計算時間戳
  50. $date = time()*1000;
  51. //POST請求url
  52. $postUrl = $url."/message/?topic=".$topic."&time=".$date."&tag=http&key=http";
  53. //簽名字符串
  54. $signString = $topic.$newline.$pid.$newline.$this->md5($body).$newline.$date;
  55. //計算簽名
  56. $sign = $util->calSignatue($signString,$sk);
  57. //初始化網絡通信模塊
  58. $ch = curl_init();
  59. //構造簽名標記
  60. $signFlag = $this::$signature.":".$sign;
  61. //構造密鑰標記
  62. $akFlag = $this::$aks.":".$ak;
  63. //標記
  64. $producerFlag = $this::$producerid.":".$pid;
  65. //構造HTTP請求頭部內容類型標記
  66. $contentFlag = "Content-Type:text/html;charset=UTF-8";
  67. //構造HTTP請求頭部
  68. $headers = array(
  69. $signFlag,
  70. $akFlag,
  71. $producerFlag,
  72. $contentFlag,
  73. );
  74. //設置HTTP頭部內容
  75. curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
  76. //設置HTTP請求類型,此處為POST
  77. curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST");
  78. //設置HTTP請求的URL
  79. curl_setopt($ch,CURLOPT_URL,$postUrl);
  80. //設置HTTP請求的body
  81. curl_setopt($ch,CURLOPT_POSTFIELDS,$body);
  82. //構造執行環境
  83. ob_start();
  84. //開始發送HTTP請求
  85. curl_exec($ch);
  86. //獲取請求應答消息
  87. $result = ob_get_contents();
  88. //清理執行環境
  89. ob_end_clean();
  90. //打印請求應答結果
  91. var_dump($result);
  92. //關閉連接
  93. curl_close($ch);
  94. }
  95. }
  96. }
  97. //構造消息發布者
  98. $producer = new HttpProducer();
  99. //啟動消息發布者
  100. $producer->process();
  101. ?>

3. 接收消息示例程序(httpConsumer.php)

通過 HTTP 協議接收消息,請參考以下示例代碼。

  1. <?php
  2. include ("Util.php");
  3. /*
  4. * 消息訂閱者
  5. */
  6. class HttpConsumer
  7. {
  8. //簽名
  9. private static $signature = "Signature";
  10. //Consumer ID
  11. private static $consumerid = "ConsumerID";
  12. //訪問碼
  13. private static $ak = "AccessKey";
  14. //配置信息
  15. private static $config = null;
  16. //構造函數
  17. function __construct()
  18. {
  19. //讀取配置信息
  20. $this::$config = parse_ini_file("config.ini");
  21. }
  22. //訂閱流程
  23. public function process()
  24. {
  25. //打印配置信息
  26. var_dump($this::$config);
  27. //獲取Topic
  28. $topic = $this::$config["Topic"];
  29. //獲取Topic的URL路徑
  30. $url = $this::$config["URL"];
  31. //阿裏雲身份驗證碼
  32. $ak = $this::$config["Ak"];
  33. //阿裏雲身份驗證密鑰
  34. $sk = $this::$config["Sk"];
  35. //Consumer ID
  36. $cid = $this::$config["ConsumerID"];
  37. $newline = "n";
  38. //構造工具對象
  39. $util = new Util();
  40. while (true)
  41. {
  42. try
  43. {
  44. //構造時間戳
  45. $date = time()*1000;
  46. //簽名字符串
  47. $signString = $topic.$newline.$cid.$newline.$date;
  48. //計算簽名
  49. $sign = $util->calSignatue($signString,$sk);
  50. //構造簽名標記
  51. $signFlag = $this::$signature.":".$sign;
  52. //構造密鑰標記
  53. $akFlag = $this::$ak.":".$ak;
  54. //標記
  55. $consumerFlag = $this::$consumerid.":".$cid;
  56. //構造HTTP請求發送內容類型標記
  57. $contentFlag = "Content-Type:text/html;charset=UTF-8";
  58. //構造HTTP頭部信息
  59. $headers = array(
  60. $signFlag,
  61. $akFlag,
  62. $consumerFlag,
  63. $contentFlag,
  64. );
  65. //構造HTTP請求URL
  66. $getUrl = $url."/message/?topic=".$topic."&time=".$date."&num=32";
  67. //初始化網絡通信模塊
  68. $ch = curl_init();
  69. //填充HTTP頭部信息
  70. curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
  71. //設置HTTP請求類型,此處為GET
  72. curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"GET");
  73. //設置HTTP請求URL
  74. curl_setopt($ch,CURLOPT_URL,$getUrl);
  75. //構造執行環境
  76. ob_start();
  77. //開始發送HTTP請求
  78. curl_exec($ch);
  79. //獲取請求應答消息
  80. $result = ob_get_contents();
  81. //清理執行環境
  82. ob_end_clean();
  83. //打印請求應答信息
  84. var_dump($result);
  85. //關閉HTTP網絡連接
  86. curl_close($ch);
  87. //解析HTTP應答信息
  88. $messages = json_decode($result,true);
  89. //如果應答信息中的沒有包含任何的Topic信息,則直接跳過
  90. if (count($messages) ==0)
  91. {
  92. continue;
  93. }
  94. //依次遍曆每個Topic消息
  95. foreach ((array)$messages as $message)
  96. {
  97. var_dump($message);
  98. //獲取時間戳
  99. $date = (int)($util->microtime_float()*1000);
  100. //構造刪除Topic消息URL
  101. $delUrl = $url."/message/?msgHandle=".$message['msgHandle']."&topic=".$topic."&time=".$date;
  102. //簽名字符串
  103. $signString = $topic.$newline.$cid.$newline.$message['msgHandle'].$newline.$date;
  104. //計算簽名
  105. $sign = $util->calSignatue($signString,$sk);
  106. //構造簽名標記
  107. $signFlag = $this::$signature.":".$sign;
  108. //構造密鑰標記
  109. $akFlag = $this::$ak.":".$ak;
  110. //構造消費者組標記
  111. $consumerFlag = $this::$consumerid.":".$cid;
  112. //構造HTTP請求頭部信息
  113. $delheaders = array(
  114. $signFlag,
  115. $akFlag,
  116. $consumerFlag,
  117. $contentFlag,
  118. );
  119. //初始化網絡通信模塊
  120. $ch = curl_init();
  121. //填充HTTP請求頭部信息
  122. curl_setopt($ch,CURLOPT_HTTPHEADER,$delheaders);
  123. //設置HTTP請求URL信息
  124. curl_setopt($ch,CURLOPT_URL,$delUrl);
  125. //設置HTTP請求類型,此處為DELETE
  126. curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'DELETE');
  127. //構造執行環境
  128. ob_start();
  129. //開始發送HTTP請求
  130. curl_exec($ch);
  131. //獲取請求應答消息
  132. $result = ob_get_contents();
  133. //清理執行環境
  134. ob_end_clean();
  135. //打印應答消息
  136. var_dump($result);
  137. //關閉連接
  138. curl_close($ch);
  139. }
  140. }
  141. catch (Exception $e)
  142. {
  143. //打印異常信息
  144. echo $e->getMessage();
  145. }
  146. }
  147. }
  148. }
  149. //構造消息訂閱者
  150. $consumer = new HttpConsumer();
  151. //啟動消息訂閱者
  152. $consumer->process();
  153. ?>

4. 工具方法(Util.php)示例程序

示例中使用的工具方法如下。

  1. <?php
  2. /*
  3. * 工具類
  4. */
  5. class Util
  6. {
  7. //計算簽名
  8. public static function calSignatue($str,$key)
  9. {
  10. $sign = "";
  11. if(function_exists("hash_hmac"))
  12. {
  13. $sign = base64_encode(hash_hmac("sha1",$str,$key,true));
  14. }
  15. else
  16. {
  17. $blockSize = 64;
  18. $hashfunc = "sha1";
  19. if(strlen($key) > $blockSize)
  20. {
  21. $key = pack('H*',$hashfunc($key));
  22. }
  23. $key = str_pad($key,$blockSize,chr(0x00));
  24. $ipad = str_repeat(chr(0x36),$blockSize);
  25. $opad = str_repeat(chr(0x5c),$blockSize);
  26. $hmac = pack(
  27. 'H*',$hashfunc(
  28. ($key^$opad).pack(
  29. 'H*',$hashfunc($key^$ipad).$str
  30. )
  31. )
  32. );
  33. $sign = base64_encode($hmac);
  34. }
  35. return $sign;
  36. }
  37. //計算時間戳
  38. public static function microtime_float()
  39. {
  40. list($usec,$sec) = explode(" ",microtime());
  41. return ((float)$usec+(float)$sec);
  42. }
  43. }
  44. ?>

最後更新:2016-11-23 16:04:13

  上一篇:go Java 收發消息__HTTP 接入(簡單)_消息隊列 MQ-阿裏雲
  下一篇:go Python 收發消息__HTTP 接入(簡單)_消息隊列 MQ-阿裏雲