Created
April 17, 2013 08:53
-
-
Save Maks3w/5402827 to your computer and use it in GitHub Desktop.
BitLy bulk actions. PHP script for bulk actions like archive, edit, etc using BitLy API
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 | |
/** | |
* BitLy bulk actions | |
* | |
* PHP script for make bulk actions with BitLy. | |
* | |
* Instructions: Customize parameters and edit the foreach loop for more detailed filtering | |
* | |
* @author Maks3w | |
*/ | |
// Customizable parameters | |
$token = '<bitly auth token>'; // Put here your BitLy token | |
$fromWs = true; // Turn false for read from previously saved results ($dataFile) | |
$saveToDisk = true; // Save search results in $dataFile | |
$dataFile = __DIR__ . '/data.json'; | |
$searchParams = 'limit=100'; // /v3/user/link_history parameters | |
$bulkAction = '/v3/user/link_edit'; | |
$actionParams = 'edit=archived&archived=true&link='; // This example mark the link as archived. link (shorten link) value is added later | |
// End customizable parameters | |
$continue = true; | |
$domain = 'https://api-ssl.bitly.com'; | |
$searchMethod = '/v3/user/link_history'; | |
$apiMask = '%s%s?access_token=%s&%s'; | |
$searchUrl = sprintf($apiMask, $domain, $searchMethod, $token, $searchParams); | |
while ($continue) { | |
if ($fromWs) { | |
$response = file_get_contents($searchUrl); | |
if ($saveToDisk) { | |
file_put_contents($dataFile, $response); | |
} | |
} else { | |
$searchUrl = $dataFile; | |
$response = file_get_contents($searchUrl); | |
} | |
$data = json_decode($response); | |
if ($data->status_code != '200') { | |
throw new Exception($searchUrl . ' > ' . json_encode($response)); | |
} | |
$links = $data->data->link_history; | |
echo PHP_EOL, count($links), PHP_EOL; // Number of links retrieved from the API | |
$url = sprintf($apiMask, $domain, $bulkAction, $token, $actionParams); | |
$continue = false; | |
foreach ($links as $link) { | |
// Filter condition | |
/* | |
if (!strpos($link->long_url, 'some word present in long url')) { | |
continue; | |
} | |
*/ | |
$continue = $fromWs; // If at least there is one positive then continue for next search unless is from disk. | |
$response = json_decode(file_get_contents($url . $link->link)); | |
if ($response->status_code != '200') { | |
throw new Exception($url . $link->link . ' > ' . json_encode($response)); | |
} | |
echo sprintf('{%s, OK}', $link->link); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment