public function setUri($uri)
{
if ($uri instanceof Zend_Uri_Http) {
// clone the URI in order to keep the passed parameter constant
$uri = clone $uri;
} elseif (is_string($uri)) {
$uri = Zend_Uri::factory($uri);
}
if (!$uri instanceof Zend_Uri_Http) {
/** @see Zend_Http_Client_Exception */
require_once 'Zend/Http/Client/Exception.php';
throw new Zend_Http_Client_Exception('Passed parameter is not a valid HTTP URI.');
}
// Set auth if username and password has been specified in the uri
if ($uri->getUsername() && $uri->getPassword()) {
$this->setAuth($uri->getUsername(), $uri->getPassword());
}
// We have no ports, set the defaults
if (!$uri->getPort()) {
$uri->setPort($uri->getScheme() == 'https' ? 443 : 80);
}
$this->uri = $uri;
return $this;
}
public function mailgunRequest($type, $domain, $apiKey, $data, $method = Zend_Http_Client::GET, $uriOveride = false) { $client = new Zend_Http_Client(); $client->setAuth("api", $apiKey); $client->setMethod($method); if ($uriOveride) { $client->setUri($uriOveride); } else { $client->setUri($this->apiUrl . $domain . "/" . $type); } if ($method == Zend_Http_Client::POST) { foreach ($data as $key => $value) { $client->setParameterPost($key, $value); } } else { foreach ($data as $key => $value) { $client->setParameterGet($key, $value); } } try { $response = $client->request(); if ($response->getStatus() == 200) { return json_decode($response->getBody()); } else { throw new Zend_Http_Exception("Error connecting to MailGun API. Returned error code: " . $response->getStatus() . " --- " . $response->getBody()); } } catch (Exception $e) { Mage::logException($e); return false; } }