Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
90.91% |
10 / 11 |
CRAP | |
89.66% |
78 / 87 |
| Meli | |
0.00% |
0 / 1 |
|
90.91% |
10 / 11 |
22.54 | |
89.66% |
78 / 87 |
| __construct($client_id, $client_secret, $access_token = null, $refresh_token = null) | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
| getAuthUrl($redirect_uri) | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| authorize($code, $redirect_uri) | |
100.00% |
1 / 1 |
4 | |
100.00% |
16 / 16 |
|||
| refreshAccessToken() | |
100.00% |
1 / 1 |
4 | |
100.00% |
18 / 18 |
|||
| get($path, $params = null) | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| post($path, $body = null, $params = array() | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
|||
| put($path, $body = null, $params = null) | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
|||
| delete($path, $params = null) | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| options($path, $params = null) | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
| execute($path, $opts = array() | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 9 |
|||
| make_path($path, $params = array() | |
100.00% |
1 / 1 |
5 | |
100.00% |
16 / 16 |
|||
| <?php | |
| class Meli { | |
| /** | |
| * @version 1.0.0 | |
| */ | |
| const VERSION = "1.0.0"; | |
| /** | |
| * @var $API_ROOT_URL is a main URL to access the Meli API's. | |
| * @var $AUTH_URL is a url to redirect the user for login. | |
| */ | |
| protected static $API_ROOT_URL = "https://api.mercadolibre.com"; | |
| protected static $AUTH_URL = "https://auth.mercadolivre.com.br/authorization"; | |
| protected static $OAUTH_URL = "/oauth/token"; | |
| /** | |
| * Configuration for CURL | |
| */ | |
| public static $CURL_OPTS = array( | |
| CURLOPT_USERAGENT => "MELI-PHP-SDK-1.0.0", | |
| CURLOPT_CONNECTTIMEOUT => 10, | |
| CURLOPT_RETURNTRANSFER => 1, | |
| CURLOPT_TIMEOUT => 60 | |
| ); | |
| protected $client_id; | |
| protected $client_secret; | |
| protected $redirect_uri; | |
| protected $access_token; | |
| protected $refresh_token; | |
| /** | |
| * Constructor method. Set all variables to connect in Meli | |
| * | |
| * @param string $client_id | |
| * @param string $client_secret | |
| * @param string $access_token | |
| * @param string $refresh_token | |
| */ | |
| public function __construct($client_id, $client_secret, $access_token = null, $refresh_token = null) { | |
| $this->client_id = $client_id; | |
| $this->client_secret = $client_secret; | |
| $this->access_token = $access_token; | |
| $this->refresh_token = $refresh_token; | |
| } | |
| /** | |
| * Return an string with a complete Meli login url. | |
| * NOTE: You can modify the $AUTH_URL to change the language of login | |
| * | |
| * @param string $redirect_uri | |
| * @return string | |
| */ | |
| public function getAuthUrl($redirect_uri) { | |
| $this->redirect_uri = $redirect_uri; | |
| $params = array("client_id" => $this->client_id, "response_type" => "code", "redirect_uri" => $redirect_uri); | |
| $auth_uri = self::$AUTH_URL."?".http_build_query($params); | |
| return $auth_uri; | |
| } | |
| /** | |
| * Executes a POST Request to authorize the application and take | |
| * an AccessToken. | |
| * | |
| * @param string $code | |
| * @param string $redirect_uri | |
| * | |
| */ | |
| public function authorize($code, $redirect_uri) { | |
| if($redirect_uri) | |
| $this->redirect_uri = $redirect_uri; | |
| $body = array( | |
| "grant_type" => "authorization_code", | |
| "client_id" => $this->client_id, | |
| "client_secret" => $this->client_secret, | |
| "code" => $code, | |
| "redirect_uri" => $this->redirect_uri | |
| ); | |
| $opts = array( | |
| CURLOPT_POST => true, | |
| CURLOPT_POSTFIELDS => $body | |
| ); | |
| $request = $this->execute(self::$OAUTH_URL, $opts); | |
| if($request["httpCode"] == 200) { | |
| $this->access_token = $request["body"]->access_token; | |
| if($request["body"]->refresh_token) | |
| $this->refresh_token = $request["body"]->refresh_token; | |
| return $request; | |
| } else { | |
| return $request; | |
| } | |
| } | |
| /** | |
| * Execute a POST Request to create a new AccessToken from a existent refresh_token | |
| * | |
| * @return string|mixed | |
| */ | |
| public function refreshAccessToken() { | |
| if($this->refresh_token) { | |
| $body = array( | |
| "grant_type" => "refresh_token", | |
| "client_id" => $this->client_id, | |
| "client_secret" => $this->client_secret, | |
| "refresh_token" => $this->refresh_token | |
| ); | |
| $opts = array( | |
| CURLOPT_POST => true, | |
| CURLOPT_POSTFIELDS => $body | |
| ); | |
| $request = $this->execute(self::$OAUTH_URL, $opts); | |
| if($request["httpCode"] == 200) { | |
| $this->access_token = $request["body"]->access_token; | |
| if($request["body"]->refresh_token) | |
| $this->refresh_token = $request["body"]->refresh_token; | |
| return $request; | |
| } else { | |
| return $request; | |
| } | |
| } else { | |
| $result = array( | |
| 'error' => 'Offline-Access is not allowed.', | |
| 'httpCode' => null | |
| ); | |
| return $result; | |
| } | |
| } | |
| /** | |
| * Execute a GET Request | |
| * | |
| * @param string $path | |
| * @param array $params | |
| * @return mixed | |
| */ | |
| public function get($path, $params = null) { | |
| $exec = $this->execute($path, null, $params); | |
| return $exec; | |
| } | |
| /** | |
| * Execute a POST Request | |
| * | |
| * @param unknown $path | |
| * @param string $body | |
| * @param array $params | |
| * @return mixed | |
| */ | |
| public function post($path, $body = null, $params = array()) { | |
| $body = json_encode($body); | |
| $opts = array( | |
| CURLOPT_HTTPHEADER => array('Content-Type: application/json'), | |
| CURLOPT_POST => true, | |
| CURLOPT_POSTFIELDS => $body | |
| ); | |
| $exec = $this->execute($path, $opts, $params); | |
| return $exec; | |
| } | |
| /** | |
| * Execute a PUT Request | |
| * | |
| * @param string $path | |
| * @param string $body | |
| * @param array $params | |
| * @return mixed | |
| */ | |
| public function put($path, $body = null, $params = null) { | |
| $body = json_encode($body); | |
| $opts = array( | |
| CURLOPT_HTTPHEADER => array('Content-Type: application/json'), | |
| CURLOPT_CUSTOMREQUEST => "PUT", | |
| CURLOPT_POSTFIELDS => $body | |
| ); | |
| $exec = $this->execute($path, $opts, $params); | |
| return $exec; | |
| } | |
| /** | |
| * Execute a DELETE Request | |
| * | |
| * @param string $path | |
| * @param array $params | |
| * @return mixed | |
| */ | |
| public function delete($path, $params = null) { | |
| $exec = $this->execute($path, null, $params); | |
| return $exec; | |
| } | |
| /** | |
| * Execute a OPTION Request | |
| * | |
| * @param string $path | |
| * @param array $params | |
| * @return mixed | |
| */ | |
| public function options($path, $params = null) { | |
| $opts = array( | |
| CURLOPT_CUSTOMREQUEST => "OPTIONS" | |
| ); | |
| $exec = $this->execute($path, $opts, $params); | |
| return $exec; | |
| } | |
| /** | |
| * Execute all requests and returns the json body and headers | |
| * | |
| * @param string $path | |
| * @param array $opts | |
| * @param array $params | |
| * @return mixed | |
| */ | |
| public function execute($path, $opts = array(), $params = array()) { | |
| $uri = $this->make_path($path, $params); | |
| $ch = curl_init($uri); | |
| curl_setopt_array($ch, self::$CURL_OPTS); | |
| if(!empty($opts)) | |
| curl_setopt_array($ch, $opts); | |
| $return["body"] = json_decode(curl_exec($ch)); | |
| $return["httpCode"] = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
| curl_close($ch); | |
| return $return; | |
| } | |
| /** | |
| * Check and construct an real URL to make request | |
| * | |
| * @param string $path | |
| * @param array $params | |
| * @return string | |
| */ | |
| public function make_path($path, $params = array()) { | |
| if (!preg_match("/^http/", $path)) { | |
| if (!preg_match("/^\//", $path)) { | |
| $path = '/'.$path; | |
| } | |
| $uri = self::$API_ROOT_URL.$path; | |
| } else { | |
| $uri = $path; | |
| } | |
| if(!empty($params)) { | |
| $paramsJoined = array(); | |
| foreach($params as $param => $value) { | |
| $paramsJoined[] = "$param=$value"; | |
| } | |
| $params = '?'.implode('&', $paramsJoined); | |
| $uri = $uri.$params; | |
| } | |
| return $uri; | |
| } |