Last active
October 3, 2016 19:43
-
-
Save tharmann/5dc60e57ff84b7ee13855472097558bc to your computer and use it in GitHub Desktop.
This is a simple snippet that I intent to complexify later. I used it to remove 1000+ posts that were created automatically (via a WordPress plugin) on a Facebook page. It takes a search string and looks for posts matching that string - then it deletes them. Keep in mind that you have to use FB's paging scheme to search through a large number of…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//make sure your page access token has the 'publish_actions' scope | |
$access_token = 'YOURACCESSTOKEN'; | |
$pageid = 'YOURPAGEID'; | |
//fb graph api only allows this to be up to 100 | |
$limit = '100'; | |
//retireve posts on specific page ID: | |
$fb_ret_url = 'https://graph.facebook.com/' . $pageid . '/posts?limit=' . $limit . '&access_token=' . $access_token; | |
//retrieve the data | |
$response = file_get_contents( $fb_ret_url ); | |
//parse the data | |
$response_parsed = json_decode( $response, true ); | |
//loop through the data and find/delete the posts that match our string in the message field | |
//this only searches for matches on the first page of results - for searching everything, look at the $response_parsed['paging'] to get the next and previous page URL's | |
$search_string = 'Something to search on..'; | |
$count = 0; | |
foreach ( $response_parsed['data'] as $fbpost ) { | |
//switch out $fbpost['message'] with the field that you want to search on - e.g. ['created_time'] | |
if ( isset( $fbpost['message'] ) ) { | |
$found = strpos( $fbpost['message'], $search_string ); | |
if ( $found !== false ) { | |
$delurl = 'https://graph.facebook.com/' . $fbpost['id'] . '?access_token=' . $access_token; | |
$ch = curl_init(); | |
curl_setopt( $ch, CURLOPT_URL, $delurl ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false ); | |
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE' ); | |
$output = curl_exec( $ch ); | |
curl_close( $ch ); | |
//check if the operation was succsesful and count | |
if ( $output ) { | |
$count++; | |
} | |
} | |
} | |
} | |
//display results | |
if ( $count === 1 ) { | |
echo '<h1 align="center">' . $count . ' FACEBOOK POST DELETED</h1>'; | |
} else { | |
echo '<h1 align="center">' . $count . ' FACEBOOK POSTS DELETED</h1>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment