public
Last active

Quick PHP Functions to call Brown Paper Tickets API to get event and dates

  • Download Gist
bpt.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
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' => 'donations@thetorchtheatre.com'
);
}
else
{
$params = array(
'id' => 'Fjk4VxX2pn',
'client' => 'donations@thetorchtheatre.com',
'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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.