Skip to content

Instantly share code, notes, and snippets.

@geeksunny
Created January 6, 2013 12:40
Show Gist options
  • Save geeksunny/4466882 to your computer and use it in GitHub Desktop.
Save geeksunny/4466882 to your computer and use it in GitHub Desktop.
A simple class for interfacing with the Yelp API.
<?php
//error_reporting("E_ALL");
//ini_set('display_errors', '1');
// ~~~ EXAMPLE CODE ~~~ //
/*
// Set your keys here
$consumer_key = "xxxxxxxxxxxxxxxxxxxxx";
$consumer_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$token_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
$yelp = new Yelp($consumer_key, $consumer_secret, $token, $token_secret);
$yelp->setSearch('60610','restaurants');
$response = $yelp->getSearch(true);
var_dump($response);
*/
// ~~~~~~~~~~~~~~~~~~~~ //
class Yelp {
private $consumer;
private $token;
private $signature_method;
private $unsignedURL = false;
// Object Construction
public function __construct($consumer_key, $consumer_secret, $token, $token_secret) {
// OAuth library required for calling the API
require_once('lib/OAuth.php');
// Consumer object built using the OAuth library
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
// Token object built using the OAuth library
$this->token = new OAuthToken($token, $token_secret);
// Yelp uses HMAC SHA1 encoding
$this->signature_method = new OAuthSignatureMethod_HMAC_SHA1();
}
// --- Setters
// Generates the unsigned URL for our API call.
public function setSearch($location, $terms, $limit = 20, $offset = 0) {
// Location can be any type of search term. Zip, city, street address, etc.
// Terms contains the search string of what you're looking for, like 'food', 'insurance agencies', 'department stores'. Can be an array.
// If $terms is an array, convert it into a comma separated string
if (is_array($terms)) {
$terms = implode(",",$terms);
}
// If $terms or $location are empty, return false
if (empty($location) || empty($terms)) {
return false;
}
// If $limit is above 20, drop it back down.
if ($limit > 20) {
$limit = 20;
}
// Constructing the URL string.
$url = "http://api.yelp.com/v2/search";
$string = "location=$location&term=$terms&limit=$limit&offset=$offset";
$this->unsignedURL = $url."?".$string;
return true;
}
// --- Getters
public function getSearch($asArray = false) {
// Bail out of no terms have been set yet.
if (!$this->unsignedURL)
return false;
// Sign the URL with the API.
$signed_url = $this->signURL($this->unsignedURL);
// Send Yelp API Call
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch); // Yelp response
curl_close($ch);
// Handle Yelp response data
$response = json_decode($data, $asArray);
return $response;
}
// Other functions
private function signURL($unsigned_url) {
// Build OAuth Request using the OAuth PHP library. Uses the consumer and token object created above.
$oauthrequest = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'GET', $unsigned_url);
// Sign the request
$oauthrequest->sign_request($this->signature_method, $this->consumer, $this->token);
// Get the signed URL
$signed_url = $oauthrequest->to_url();
// Return the signed URL.
return $signed_url;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment