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

时间:2019-05-13 04:27:14

标签: php laravel-5

我有来自库https://github.com/javiertelioz/mercadolibre的以下2个代码可与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;
    }
}

和带有以下代码的Controller 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-如第一个示例一样使用门面(meli)

  

meli :: getAuthUrl

2-替换代码:

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', '')),

但是收到错误

  

函数App \ Sources \ Meli :: __ construct()的参数太少,0   通过   /Applications/MAMP/htdocs/price2b/app/Http/Controllers/MeliController.php

任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

该错误告诉您问题所在:您正在静态地调用方法(stack runghc -- -isrc app/Main.hs ),但这不是静态方法。您必须在类的实例上调用它。这意味着您的第三种方法:

meli::getAuthUrl(...)

是正确的。

但是,正如您指出的那样,您会收到“参数过多”错误。这是因为实例化'url' => (new \App\Sources\Meli)->getAuthUrl(env('ML_AUTHENTICATION_URL', '')), 类时没有传递任何参数。也就是说,Meli等效于new \App\Sources\Meli,将零个参数传递给构造函数。

但是上面发布的new \App\Sources\Meli()类的构造函数看起来像这样:

Meli

因此,您需要传递至少2个参数,而不是零。换句话说,至少是这样的:

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