Hi!
I am working on a Google Adsense Import Class and am having a problem:
When performing multiple CURL executions, existing MySQL connections are disconnected!
Anyone has an idear as of why this occurs ?
The class:
class google_adsense {
var $username = 'xxxxxxxx';
var $password = 'xxxxxxxx';
var $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
var $curl_connection;
function adsense_connect() {
$url = 'https://www.google.com/adsense/login.do';
$params = array();
$params["username"] = $this->username;
$params["password"] = $this->password;
$params["null"] = 'Aanmelden';
if (!is_writeable('cookie.txt')) {
print '<b>GOOGLE ADSENSE CLASS</b><br>ERROR: cookie.txt does not exist or is not writeable!';
exit;
}
$this->curl_connection = curl_init();
curl_setopt($this->curl_connection, CURLOPT_POST,1);
curl_setopt($this->curl_connection, CURLOPT_POSTFIELDS,$params);
curl_setopt($this->curl_connection, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($this->curl_connection, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($this->curl_connection, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($this->curl_connection, CURLOPT_URL,$url);
curl_setopt($this->curl_connection, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($this->curl_connection, CURLOPT_RETURNTRANSFER,1);
$google_data = curl_exec($this->curl_connection);
if ($google_data) {
$google_data='';
} else {
print '<b>GOOGLE ADSENSE CLASS</b><br>ERROR: Failed to connect!';
exit;
}
}
function adsense_disconnect() {
curl_close($this->curl_connection);
}
function adsense_channel_id($channel) { // RETURN ADSENSE CHANNEL ID FOR CHANNELNAME, CREATE NEW WHEN NOT FOUND
$this->adsense_connect();
if (!$channel) {
print '<b>GOOGLE ADSENSE CLASS</b><br>ERROR: No channel specified!';
exit;
}
$url = 'https://www.google.com/adsense/channel-save.do';
$params = array();
$params["formSource"] = 'customChannels';
$params["newCustomChannelName"] = $channel;
$params["createCustomChannel"] = 'Nieuw kanaal maken';
$params["customChannelToRename.id"] = '';
$params["renamedCustomChannelName"] = '';
if (!is_resource($this->curl_connection)) {
print '<b>GOOGLE ADSENSE CLASS</b><br>ERROR: Google Adsense Connection not available or not started!';
exit;
}
curl_setopt($this->curl_connection, CURLOPT_POSTFIELDS,$params);
curl_setopt($this->curl_connection, CURLOPT_URL,$url);
$google_data = curl_exec($this->curl_connection);
$url = 'https://www.google.com/adsense/code';
curl_setopt($this->curl_connection, CURLOPT_URL,$url);
$google_data = curl_exec($this->curl_connection);
preg_match_all("|<select[^>]+name=\"channel\">(.*)</select>|Ui",
$google_data,
$out, PREG_PATTERN_ORDER);
preg_match_all("|<option value=\"(.*)\">(.*)</option>|Ui",
$out[1][0],
$channel_ids, PREG_PATTERN_ORDER);
foreach ($channel_ids[2] as $n => $c) {
$channel_ids[2][$n] = trim($c);
}
$channel_id = trim($channel_ids[1][trim(array_search ($channel,$channel_ids[2]))]);
$this->adsense_disconnect();
if ($channel_id) {
return $channel_id;
} else {
return false;
}
}
function adsense_get_stats($channel,$start,$end) {
$url = 'https://www.google.com/adsense/report/aggregate';
$params = array();
$params["product"] = 'afc';
$params["dateRange.dateRangeType"] = 'custom';
$params["dateRange.customDate.start.day"] = date("j",strtotime($start));
$params["dateRange.customDate.start.month"] = date("n",strtotime($start));
$params["dateRange.customDate.start.year"] = date("Y",strtotime($start));
$params["dateRange.customDate.end.day"] = date("j",strtotime($end));
$params["dateRange.customDate.end.month"] = date("n",strtotime($end));
$params["dateRange.customDate.end.year"] = date("Y",strtotime($end));
$params["groupByPref"] = 'date';
$params["reportType"] = 'channel';
$params["c.id"] = '1615735';
$params["null"] = 'Rapport weergeven';
if (!is_resource($this->curl_connection)) {
print '<b>GOOGLE ADSENSE CLASS</b><br>ERROR: Google Adsense Connection not available or not started!';
exit;
}
curl_setopt($this->curl_connection, CURLOPT_POSTFIELDS,$params);
curl_setopt($this->curl_connection, CURLOPT_URL,$url);
$google_data = curl_exec ($this->curl_connection);
if ($google_data) {
$google_data='';
return true;
} else {
return false;
}
}
}