【问题标题】:Appsync's graphql subscription not working with PHP5Appsync 的 graphql 订阅不适用于 PHP5
【发布时间】:2020-10-27 11:44:23
【问题描述】:

谁能帮我提供一个使用 php5 和 AWS Appsync 或 php5 WebSocket 客户端(与 Appsync 一起使用)订阅 graphql 的工作示例? 我正在尝试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();

在 sn-p 上运行会出现以下错误:

$ 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 websocket graphql aws-appsync graphql-subscriptions


    【解决方案1】:

    我已经弄清楚了,现在它在 >= PHP 5.6 上运行良好。

    这是一个相同的工作示例,以防万一有人需要。

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

    猜你喜欢
    • 2021-11-09
    • 1970-01-01
    • 2020-01-18
    • 2018-10-20
    • 2019-04-14
    • 2022-07-04
    • 2019-08-09
    • 2021-07-14
    • 2020-03-21
    相关资源
    最近更新 更多