Appsync 的 graphql 订阅不适用于 PHP5

Appsync's graphql subscription not working with PHP5

谁能帮我举一个使用 php5 和 AWS Appsync 或 php5 WebSocket 客户端的 graphql 订阅的工作示例,它与 Appsync 一起工作? 我正在尝试 textalk 但没有成功。

<?php
require(dirname(dirname(__FILE__)) . '/graphql/vendor/autoload.php');

use WebSocket\Client;

$query = <<<'GRAPHQL'
subscription onCreateProfile{
  onCreateBatch {
    hotelId
    batchId
    profiles {
      id
    }
  }
}
GRAPHQL;

$appSyncURL = 'wss://pbnblnr7xxxxxxxxxxxx.appsync-realtime-api.ap-south-1.amazonaws.com/graphql';
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'verify_peer', false);
stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
$client = new WebSocket\Client($appSyncURL, [
    'timeout' => 60, // 1 minute time out
    'context' => $context,
    'headers' => [
        'x-api-key' => 'APIKEY',
    ],
]);
$client->send($query);
echo $client->receive();
$client->close();

运行 上面的代码片段给出了以下错误:

$ php subscription.php
{"payload":{"errors":[{"message":"NoProtocolError","errorCode":400}]},"type":"connection_error"}PHP Fatal error:  Uncaught WebSocket\ConnectionException: Empty read; connection dead?  Stream state: {"crypto":{"protocol":"UNKNOWN","cipher_name":"TLS_AES_256_GCM_SHA384","cipher_bits":256,"cipher_version":"TLSv1.3"},"timed_out":false,"blocked":true,"eof":true,"stream_type":"tcp_socket\/ssl","mode":"r+","unread_bytes":0,"seekable":false} in /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php:316
Stack trace:
#0 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(299): WebSocket\Base->throwException('Empty read; con...')
#1 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(159): WebSocket\Base->read(2)
#2 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(149): WebSocket\Base->receiveFragment()
#3 /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php(272): WebSocket\Base->receive()
#4 /var/www/html/demo/graphql/subscription.php(31): WebSocket\Base->close()
#5 {main}
  thrown in /var/www/html/demo/graphql/vendor/textalk/websocket/lib/Base.php on line 316

那么有人尝试过 php5 的 graphQL 订阅吗?我的要求是在 PHP 应用程序(在后端)中使用 Graphql 订阅。我知道 graphQL 订阅可以通过 javascript WebSocket 客户端在前端正常工作。但是我必须在 PHP5 应用程序中使用 graphql 订阅。

任何帮助将不胜感激!

我已经弄明白了,现在它在 >= PHP 5.6.

上运行良好

这是相同的工作示例,以备不时之需。

https://github.com/roshanlabh/php-appsync-consumer-example/blob/master/appsync-subscription.php