尝试使用 PHP 集成 API 时收到异常

Receiving exception when attempting to integrate an API using PHP

目前,我正在努力将聊天 API(称为 pusher Chatkit)集成到我正在开发的应用程序中。对于后端,正在使用 PHP 和 laravel,并且正在使用 postman 来测试服务器。在 Postman 上测试时,我收到 500 内部服务器错误,但出现以下异常:

{
    "message": "You must provide an instance_locator",
    "exception": "Chatkit\Exceptions\MissingArgumentException",
    "file": "/Users/shaquilenoor/Desktop/chatapi/vendor/pusher/pusher-chatkit-server/src/Chatkit.php",
    "line": 49,
    "trace": [

在跟踪中,提到了很多 files/lines,所以我省略了,因为它太多了,不适合这个 post(不过你可以问是否需要,我可以制作一个 Gdrive link 中)

导致所引用的行 + 函数的代码是这样的:

<?php

namespace Chatkit;

use Chatkit\Exceptions\ChatkitException;
use Chatkit\Exceptions\ConfigurationException;
use Chatkit\Exceptions\ConnectionException;
use Chatkit\Exceptions\MissingArgumentException;
use Chatkit\Exceptions\TypeMismatchException;
use Firebase\JWT\JWT;

class Chatkit
{
    protected $settings = array(
        'scheme'       => 'https',
        'port'         => 80,
        'timeout'      => 30,
        'debug'        => false,
        'curl_options' => array(),
    );
    protected $logger = null;
    protected $ch = null; // Curl handler

    protected $api_settings = array();
    protected $authorizer_settings = array();
    protected $cursor_settings = array();

    const GLOBAL_SCOPE = 'global';
    const ROOM_SCOPE = 'room';

    /**
     *
     * Initializes a new Chatkit instance.
     *
     *
     * @param array $options   Options to configure the Chatkit instance.
     *                         instance_locator - your Chatkit instance locator
     *                         key - your Chatkit instance's key
     *                         scheme - e.g. http or https
     *                         host - the host; no trailing forward slash.
     *                         port - the http port
     *                         timeout - the http timeout
     */
    public function __construct($options)
    {
        $this->checkCompatibility();

        if (!isset($options['instance_locator'])) {
            throw new MissingArgumentException('You must provide an instance_locator');
        }
        if (!isset($options['key'])) {
            throw new MissingArgumentException('You must provide a key');
        }

        $this->settings['instance_locator'] = $options['instance_locator'];
        $this->settings['key'] = $options['key'];
        $this->api_settings['service_name'] = 'chatkit';
        $this->api_settings['service_version'] = 'v2';
        $this->authorizer_settings['service_name'] = 'chatkit_authorizer';
        $this->authorizer_settings['service_version'] = 'v2';
        $this->cursor_settings['service_name'] = 'chatkit_cursors';
        $this->cursor_settings['service_version'] = 'v2';

        foreach ($options as $key => $value) {
            // only set if valid setting/option
            if (isset($this->settings[$key])) {
                $this->settings[$key] = $value;
            }
        }
    }

instance_locator 指的是在 Pusher Chatkit 上生成的代码,我 link 在我的文件中集成了那个 API。这是我第一次使用 PHP 集成后端服务器,所以有点迷茫!将不胜感激一些关于我应该在哪里寻找解决问题的建议,并且非常乐意提供更多信息。干杯

根据您创建 API 请求的方式,您应该这样做:

$chatkit = new Chatkit(['instance_locator' => *locator*, 'key' => *actualKey*]);

您收到的错误意味着您还没有传递数组中的变量。