Created
April 11, 2012 18:16
-
-
Save Opus1no2/2361090 to your computer and use it in GitHub Desktop.
Small Mashup of Ifbyphone and api.wine.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* A small mash-up of Ifbyphone and api.wine.com | |
* | |
* Returns a brief description of a wine label that is | |
* sent via SMS to an Ifbyphone number. | |
* | |
* Usage: | |
* | |
* $test = new Wine(9524577652, 2122569386, 'mondavi'); | |
* $test->execMessage(); | |
* | |
*/ | |
class Wine | |
{ | |
const WINE_KEY = ''; | |
const IBP_KEY = ''; | |
const IBP_BASE = 'https://secure.ifbyphone.com/ibp_api.php?'; | |
const WINE_BASE = 'http://services.wine.com/api/beta/service.svc/xml/catalog?'; | |
const GOOGLE_BASE = 'https://www.googleapis.com/urlshortener/v1/url'; | |
protected $_to; | |
protected $_from; | |
protected $_message; | |
public function __construct($to, $from, $message) | |
{ | |
$this->_to = $to; | |
$this->_from = $from; | |
$this->_message = urlencode($message); | |
} | |
/** | |
* | |
* Retrieve data about the wine label provided in the SMS message | |
*/ | |
public function getData() | |
{ | |
$options = array( | |
'search' => $this->_message, | |
'size' => 1, | |
'apikey' => self::WINE_KEY | |
); | |
$query = $this->setQuery($options); | |
$url = self::WINE_BASE . $query; | |
$contents = file_get_contents($url); | |
$content_obj = simplexml_load_string($contents); | |
foreach($content_obj->Products->List->Product as $product) { | |
$data .= $product->Name . PHP_EOL; | |
$data .= $this->shortenUrl($product->Url) . PHP_EOL; | |
$data .= "Rating: " .$product->Ratings->HighestScore ." out of 100" . PHP_EOL; | |
$data .= "Retail price: $".$product->PriceRetail; | |
$return_data = urldecode($data); | |
} | |
return $return_data; | |
} | |
/** | |
* | |
* Shorten the URL to return in the text message | |
* | |
* @param string $data | |
*/ | |
public function shortenUrl($data) | |
{ | |
$datastring = (string)$data; | |
$url = self::GOOGLE_BASE; | |
$header = 'Content-Type: application/json'; | |
$params = "{'longUrl': '$datastring'}"; | |
$result = json_decode($this->_sendResponse($url, $header, $params)); | |
return ($result->{'id'}); | |
} | |
/** | |
* | |
* Create a query for an HTTP request to either Ifbyphone or api.wine.com | |
* given an array of key value pairs | |
* | |
* @param array $options | |
*/ | |
public function setQuery(array $options) | |
{ | |
foreach ($options as $param => $value) { | |
$array[$param] = $value; | |
} | |
$query = http_build_query($array); | |
return $query; | |
} | |
/** | |
* | |
* Request information | |
* | |
* @param string $url | |
* @param string $header | |
* @param string $params | |
*/ | |
protected function _sendResponse($url, $header = null, $params = null){ | |
$ch=curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
$result=curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
/** | |
* | |
* Send text message with information | |
*/ | |
public function textMessage() | |
{ | |
$options = array( | |
'api_key' => self::IBP_KEY, | |
'action' => 'sms.send_message', | |
'to' => $this->_to, | |
'from' => $this->_from, | |
'message' => $this->getData() | |
); | |
$query = $this->setQuery($options); | |
$url = self::IBP_BASE . $query; | |
return $this->_sendResponse($url); | |
} | |
/** | |
* | |
* Wrapper for _sendMessage method | |
*/ | |
public function execMessage(){ | |
$this->textMessage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment