Last active
June 7, 2017 06:15
-
-
Save rajivseelam/9462662 to your computer and use it in GitHub Desktop.
Create Google Alert and grab the feed.
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
Google alerts is a way to manage your content by subscribing to keywords. Here is an example on how to make that google alert with php. | |
Get the feed url and parse the feed. There won't be any feed available as soon the alert is created. | |
------------------------------------- | |
Copy composer.json and do composer update. | |
Add this to providers | |
'Awjudd\FeedReader\FeedReaderServiceProvider', | |
Add this to aliases | |
'FeedReader' => 'Awjudd\FeedReader\Facades\FeedReader', | |
Add route to routes.php. | |
Copy GAlerts.php in app/helpers folder. | |
Then make app/start/global.php look like following: | |
ClassLoader::addDirectories(array( | |
app_path().'/commands', | |
app_path().'/controllers', | |
app_path().'/models', | |
app_path().'/database/seeds', | |
app_path().'/helpers', | |
)); |
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
{ | |
"name": "laravel/laravel", | |
"description": "The Laravel Framework.", | |
"keywords": ["framework", "laravel"], | |
"license": "MIT", | |
"require": { | |
"laravel/framework": "4.1.*", | |
"simplepie/simplepie": "dev-master", | |
"awjudd/feed-reader": "dev-master" | |
}, | |
"autoload": { | |
"classmap": [ | |
"app/commands", | |
"app/controllers", | |
"app/models", | |
"app/database/migrations", | |
"app/database/seeds", | |
"app/tests/TestCase.php" | |
] | |
}, | |
"scripts": { | |
"post-install-cmd": [ | |
"php artisan clear-compiled", | |
"php artisan optimize" | |
], | |
"post-update-cmd": [ | |
"php artisan clear-compiled", | |
"php artisan optimize" | |
], | |
"post-create-project-cmd": [ | |
"php artisan key:generate" | |
] | |
}, | |
"config": { | |
"preferred-install": "dist" | |
}, | |
"minimum-stability": "stable" | |
} |
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 | |
Route::get('galert',function(){ | |
$Ga = new GAlerts(); | |
$result = $Ga->createAlert('A R Rahman'); | |
var_dump($result); | |
$feed = FeedReader::read($result); | |
foreach ($feed->get_items() as $item) { | |
echo '<pre>'; | |
echo $item->get_link().'<br>'; | |
echo $item->get_title().'<br>'; | |
echo $item->get_description().'<br>'; | |
echo $item->get_date().'<br>'; | |
echo $item->get_id().'<br>'; | |
echo $item->get_feed().'<br>'; | |
echo '</pre>'; | |
echo '<br>'; | |
$result = explode('?q=',$item->get_link())[1]; | |
$result = explode('&',$result)[0]; | |
echo $result; | |
echo file_get_contents($result); | |
echo '<br>-----------------------------------------<br>'; | |
} | |
}); |
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 | |
// --- GOOGLE ACCOUNT SETTINGS ------------------------- | |
define( 'GOOGLE_USERNAME', '[email protected]' ); | |
define( 'GOOGLE_PASSWORD', 'supersecretpassword'); | |
// --- ALERT SETTINGS ---------------------------------- | |
// alert frequency ( how often you recieve emails ) | |
// - as it happens: 0 | |
// - daily: 1 | |
// - weekly: 6 | |
define( 'ALERT_FREQUENCY', 0 ); | |
// result type ( what to seaerch ) | |
// - everything: 7 | |
// - discussions: 8 | |
// - news: 1 | |
// - blogs: 4 | |
// - video: 9 | |
// - books: 22 | |
define( 'ALERT_RESULT_TYPE', 7 ); | |
// result quality filter | |
// - best results: 0 | |
// - all results: 1 | |
define( 'ALERT_QUALITY', 0 ); | |
// --- CURL SETTINGS ---------------------------------- | |
define( 'GOOGLE_COOKIEFILE', 'cookies.txt' ); | |
class GAlerts | |
{ | |
public function createAlert( $search ) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); | |
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_COOKIEJAR, GOOGLE_COOKIEFILE); | |
curl_setopt($ch, CURLOPT_COOKIEFILE, GOOGLE_COOKIEFILE); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 120); | |
curl_setopt($ch, CURLOPT_URL, | |
'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage'); | |
$data = curl_exec($ch); | |
$formFields = $this->getFormFields( $data ); | |
$formFields['Email'] = GOOGLE_USERNAME; | |
$formFields['Passwd'] = GOOGLE_PASSWORD; | |
unset($formFields['PersistentCookie']); | |
$post_string = ''; | |
foreach($formFields as $key => $value) { | |
$post_string .= $key . '=' . urlencode($value) . '&'; | |
} | |
$post_string = substr($post_string, 0, -1); | |
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth'); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); | |
$result = curl_exec($ch); | |
if (strpos($result, '<title>Redirecting') === false) { | |
var_dump($result); | |
die("Login failed"); | |
} else { | |
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts'); | |
curl_setopt($ch, CURLOPT_POST, 0); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, null); | |
$result = curl_exec($ch); | |
// Create alert | |
preg_match('/<input type="hidden" name="x" value="([^"]+)"/', $result, $matches); | |
$post = array( | |
"x" => $matches[1], // anti-XSRF key | |
"q" => $search, // Search term | |
"t" => ALERT_RESULT_TYPE, // Result type: 7-everything, 8-discussions | |
"f" => ALERT_FREQUENCY, // Frequency: 0: as it happens, 1: daily, 6: weekly | |
"l" => ALERT_QUALITY, // All results: 1, best results: 0 | |
"e" => "feed" // Type of delivery: rss: "feed" | |
); | |
$post_string = ''; | |
foreach($post as $key => $value) { | |
$post_string .= $key . '=' . urlencode($value) . '&'; | |
} | |
$post_string = substr($post_string, 0, -1); | |
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts/create'); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); | |
$result = curl_exec($ch); | |
$matches = array(); | |
preg_match('#<a href="(http://www.google.com/alerts/feeds/[\d/]+)"#', $result, $matches); | |
$top_alert = $matches[1]; | |
return $top_alert; | |
} | |
} | |
private function getFormFields($data) | |
{ | |
if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) { | |
$inputs = $this->getInputs($matches[1]); | |
return $inputs; | |
} else { | |
die("didn't find login form"); | |
} | |
} | |
private function getInputs($form) | |
{ | |
$inputs = array(); | |
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches); | |
if ($elements > 0) { | |
for($i = 0; $i < $elements; $i++) { | |
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]); | |
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) { | |
$name = $name[1]; | |
$value = ''; | |
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) { | |
$value = $value[1]; | |
} | |
$inputs[$name] = $value; | |
} | |
} | |
} | |
return $inputs; | |
} | |
} // end class | |
?> |
Hi @rajivseelam and @theodis, This code is not working for me (error always: login failed). Do you have same problem?
Agree with @djeraseit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.google.com/alerts/create
This url does not exit. It should also be https:
I believe curl won't work as the site looks to be Ajax.