Skip to content

Instantly share code, notes, and snippets.

@kressaty
Created June 25, 2011 21:37
Show Gist options
  • Save kressaty/1046927 to your computer and use it in GitHub Desktop.
Save kressaty/1046927 to your computer and use it in GitHub Desktop.
Quick PHP Functions to call Brown Paper Tickets API to get event and dates
function bpt_handler($atts)
{
extract(shortcode_atts(array(
"event_id" => ''
), $atts));
$event_data = $this->getBptEvent($event_id);
return '<pre>'.$event_data.'</pre>';
}
add_shortcode('bpt', 'bpt_handler');
function getBptEvent($event_id='184397')
{
$endpoint = "https://www.brownpapertickets.com/api2/eventlist";
if(empty($event_id) OR $event_id == null OR $event_id == '')
{
$params = array(
'id' => 'Fjk4VxX2pn',
'client' => '[email protected]'
);
}
else
{
$params = array(
'id' => 'Fjk4VxX2pn',
'client' => '[email protected]',
'event_id' => $event_id
);
}
$callback_data = $params;
$encoded = "";
foreach($callback_data AS $key=>$value)
$encoded .= "$key=".urlencode($value)."&";
$encoded = substr($encoded, 0, -1);
//echo "starting";
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$endpoint);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_USERPWD, TWILIO_TOKEN.":X");
curl_setopt($ch,CURLOPT_POSTFIELDS,$encoded);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 50);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
//execute post
if(FALSE === ($result = curl_exec($ch)))
{
echo "Curl failed with error " . curl_error($ch);
return;
}
// get result code
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (intval($responseCode / 100) != 2)
{
echo 'Error while requesting. Service returned HTTP code '.$responseCode.' and curl error '.curl_error($ch);
return;
}
//close connection
curl_close($ch);
//dumpVars($result);
$event = new SimpleXMLElement($result, LIBXML_NOWARNING);
foreach($event->event as $e)
{
$event_individual = get_object_vars($e);
//$e->dates = $this->getBptEventDates($e->event_id);
$event_individual['dates'] = $this->getBptEventDates($e->event_id);
$output[] = $event_individual;
}
dumpVars($output);
}
function getBptEventDates($event_id)
{
$endpoint = "https://www.brownpapertickets.com/api2/datelist";
$params = array(
'id' => 'Fjk4VxX2pn',
'event_id' => $event_id
);
$callback_data = $params;
$encoded = "";
foreach($callback_data AS $key=>$value)
$encoded .= "$key=".urlencode($value)."&";
$encoded = substr($encoded, 0, -1);
//echo "starting";
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$endpoint);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_USERPWD, TWILIO_TOKEN.":X");
curl_setopt($ch,CURLOPT_POSTFIELDS,$encoded);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 50);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
//execute post
if(FALSE === ($result = curl_exec($ch)))
{
echo "Curl failed with error " . curl_error($ch);
return;
}
// get result code
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (intval($responseCode / 100) != 2)
{
echo 'Error while requesting. Service returned HTTP code '.$responseCode.' and curl error '.curl_error($ch);
return;
}
//close connection
curl_close($ch);
//dumpVars($result);
$date = new SimpleXMLElement($result, LIBXML_NOWARNING);
foreach($date->date as $d)
{
$dates[] = get_object_vars($d);
}
return $dates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment