Aanpassing aan programma - Facebooks Marketing API

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Top Low-Code Developer Gezocht!

Bedrijfsomschrijving Unieke Kansen, Uitstekende Arbeidsvoorwaarden & Inspirerend Team Wij zijn een toonaangevende, internationale organisatie die de toekomst van technologie vormgeeft door het creëren van innovatieve en baanbrekende oplossingen. Ons succes is gebaseerd op een hecht en gepassioneerd team van professionals die altijd streven naar het overtreffen van verwachtingen. Als jij deel wilt uitmaken van een dynamische, vooruitstrevende en inspirerende werkomgeving, dan is dit de perfecte kans voor jou! Functieomschrijving Als Low-Code Developer ben je een cruciaal onderdeel van ons team. Je werkt samen met collega's uit verschillende disciplines om geavanceerde applicaties te ontwikkelen en te optimaliseren met behulp van Low-code

Bekijk vacature »

Lise A

Lise A

03/07/2015 09:02:59
Quote Anchor link
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?

Toevoeging op 03/07/2015 09:04:49:

Dit is de PHP code:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?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()) .  '&currency=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']);
}

?>
Gewijzigd op 03/07/2015 09:09:10 door Lise A
 
PHP hulp

PHP hulp

28/03/2024 21:17:45
 
Pipo Clown

Pipo Clown

03/07/2015 11:22:02
Quote Anchor link
Wanneer ik het goed begrijp is het de bedoeling dat wij jouw onderzoek gaan doen ?
 
Lise A

Lise A

03/07/2015 11:24:30
Quote Anchor link
Nee jullie zouden me helpen met het verzamelen van de data die ik gebruik voor de analyse in mijn thesis.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.