pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <i id='sVey5'><tr id='sVey5'><dt id='sVey5'><q id='sVey5'><span id='sVey5'><b id='sVey5'><form id='sVey5'><ins id='sVey5'></ins><ul id='sVey5'></ul><sub id='sVey5'></sub></form><legend id='sVey5'></legend><bdo id='sVey5'><pre id='sVey5'><center id='sVey5'></center></pre></bdo></b><th id='sVey5'></th></span></q></dt></tr></i><div class="7fnfdp7" id='sVey5'><tfoot id='sVey5'></tfoot><dl id='sVey5'><fieldset id='sVey5'></fieldset></dl></div>
      <legend id='sVey5'><style id='sVey5'><dir id='sVey5'><q id='sVey5'></q></dir></style></legend>
        <bdo id='sVey5'></bdo><ul id='sVey5'></ul>
    1. <tfoot id='sVey5'></tfoot>

    2. <small id='sVey5'></small><noframes id='sVey5'>

        WebSockets - 通過 php 發送 json 數據

        WebSockets - send json data via php(WebSockets - 通過 php 發送 json 數據)

          <tbody id='Hq5pi'></tbody>
      1. <small id='Hq5pi'></small><noframes id='Hq5pi'>

        <tfoot id='Hq5pi'></tfoot>
          <bdo id='Hq5pi'></bdo><ul id='Hq5pi'></ul>

          • <i id='Hq5pi'><tr id='Hq5pi'><dt id='Hq5pi'><q id='Hq5pi'><span id='Hq5pi'><b id='Hq5pi'><form id='Hq5pi'><ins id='Hq5pi'></ins><ul id='Hq5pi'></ul><sub id='Hq5pi'></sub></form><legend id='Hq5pi'></legend><bdo id='Hq5pi'><pre id='Hq5pi'><center id='Hq5pi'></center></pre></bdo></b><th id='Hq5pi'></th></span></q></dt></tr></i><div class="lhnxtb5" id='Hq5pi'><tfoot id='Hq5pi'></tfoot><dl id='Hq5pi'><fieldset id='Hq5pi'></fieldset></dl></div>

              1. <legend id='Hq5pi'><style id='Hq5pi'><dir id='Hq5pi'><q id='Hq5pi'></q></dir></style></legend>
                  本文介紹了WebSockets - 通過 php 發送 json 數據的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試從 PHP 向我的 websocket aws api 網關發送一個即發即棄的請求.

                  我設置了一個名為sendmessage"的操作.

                  這是我正在使用的代碼:

                  $protocol = "ssl";$host = ".amazonaws.com";$端口 = 443;$path = "//";$超時= 2000;$socket = pfsockopen($protocol . "://" . $host, $port,$errno, $errstr, $timeout);$content = "{'action': 'sendmessage', 'data': 'test'}";$body = "POST $path HTTP/1.1
                  ";$body .= "主機:$host
                  ";$body .= "內容類型:應用程序/json
                  ";$body .= "內容長度:" .strlen($content) ."
                  ";$body .= "連接:關閉
                  
                  ";$body .= $content;$body .= "
                  ";fwrite($socket, $body);

                  然而,什么也沒發生.

                  如果我使用 wscat,例如:

                  wscat -c wss://.amazonaws.com/>{'動作':'發送消息','數據':'測試'}>

                  效果很好.

                  我的 php 代碼做錯了什么?

                  注意:我需要保持套接字連接(使用 pfsockopen 函數時的方式).

                  解決方案

                  由于你沒有提供handpoint鏈接,這里有一些注意事項,請自行測試!

                  我猜問題出在wss部分,php需要先檢索證書,這樣才能加密數據.

                  您的代碼應該可以在 ws:// 流上正常工作.

                  要連接到常規 ws:// 流,只需使用 fsockopen().


                  但是要連接到 wss:// secure websocket 流,使用 php,沒有庫,我們需要先創建一個隧道,通過查詢 公鑰與stream_socket_client.

                  這是一種握手機制.這可以按如下方式完成.

                  注意第一個 ssl:// 調用.這是TLS 1.0 協議.

                  輸出應如下所示:

                  string(44) HTTP/1.1 101 Web Socket 協議握手"string(21) 連接:升級"string(37) 日期:2019 年 12 月 12 日星期四 04:06:27 GMT"string(52) "Sec-WebSocket-Accept: fTYwcEa6D9kJBtghptkz1e9CtBI="string(25) 服務器:Kaazing 網關";string(20) 升級:websocket";


                  相同的基本代碼,另一個示例,從 Binance wss:// 流中提取數據.

                  我們還可以使用帶有 tls 的 TLS 1.2:// 握手代替.適用于大多數服務器.


                  這是一種從 php 檢索僅遠程手持設備的 ssl RSA 公鑰的方法.可用于加速以后的連接.

                  真的,capture_peer_cert_chain"=>真的];$a = stream_context_create([ssl"=>$opt]);$b = stream_socket_client("ssl://stream.binance.com:9443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $a);$cont = stream_context_get_params($b);$key = openssl_pkey_get_public($cont[options"][ssl"][peer_certificate"]);$c = openssl_pkey_get_details($key);var_dump($c["key"]);

                  輸出類似:

                  -----開始公鑰--MIIBIjANBgkqhki(...)7aEsFtUNkwM5R5b1mpqzAwqHyvdamJx20bT6SS6PYXSr/dv8ak1d4e2Q0nIa1O7l3w0bZZ4wnp5B8Z+tjPd1W8uaZoRO2iVkPMh2yPlj0mmtUw1YlfDyutH/t4FlRCDiD4JjdREQGs381/+jbkdjl2SIb1IyNiCdAXA6zsqxwIDAQAB-----結束公鑰-----

                  可能還有其他怪癖,當然,我們需要主要的handpoint^.很高興測試一下.否則祝你好運,關于這個主題的文檔非常缺乏.

                  這是仍然是一個新生的協議(2011 年!).最好的細節在 RFC 規范中:

                  <塊引用>

                  WebSocket 協議由 IETF 標準化為 RFC 64552011

                  關于握手,必須由GET請求發起.

                  <塊引用>

                  客戶端將發送一個非常標準的 HTTP 請求,其標頭為看起來像這樣(HTTP 版本必須是 1.1 或更高版本,并且方法必須是GET)

                  Writing_WebSocket_servers#Client_handshake_request


                  簡而言之:

                  <塊引用>

                  如果未加密的 WebSocket 流量流經顯式或不支持 WebSockets 的透明代理服務器,連接可能會失敗.

                  WebSocket#Proxy_traversal

                  Transport_Layer_Security#Digital_certificates

                  I'm trying to send a fire-and-forget request from PHP to my websocket aws api gateway.

                  I've set up an action called "sendmessage".

                  This is the code I'm using:

                  $protocol = "ssl";
                  $host = "<myendpoint>.amazonaws.com";
                  $port = 443;
                  $path = "/<mystage>/";
                  $timeout = 2000;
                  
                  $socket = pfsockopen($protocol . "://" . $host, $port,
                                      $errno, $errstr, $timeout);
                  
                  $content = "{'action': 'sendmessage', 'data': 'test'}";
                  $body = "POST $path HTTP/1.1
                  ";
                  $body .= "Host: $host
                  ";
                  $body .= "Content-Type: application/json
                  ";
                  $body .= "Content-Length: " . strlen($content) . "
                  ";
                  $body .= "Connection: Close
                  
                  ";
                  $body .= $content;
                  $body .= "
                  ";
                  
                  fwrite($socket, $body);
                  

                  However, nothing happens.

                  If I use wscat, like:

                  wscat -c wss://<my-endpoint>.amazonaws.com/<my-stage>
                  
                  > {'action': 'sendmessage', 'data': 'test'}
                  >
                  

                  it works just fine.

                  What am I doing wrong in my php code?

                  Note: I need the socket connection to be persistent (the way it is when using the pfsockopen function).

                  解決方案

                  Since you didn't provide a handpoint link, here is some notes, following own tests!

                  I guess the issue comes from the wss part, php needs to retrieve the certificate first, so it can encrypt the data.

                  Your code should work just fine on a ws:// stream.

                  To connect to a regular ws:// stream, one can simply use fsockopen().

                  <?php
                  $fp = fsockopen("udp://echo.websocket.org", 13, $errno, $errstr);
                  if (!$fp) {
                      echo "ERROR: $errno - $errstr<br />
                  ";
                  } else {
                      fwrite($fp, "
                  ");
                      echo "Connected!";
                      echo fread($fp, 26);
                      fclose($fp);
                  }
                  


                  But to connect to a wss:// secure websocket stream, using php, without libraries, we need to create a tunnel first, by querying the public key with stream_socket_client.

                  This is a handshake mechanism. This can be done as follow.

                  Notice the first ssl:// call. This is the TLS 1.0 protocol.

                  <?php  
                  $sock = stream_socket_client("ssl://echo.websocket.org:443",$e,$n,30,STREAM_CLIENT_CONNECT,stream_context_create(null));
                  if(!$sock){
                   echo"[$n]$e".PHP_EOL;
                  } else {
                    fwrite($sock,"GET / HTTP/1.1
                  Host: echo.websocket.org
                  Accept: */*
                  Connection: Upgrade
                  Upgrade: websocket
                  Sec-WebSocket-Version: 13
                  Sec-WebSocket-Key: ".rand(0,999)."
                  
                  ");
                    while(!feof($sock)){
                      var_dump(fgets($sock,2048));
                    }
                  }
                  

                  The output should looks like:

                  string(44) "HTTP/1.1 101 Web Socket Protocol Handshake"
                  string(21) "Connection: Upgrade"
                  string(37) "Date: Thu, 12 Dec 2019 04:06:27 GMT"
                  string(52) "Sec-WebSocket-Accept: fTYwcEa6D9kJBtghptkz1e9CtBI="
                  string(25) "Server: Kaazing Gateway"
                  string(20) "Upgrade: websocket"
                  


                  Same base code, another example, pulling data from Binance wss:// stream.

                  We can also use TLS 1.2, with a tls:// handshake instead. Works on most servers.

                  <?php
                  $sock = stream_socket_client("tls://stream.binance.com:9443",$error,$errnum,30,STREAM_CLIENT_CONNECT,stream_context_create(null));
                  if (!$sock) {
                      echo "[$errnum] $error" . PHP_EOL;
                  } else {
                    echo "Connected - Do NOT get rekt!" . PHP_EOL;
                    fwrite($sock, "GET /stream?streams=btcusdt@kline_1m HTTP/1.1
                  Host: stream.binance.com:9443
                  Accept: */*
                  Connection: Upgrade
                  Upgrade: websocket
                  Sec-WebSocket-Version: 13
                  Sec-WebSocket-Key: ".rand(0,999)."
                  
                  ");
                    while (!feof($sock)) {
                      var_dump(explode(",",fgets($sock, 512)));
                    }
                  } 
                  


                  Here is a way to retrieve only the ssl RSA public key of a remote handpoint, from php. Can be used to speed up later connections.

                  <?php
                  $opt = [
                    "capture_peer_cert" => true,
                    "capture_peer_cert_chain" => true
                  ];
                  $a = stream_context_create(["ssl"=>$opt]);
                  $b = stream_socket_client("ssl://stream.binance.com:9443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $a);
                  $cont = stream_context_get_params($b);
                  $key = openssl_pkey_get_public($cont["options"]["ssl"]["peer_certificate"]);
                  $c = openssl_pkey_get_details($key);
                  var_dump($c["key"]);
                  

                  Output something like:

                  -----BEGIN PUBLIC KEY-----
                  MIIBIjANBgkqhki(...)7aEsFtUNkwM5R5b1mpqzAwqHyvdamJx20bT6SS6
                  PYXSr/dv8ak1d4e2Q0nIa1O7l3w0bZZ4wnp5B8Z+tjPd1W8uaZoRO2iVkPMh2yPl
                  j0mmtUw1YlfDyutH/t4FlRCDiD4JjdREQGs381/+jbkdjl2SIb1IyNiCdAXA6zsq
                  xwIDAQAB
                  -----END PUBLIC KEY-----
                  

                  There is possibly other quircks, to be sure, we need the main handpoint^. Would be glad to test that. Otherwise good luck, there is a big lack of documentation on the subject.

                  This is still a new born protocol (2011!). Best details are in the RFC specification:

                  The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011

                  About the handshake, it must be initiated by a GET request.

                  The client will send a pretty standard HTTP request with headers that looks like this (the HTTP version must be 1.1 or greater, and the method must be GET)

                  Writing_WebSocket_servers#Client_handshake_request


                  In short:

                  If unencrypted WebSocket traffic flows through an explicit or a transparent proxy server without WebSockets support, the connection will likely fail.

                  WebSocket#Proxy_traversal

                  Transport_Layer_Security#Digital_certificates

                  這篇關于WebSockets - 通過 php 發送 json 數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)
                • <i id='XNAP1'><tr id='XNAP1'><dt id='XNAP1'><q id='XNAP1'><span id='XNAP1'><b id='XNAP1'><form id='XNAP1'><ins id='XNAP1'></ins><ul id='XNAP1'></ul><sub id='XNAP1'></sub></form><legend id='XNAP1'></legend><bdo id='XNAP1'><pre id='XNAP1'><center id='XNAP1'></center></pre></bdo></b><th id='XNAP1'></th></span></q></dt></tr></i><div class="zdrhfl7" id='XNAP1'><tfoot id='XNAP1'></tfoot><dl id='XNAP1'><fieldset id='XNAP1'></fieldset></dl></div>

                        <tbody id='XNAP1'></tbody>

                        <small id='XNAP1'></small><noframes id='XNAP1'>

                        <legend id='XNAP1'><style id='XNAP1'><dir id='XNAP1'><q id='XNAP1'></q></dir></style></legend>

                          <bdo id='XNAP1'></bdo><ul id='XNAP1'></ul>
                          1. <tfoot id='XNAP1'></tfoot>
                          2. 主站蜘蛛池模板: 对辊式破碎机-对辊制砂机-双辊-双齿辊破碎机-巩义市裕顺机械制造有限公司 | DWS物流设备_扫码称重量方一体机_快递包裹分拣机_广东高臻智能装备有限公司 | 丹佛斯变频器-Danfoss战略代理经销商-上海津信变频器有限公司 | 卫生型双针压力表-高温防腐差压表-安徽康泰电气有限公司 | 家德利门业,家居安全门,别墅大门 - 安徽家德利门业有限公司 | 工程管道/塑料管材/pvc排水管/ppr给水管/pe双壁波纹管等品牌管材批发厂家-河南洁尔康建材 | MES系统工业智能终端_生产管理看板/安灯/ESOP/静电监控_讯鹏科技 | 水冷散热器_水冷电子散热器_大功率散热器_水冷板散热器厂家-河源市恒光辉散热器有限公司 | 金刚网,金刚网窗纱,不锈钢网,金刚网厂家- 河北萨邦丝网制品有限公司 | 北京发电机出租_发电机租赁_北京发电机维修 - 河北腾伦发电机出租 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 九爱图纸|机械CAD图纸下载交流中心 | 石英砂矿石色选机_履带辣椒色选机_X光异物检测机-合肥幼狮光电科技 | 山东艾德实业有限公司| 深圳工程师职称评定条件及流程_深圳职称评审_职称评审-职称网 | 利浦顿蒸汽发生器厂家-电蒸汽发生器/燃气蒸汽发生器_湖北利浦顿热能科技有限公司官网 | 飞飞影视_热门电影在线观看_影视大全 | arch电源_SINPRO_开关电源_模块电源_医疗电源-东佑源 | EPK超声波测厚仪,德国EPK测厚仪维修-上海树信仪器仪表有限公司 | 自动配料系统_称重配料控制系统厂家 | 流水线电子称-钰恒-上下限报警电子秤-上海宿衡实业有限公司 | 金库门,金库房,金库门厂家,金库门价格-河北特旺柜业有限公司 | 日本东丽膜_反渗透膜_RO膜价格_超滤膜_纳滤膜-北京东丽阳光官网 日本细胞免疫疗法_肿瘤免疫治疗_NK细胞疗法 - 免疫密码 | 拉力测试机|材料拉伸试验机|电子拉力机价格|万能试验机厂家|苏州皖仪实验仪器有限公司 | 天津蒸汽/热水锅炉-电锅炉安装维修直销厂家-天津鑫淼暖通设备有限公司 | 余姚生活网_余姚论坛_余姚市综合门户网站 | 淄博不锈钢无缝管,淄博不锈钢管-鑫门物资有限公司 | 上海盐水喷雾试验机_两厢式冷热冲击试验箱-巨怡环试 | 北京律师事务所_房屋拆迁律师_24小时免费法律咨询_云合专业律师网 | 钢化玻璃膜|手机钢化膜|钢化膜厂家|手机保护膜-【东莞市大象电子科技有限公司】 | 北京工业设计公司-产品外观设计-产品设计公司-千策良品工业设计 北京翻译公司-专业合同翻译-医学标书翻译收费标准-慕迪灵 | 智能楼宇-楼宇自控系统-楼宇智能化-楼宇自动化-三水智能化 | 首页|成都尚玖保洁_家政保洁_开荒保洁_成都保洁 | 专业深孔加工_东莞深孔钻加工_东莞深孔钻_东莞深孔加工_模具深孔钻加工厂-东莞市超耀实业有限公司 | MOOG伺服阀维修,ATOS比例流量阀维修,伺服阀维修-上海纽顿液压设备有限公司 | 炒货机-炒菜机-炒酱机-炒米机@霍氏机械 | 深圳宣传片制作-企业宣传视频制作-产品视频拍摄-产品动画制作-短视频拍摄制作公司 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 橡胶电子拉力机-塑料-微电脑电子拉力试验机厂家-江苏天源 | 暖气片十大品牌厂家_铜铝复合暖气片厂家_暖气片什么牌子好_欣鑫达散热器 | 英国雷迪地下管线探测仪-雷迪RD8100管线仪-多功能数字听漏仪-北京迪瑞进创科技有限公司 |