代码之家  ›  专栏  ›  技术社区  ›  s_h

MercadoLibre API给出错误“不应静态调用getAuthUrl”

  •  -1
  • s_h  · 技术社区  · 6 年前

    https://github.com/javiertelioz/mercadolibre 要连接MercadoLibre的API:

    Meli.php类:

    <?php
    
    namespace App\Sources;
    
    use App\Sources\MercadoLibre\Utils;
    
    class Meli extends Utils {
    
        /**
         * @version 1.0.0
         */
        const VERSION  = "1.0.0";
    
        /**
         * Configuration for urls
         */
        protected $urls = array(
            'API_ROOT_URL' => 'https://api.mercadolibre.com',
            'AUTH_URL'     => 'http://auth.mercadolibre.com.ar/authorization',
            'OAUTH_URL'    => '/oauth/token'
        );
    
        /**
         * Configuration for CURL
         */
        protected $curl_opts = array(
            CURLOPT_USERAGENT => "MELI-PHP-SDK-1.0.0",
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_CONNECTTIMEOUT => 10,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_TIMEOUT => 60
        );
    
        protected $client_id;
        protected $client_secret;
    
        /**
         * Constructor method. Set all variables to connect in Meli
         *
         * @param string $client_id
         * @param string $client_secret
         * @param string $access_token
         */
        public function __construct($client_id, $client_secret, $urls = null, $curl_opts = null) {
            $this->client_id     = $client_id;
            $this->client_secret = $client_secret;
            $this->urls          = $urls ? $urls : $this->urls;
            $this->curl_opts     = $curl_opts ? $curl_opts : $this->curl_opts;
        }
    
        /**
         * Return an string with a complete Meli login url.
         *
         * @param string $redirect_uri
         * @return string
         */
        public function getAuthUrl($redirect_uri) {
            $params = array("client_id" => $this->client_id, "response_type" => "code", "redirect_uri" => $redirect_uri);
            $auth_uri = $this->urls['AUTH_URL'] . "?" . http_build_query($params);
            return $auth_uri;
        }
    }
    

    以及控制器MeliController.php,代码如下:

    class MeliController extends Controller
    {
        /**
         * Login Page (Mercado Libre)
         */
        public function login() {
            session()->regenerate();
            return view('auth/melilogin')->with('auth', [
                'url' => meli::getAuthUrl(env('ML_AUTHENTICATION_URL', '')),
            ]);
        }
    
        public function logout() {
            if(session('profile')) {
                session()->forget('profile');
                session()->flush();
            }
            return \Redirect::to('/auth/melilogin');
        }
    }
    

    不应调用非静态方法App\Sources\Meli::getAuthUrl() 静态的

    1-在第一个例子中使用facade(meli)

    meli::getAuthUrl

    public function getAuthUrl($redirect_uri) {
                $params = array("client_id" => $this->client_id, "response_type" => "code", "redirect_uri" => $redirect_uri);
                $auth_uri = $this->urls['AUTH_URL'] . "?" . http_build_query($params);
                return $auth_uri;
            }
        }
    

    使用公共静态函数和$self而不是$this,但没有成功。

    3-使用以下方法动态调用:

        'url' => (new \App\Sources\Meli)->getAuthUrl(env('ML_AUTHENTICATION_URL', '')),
    

    但接收错误

    传入

    感谢任何帮助。

    1 回复  |  直到 6 年前
        1
  •  1
  •   elixenide Ren    6 年前

    错误告诉您问题:您正在静态调用该方法( meli::getAuthUrl(...) ),但它不是静态方法。你必须在类的实例上调用它。这意味着你的第三种方法:

    'url' => (new \App\Sources\Meli)->getAuthUrl(env('ML_AUTHENTICATION_URL', '')),
    

    是正确的。

    Meli 上课。也就是说, new \App\Sources\Meli 相当于 new \App\Sources\Meli() ,向构造函数传递零参数。

    但是 梅利

    public function __construct($client_id, $client_secret, $urls = null, $curl_opts = null)
    

    所以,至少需要传递两个参数,而不是零。换句话说,至少,像这样:

    'url' => (new \App\Sources\Meli($someClientId, $someClientSecret))->getAuthUrl(env('ML_AUTHENTICATION_URL', '')),
    
    推荐文章