Skip to content

Instantly share code, notes, and snippets.

@chinnurtb
Created October 30, 2013 13:22
Show Gist options
  • Select an option

  • Save chinnurtb/7232582 to your computer and use it in GitHub Desktop.

Select an option

Save chinnurtb/7232582 to your computer and use it in GitHub Desktop.
<?php
function gs_adreq($adreq) {
// Required variable checking
if(! array_key_exists('site_id', $adreq) ||
! array_key_exists('HTTP_USER_AGENT', $_SERVER)) {
return 'no site id or user agent';
}
// Configuration
$url = 'http://adsx.greystripe.com/openx/www/delivery/mw2.php';
$timeout = array_key_exists('timeout', $adreq) ? $adreq['timeout'] : 5;
unset($adreq['timeout']);
// request parameters
$adreq['language'] = 'php';
$adreq['version'] = '1.0';
$adreq['format'] = 'html';
if (! array_key_exists('ad_id', $adreq)) {
$adreq['ad_id'] = rand(1000,9999);
}
$params = implode_with_keys('&', '=', $adreq);
// phone headers
$ignoredHeaders = array('HTTP_PRAGMA', 'HTTP_CACHE_CONTROL', 'HTTP_CONNECTION',
'HTTP_COOKIE', 'HTTP_ACCEPT');
$phoneHeaders = array();
foreach ($_SERVER as $k => $v) {
if (! in_array($k, $ignoredHeaders)) {
$phoneHeaders[$k] = $v;
}
}
if (count($phoneHeaders) > 0) {
$params .= '&phone_headers=' . urlencode(implode_with_keys('||', '=>', $phoneHeaders));
}
// request ad
$request = curl_init();
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($request, CURLOPT_HEADER, 1);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_TIMEOUT, $timeout);
curl_setopt($request, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($request, CURLOPT_HTTPHEADER,
array('Content-Type: application/x-www-form-urlencoded', 'Connection: Close'));
curl_setopt($request, CURLOPT_POSTFIELDS, $params);
$ad_contents = curl_exec($request);
$header_size = curl_getinfo($request, CURLINFO_HEADER_SIZE);
$header = substr($ad_contents, 0, $header_size);
$html = substr($ad_contents, $header_size, strlen($ad_contents));
if (preg_match('/Greystripe-ErrorCode:\s*(.*)/i', $header, $matches)) {
$greystripe_error_code = trim($matches[1]);
}
if (preg_match('/Greystripe-ErrorReason:\s*(.*)/i', $header, $matches)) {
$greystripe_error_reason = trim($matches[1]);
}
if(!empty($greystripe_error_code) && !empty($greystripe_error_reason)) {
$errMsg = '';
/*
* COMMENT OUT FOR ERROR MSGS!!
*
$errMsg .= '\n<!--\n';
$errMsg .= 'Greystripe Error Code: ' . $greystripe_error_code . '\n';
$errMsg .= 'Greystripe Error Reason: '. $greystripe_error_reason . '\n';
$errMsg .= '\n-->\n';
*/
return $errMsg;
}
$code = curl_getinfo($request, CURLINFO_HTTP_CODE);
curl_close($request);
if($code != 200) {
return '';
}
return trim($html);
}
function implode_with_keys($glue, $subglue, $array) {
$ret = array();
foreach($array as $key => $value) {
if (is_array($value)) {
$value = implode(',', $value);
}
$ret[] = urlencode($key).$subglue.urlencode($value);
}
return implode($glue, $ret);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment