Skip to content
This repository was archived by the owner on Feb 10, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 77 additions & 9 deletions Meli/meli.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Meli {
protected $redirect_uri;
protected $access_token;
protected $refresh_token;
protected $queue = array();

/**
* Constructor method. Set all variables to connect in Meli
Expand Down Expand Up @@ -179,17 +180,21 @@ public function get($path, $params = null, $assoc = false) {
* @param array $params
* @return mixed
*/
public function post($path, $body = null, $params = array()) {
public function post($path, $body = null, $params = array(), $queue = false) {
$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);
if ($queue) {
$this->queue($path, $opts, $params);
} else {
$exec = $this->execute($path, $opts, $params);
return $exec;
}

return $exec;
}

/**
Expand All @@ -200,17 +205,21 @@ public function post($path, $body = null, $params = array()) {
* @param array $params
* @return mixed
*/
public function put($path, $body = null, $params = array()) {
public function put($path, $body = null, $params = array(), $queue = false) {
$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);
if ($queue) {
$this->queue($path, $opts, $params);
} else {
$exec = $this->execute($path, $opts, $params);
return $exec;
}

return $exec;
}

/**
Expand All @@ -220,14 +229,18 @@ public function put($path, $body = null, $params = array()) {
* @param array $params
* @return mixed
*/
public function delete($path, $params) {
public function delete($path, $params, $queue = false) {
$opts = array(
CURLOPT_CUSTOMREQUEST => "DELETE"
);

$exec = $this->execute($path, $opts, $params);
if ($queue) {
$this->queue($path, $opts, $params);
} else {
$exec = $this->execute($path, $opts, $params);
return $exec;
}

return $exec;
}

/**
Expand Down Expand Up @@ -273,6 +286,61 @@ public function execute($path, $opts = array(), $params = array(), $assoc = fals
return $return;
}

/**
* Queue a request to be executed later
*
* @param string $path
* @param array $opts
* @param array $params
* @param boolean $assoc
*/
public function queue($path, $opts = array(), $params = array(), $assoc = false) {
$this->queue[] = compact('path', 'opts', 'params', 'assoc');
}

/**
* Executes all queued requests and returns the json body and headers
*
* @return mixed
*/
public function executeQueued() {
$mh = curl_multi_init();
$handles = array();

if (empty($this->queue)) {
return false;
}

foreach ($this->queue as $request) {
$uri = $this->make_path($request['path'], $request['params']);
$ch = curl_init($uri);
$handles[] = $ch;
curl_setopt_array($ch, self::$CURL_OPTS);
if(!empty($request['opts'])) {
curl_setopt_array($ch, $request['opts']);
}
curl_multi_add_handle($mh, $ch);
}

$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);

$responses = array();
foreach ($handles as $ch) {
curl_multi_remove_handle($mh, $ch);
$responses[] = array(
'body' => curl_multi_getcontent($ch),
'httpCode' => curl_getinfo($ch, CURLINFO_HTTP_CODE)
);
}
curl_multi_close($mh);
$this->queue = array();

return $responses;
}

/**
* Check and construct an real URL to make request
*
Expand Down
Loading