mingyoung / dingtalk

[已停止维护]
MIT License
561 stars 117 forks source link

找不到postJson()这个方法 #98

Open jetox opened 3 years ago

jetox commented 3 years ago

Call to undefined method EasyDingTalk\Kernel\Http\Client::postJson()

huchenghua commented 3 years ago

Call to undefined method EasyDingTalk\Kernel\Http\Client::postJson()

你是laravel8吗? 你是如何解决这个问题的?

jiangslee commented 3 years ago

image 不知道怎么修复比较优雅。。我只能 hard code 进去。。

huchenghua commented 3 years ago

好的, 谢啦

jiangslee commented 3 years ago

image

App\Common\Dingtalk\Http\Client.php


<?php
namespace App\Common\Dingtalk\Http;

use EasyDingTalk\Kernel\Http\Client as BaseClient;

class Client extends BaseClient {

    public function version()
    {
        return ['http_client_version' => 'hello world 1.0'];
    }

    /**
     * GET request.
     *
     * @param string $url
     * @param array  $query
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     *
     * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string
     */
    public function get(string $url, array $query = [], $async = false)
    {
        return $this->request($url, 'GET', ['query' => $query], $async);
    }

    /**
     * JSON request.
     *
     * @param string       $url
     * @param string|array $data
     * @param array        $query
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     *
     * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string
     */
    public function postJson(string $url, array $data = [], array $query = [])
    {
        return $this->request($url, 'POST', ['query' => $query, 'json' => $data]);
    }

}

App\Common\Dingtalk\Http\ServiceProvider.php

namespace App\Common\Dingtalk\Http;

use Pimple\Container;
use Pimple\ServiceProviderInterface;

class ServiceProvider implements ServiceProviderInterface
{
    /**
     * Registers services on the given container.
     * This method should only be used to configure services and parameters.
     * It should not get services.
     *
     * @param \Pimple\Container $pimple A container instance
     */
    public function register(Container $pimple)
    {
        // isset($pimple['client']) || 
        $pimple['client'] = function ($app) {
            return new Client($app);
        };
    }
}

找到了比较合适的解决方案了,这样就不用hard code进vendor了

huchenghua commented 3 years ago

image

App\Common\Dingtalk\Http\Client.php

<?php
namespace App\Common\Dingtalk\Http;

use EasyDingTalk\Kernel\Http\Client as BaseClient;

class Client extends BaseClient {

    public function version()
    {
        return ['http_client_version' => 'hello world 1.0'];
    }

    /**
     * GET request.
     *
     * @param string $url
     * @param array  $query
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     *
     * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string
     */
    public function get(string $url, array $query = [], $async = false)
    {
        return $this->request($url, 'GET', ['query' => $query], $async);
    }

    /**
     * JSON request.
     *
     * @param string       $url
     * @param string|array $data
     * @param array        $query
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     *
     * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string
     */
    public function postJson(string $url, array $data = [], array $query = [])
    {
        return $this->request($url, 'POST', ['query' => $query, 'json' => $data]);
    }

}

App\Common\Dingtalk\Http\ServiceProvider.php

namespace App\Common\Dingtalk\Http;

use Pimple\Container;
use Pimple\ServiceProviderInterface;

class ServiceProvider implements ServiceProviderInterface
{
    /**
     * Registers services on the given container.
     * This method should only be used to configure services and parameters.
     * It should not get services.
     *
     * @param \Pimple\Container $pimple A container instance
     */
    public function register(Container $pimple)
    {
        // isset($pimple['client']) || 
        $pimple['client'] = function ($app) {
            return new Client($app);
        };
    }
}

找到了比较合适的解决方案了,这样就不用hard code进vendor了

👍👍👍