Voor een thesis onderzoek heb ik informatie nodig uit Facebooks Marketing API. Binnen die app kan je vinden hoe groot het bereik is van een bepaalde muziek artiest. Je kunt ook vinden hoe groot het bereik is van twee artiesten samen. Ik zou graag een programma hebben dat automatisch al deze combinaties tussen artiesten in één keer geeft.
Iemand van Freelancer.com heeft hier een aantal maanden geleden een werkend programma voor gemaakt, maar nu is intern bij Facebook de API enigszins veranderd en werkt dat programma niet meer. Ik heb zelf totaal geen verstand van programmeren, maar ga er vanuit dat de aanpassingen aan de PHP code klein hoeven te zijn om het weer werkend te maken. Heeft een van jullie idee hoe ik dit het beste aan kan passen?
[size=xsmall]Toevoeging op 03/07/2015 09:04:49:[/size]
Dit is de PHP code:
<?php
if (!isset($_GET['js'])) {
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Get FB audience stats</title>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
document.onreadystatechange = function() {
document.getElementById('country_code').value = '<?php if (isset($_POST['country_code']) && $_POST['country_code'] != '') echo $_POST['country_code']; else echo 'US'; ?>';
$('#process').submit(function() {
var ts = (new Date()).getTime();
$.ajax({
url: '?js=1&ts=' + ts,
type: 'post',
data: {country_code: $('#country_code').val(), age_min: $('#age_min').val(), age_max: $('#age_max').val(), access_token: $('#access_token').val(), account_id: $('#account_id').val(), input: $('#input').val()}
});
$('#output').show();
$('#result_file').attr('href', 'output.' + ts + '.csv').text('output.' + ts + '.csv');
$('#log_file').attr('href', 'log.' + ts + '.txt').text('log.' + ts + '.txt');
$(window).scrollTop(400);
var max = $('#input').val().split("\n").length;
max = (max * (max - 1)) / 2;
var percents = 0;
var status = '' + percents + '% - processing ';
var status_processing = window.setInterval(function() {
if (status.length > 20) {
status = '' + percents + '% - processing ';
} else {
status = status + '.';
}
$('.status').text(status);
}, 300);
var check_processing = window.setInterval(function() {
$.ajax({
url: '?js=1&echo=1&ts=' + ts + '&_=' + (new Date()).getTime(),
type: 'get',
success: function(data) {
var lines = data.split("\n").length;
percents = Math.floor((lines/max) * 100);
$('#text_log').html(data);
if (~data.indexOf('Script finished!!!')) {
window.clearInterval(status_processing);
window.clearInterval(check_processing);
$('.staus').text('finished');
}
}
});
}, 5000);
return false;
});
};
</script>
</head>
<body>
<h1>Get audience overlaping</h1>
<h3>Status: <span class="status">ready</span></h3>
<form id="process" action="" method="POST">
<label for="country_code">Country Code:</label>
<select name="country_code" id="country_code">
<option value="DZ">Algeria</option>
<option value="AI">Anguilla</option>
<option value="AG">Antigua</option>
<option value="AR">Argentina</option>
<option value="AU">Australia</option>
<option value="AT">Austria</option>
<option value="BS">Bahamas</option>
<option value="BH">Bahrain</option>
<option value="BB">Barbados</option>
<option value="BY">Belarus</option>
<option value="BE">Belgium</option>
<option value="BM">Bermuda</option>
<option value="BW">Botswana</option>
<option value="BR">Brazil</option>
<option value="VG">British Virgin Islands</option>
<option value="BN">Brunei</option>
<option value="BG">Bulgaria</option>
<option value="CA">Canada</option>
<option value="KY">Cayman Islands</option>
<option value="CL">Chile</option>
<option value="CN">China</option>
<option value="CO">Colombia</option>
<option value="HR">Croatia</option>
<option value="CY">Cyprus</option>
<option value="CZ">Czech Republic</option>
<option value="DK">Denmark</option>
<option value="DM">Dominica</option>
<option value="DO">Dominican Republic</option>
<option value="EG">Egypt</option>
<option value="SV">El Salvador</option>
<option value="EE">Estonia</option>
<option value="FI">Finland</option>
<option value="FR">France</option>
<option value="GY">French Guiana</option>
<option value="GE">Georgia</option>
<option value="DE">Germany</option>
<option value="GI">Gibraltar</option>
<option value="GR">Greece</option>
<option value="GD">Grenada</option>
<option value="GP">Guadeloupe</option>
<option value="GT">Guatemala</option>
<option value="HK">Hong Kong</option>
<option value="HU">Hungary</option>
<option value="IS">Iceland</option>
<option value="IN">India</option>
<option value="ID">Indonesia</option>
<option value="IR">Iran</option>
<option value="IE">Ireland</option>
<option value="IL">Israel</option>
<option value="IT">Italy</option>
<option value="JM">Jamaica</option>
<option value="JP">Japan</option>
<option value="JO">Jordan</option>
<option value="KZ">Kazakhstan</option>
<option value="KR">Korea, South</option>
<option value="KG">Kyrgyzstan</option>
<option value="LV">Latvia</option>
<option value="LT">Lithuania</option>
<option value="LU">Luxembourg</option>
<option value="MO">Macau</option>
<option value="MY">Malaysia</option>
<option value="MT">Malta</option>
<option value="MQ">Martinique</option>
<option value="MX">Mexico</option>
<option value="MD">Moldova</option>
<option value="MC">Monaco</option>
<option value="MS">Montserrat</option>
<option value="NL">Netherlands</option>
<option value="NZ">New Zealand</option>
<option value="NO">Norway</option>
<option value="OM">Oman</option>
<option value="PA">Panama</option>
<option value="PE">Peru</option>
<option value="PH">Philippines</option>
<option value="PL">Poland</option>
<option value="PT">Portugal</option>
<option value="PR">Puerto Rico</option>
<option value="QA">Qatar</option>
<option value="RE">Reunion</option>
<option value="RO">Romania</option>
<option value="RU">Russian Federation</option>
<option value="LC">Saint Lucia</option>
<option value="VC">Saint Vincent and the Grenadines</option>
<option value="SA">Saudi Arabia</option>
<option value="RS">Serbia</option>
<option value="SG">Singapore</option>
<option value="SK">Slovakia</option>
<option value="SI">Slovenia</option>
<option value="ZA">South Africa</option>
<option value="ES">Spain</option>
<option value="LK">Sri Lanka</option>
<option value="KN">St.Kitts and Nevis</option>
<option value="SE">Sweden</option>
<option value="CH">Switzerland</option>
<option value="TW">Taiwan</option>
<option value="TJ">Tajikistan</option>
<option value="TH">Thailand</option>
<option value="TT">Trinidad and Tobago</option>
<option value="TR">Turkey</option>
<option value="UA">Ukraine</option>
<option value="AE">United Arab Emirates</option>
<option value="GB">United Kingdom</option>
<option value="UI">Universal Toll Free</option>
<option value="UY">Uruguay</option>
<option value="US">USA</option>
<option value="VE">Venezuela</option>
<option value="VN">Vietnam</option>
</select>
<br/><br/>
<label for="age_min">Min age:</label> <input id="age_min" type="text" name="age_min" value="<?php if (isset($_POST['age_min']) && $_POST['age_min'] != '') echo $_POST['age_min']; else echo '18'; ?>" size="3" /><br/>
<label for="age_max">Max age:</label> <input id="age_max" type="text" name="age_max" value="<?php if (isset($_POST['age_max']) && $_POST['age_max'] != '') echo $_POST['age_max']; else echo '65'; ?>" size="3" /><br/>
<br/>
<label for="access_token">Access token:</label> <input id="access_token" type="text" name="access_token" value="<?php if (isset($_POST['access_token']) && $_POST['access_token'] != '') echo $_POST['access_token']; else echo 'CAACZBzNhafycBAHAtT9Wc3s37j3HiKYF57236wbnQbLypdbeG24RW4TwQyF4LrrDBYqpntWZCoey1FGO5v90hhUU7rFBlCH5crZARWd1VaaKfoA4c1Tg1gV69wrEDb6pqBUjZBmbGxZCYVcsfWnG7G4SxjCngb03EneJuCo73ZAvIGyyB7VLZCEddrLOwXNtkXUZAKES0cbK7ZA4gGvOe2JDA'; ?>" size="40" /><br/>
<label for="account_id">Account id:</label> <input id="account_id" type="text" name="account_id" value="<?php if (isset($_POST['account_id']) && $_POST['account_id'] != '') echo $_POST['account_id']; else echo '10204682197113332'; ?>" size="10" /><br/>
<br/>
<label for="input">Groups list:</label><br/><textarea id="input" name="input" rows="40" cols="30"><?php if (isset($_POST['input']) && $_POST['input'] != '') echo $_POST['input']; ?></textarea><br/><br/>
<input type="submit" name="submit" value="submit"/>
</form>
<div id="output" style="display:none;">
<br/><br/>
<hr/>
<br/><br/>
<h3>Status: <span class="status">ready</span></h3>
<h3>Output:</h3>
<b>Result file:</b> <a id="result_file" href="" target="_blank"></a><br/>
<b>Log file:</b> <a id="log_file" href="" target="_blank"></a><br/>
<i>The files are not completed until the script is not finished!</i>
<p id="text_log"></p>
</div>
</body>
</html>
<?php
} else if (isset($_GET['echo']) && isset($_GET['ts'])) {
echo file_get_contents('echo.' . $_GET['ts'] . '.txt');
} else if (isset($_POST['input']) && $_POST['input'] != '' && isset($_GET['ts'])) {
ini_set('max_execution_time', 9999);
$access_token = $_POST['access_token'];
$account_id = $_POST['account_id'];
$country_code = $_POST['country_code'];
$age_min = $_POST['age_min'];
$age_max = $_POST['age_max'];
$groups = array();
$groups_info = array();
$output = fopen('output.' . $_GET['ts'] . '.csv', 'w');
$log = fopen('log.' . $_GET['ts'] . '.txt', 'w');
$echo = fopen('echo.' . $_GET['ts'] . '.txt', 'w');
fwrite($log, date() . " [LOG] Start\n");
fwrite($echo, date() . " Script started <br/>\n");
$lines = explode("\n", $_POST['input']);
foreach ($lines as $line) {
$groups[] = trim(str_replace(array("\r","\n","\t"), '', $line));
}
$c = 0;
fwrite($output, "first group name;first group audience size;second group name;second group audience size;overlapping audience\n");
for ($i = 0; $i < count($groups) - 1; $i++) {
for ($j = $i + 1; $j < count($groups); $j++) {
if (!isset($groups_info[$i])) {
$groups_info[$i] = get_group_info($groups[$i], $access_token);
$targeting_spec = array(
'age_max' => $age_max,
'age_min' => $age_min,
'interests' => array(
array(
'id' => $groups_info[$i]['id'],
'name' => $groups_info[$i]['name']
)
),
'geo_locations' => array(
'countries' => array($country_code),
'location_types' => array('home', 'recent')
)
);
$groups_info[$i]['estimate'] = estimate($targeting_spec, $access_token, $account_id);
}
$first_group = $groups_info[$i];
if (!isset($groups_info[$j])) {
$groups_info[$j] = get_group_info($groups[$j], $access_token);
$targeting_spec = array(
'age_max' => $age_max,
'age_min' => $age_min,
'interests' => array(
array(
'id' => $groups_info[$j]['id'],
'name' => $groups_info[$j]['name']
)
),
'geo_locations' => array(
'countries' => array($country_code),
'location_types' => array('home', 'recent')
)
);
$groups_info[$j]['estimate'] = estimate($targeting_spec, $access_token, $account_id);
}
$second_group = $groups_info[$j];
$targeting_spec = array(
'age_max' => $age_max,
'age_min' => $age_min,
'interests' => array(
array(
'id' => $first_group['id'],
'name' => $first_group['name']
),
array(
'id' => $second_group['id'],
'name' => $second_group['name']
)
),
'geo_locations' => array(
'countries' => array($country_code),
'location_types' => array('home', 'recent')
)
);
$estimate = estimate($targeting_spec, $access_token, $account_id);
$overlapping = ($first_group['estimate'] + $second_group['estimate']) - $estimate;
if ($estimate) {
fwrite($output, "{$first_group['name']};{$first_group['estimate']};{$second_group['name']};{$second_group['estimate']};$estimate;$overlapping\n");
fwrite($log, date() . " [LOG] RESULT: " . $first_group['name'] . " AND " . $second_group['name'] . " has $estimate audience size!\n");
fwrite($echo, date() . " - " . $c++ . " - Got result <br/>\n");
} else {
fwrite($log, date() . " [LOG] PROBLEM: " . $first_group['name'] . " AND " . $second_group['name'] . " has no get audience size!\n");
fwrite($echo, date() . " - " . $c++ . " - Problem <br/>\n");
}
}
}
fwrite($echo, date() . " Script finished!!! <br/>\n");
fwrite($output, "\n");
fclose($output);
fclose($log);
fclose($echo);
}
function get($url) {
$user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36";
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($c, CURLOPT_COOKIEJAR, "cookie.txt");
//curl_setopt($c, CURLOPT_PROXY, "10.233.104.135:3128");
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($c, CURLOPT_USERAGENT, $user_agent);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
$src = curl_exec($c);
global $log;
fwrite($log, date() . " [LOG] GET: $url ERR:" . curl_errno($c) . "/" . curl_error($c) . "\n");
usleep(rand(100, 500));
curl_close($c);
return $src;
}
function generate_cb_name() {
$hex = dechex(rand(1000, 9999)) . dechex(rand(1000, 9999)) . dechex(rand(1000, 9999));
$hex = substr($hex, 0, 9);
return '__globalCallbacks.f' . $hex;
}
function encode_json_response($src) {
if (strpos($src, '({') === false) return FALSE;
$src = substr($src, strpos($src, '({') + 1);
$src = substr($src, 0, strpos($src, '})') + 1);
return json_decode($src, true);
}
function get_group_info($group_name, $access_token) {
$search_url = 'https://graph.facebook.com/v2.2/search?access_token=' . $access_token . '&callback=' . urlencode(generate_cb_name()) . '&endpoint=%2Fsearch&q=' . urlencode($group_name) . '&limit=20&locale=en_US&method=get&offset=0&pretty=0&type=adInterest';
$search_src = encode_json_response( get($search_url) );
if (!$search_src || !isset($search_src['data']) || empty($search_src['data'])) return FALSE;
// Get the maximal identical
foreach ($search_src['data'] as $e) {
if ($e['name'] === $group_name) return $e;
}
foreach ($search_src['data'] as $e) {
if (strpos($e['name'], $group_name) !== false) return $e;
}
foreach ($search_src['data'] as $e) {
if (stripos($e['name'], $group_name) !== false) return $e;
}
return $search_src['data'][0];
}
function estimate($targeting_spec, $access_token, $account_id) {
$targeting_spec = urlencode(json_encode($targeting_spec));
$estimate_url = "https://graph.facebook.com/v2.2/act_$account_id/reachestimate?access_token=" . $access_token . '&accountId=' . $account_id . '&bid_for=%5B%22conversion%22%5D&callback=' . urlencode(generate_cb_name()) . '¤cy=USD&endpoint=' . urlencode("/act_$account_id/reachestimate") . '&locale=en_US&method=get&pretty=0&targeting_spec=' . $targeting_spec;
$estimate_src = encode_json_response( get($estimate_url) );
if (!$estimate_src || !isset($estimate_src['users'])) return FALSE;
return intval($estimate_src['users']);
}
?>
1.344 views