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

时间:2019-02-05 20:57:37

标签: php laravel debugging server pusher

当前,我正在将聊天API(称为推手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": [

在跟踪中,提到了许多文件/行,因此我忽略了这一点,因为它太不适合本文了(您可以询问是否需要,我可以在其中建立一个Gdrive链接)

导致被引用的line +函数的代码是这样的:

<?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上生成的代码,我将其链接到文件中以集成该API。这是我第一次使用PHP集成后端服务器,所以有点迷失了!希望能就我应该在哪里解决该问题提供一些建议,也非常乐意提供进一步的信息。干杯

1 个答案:

答案 0 :(得分:1)

根据创建API请求的方式,您应该执行以下操作:

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

您收到的错误表示您尚未在数组中传递变量。