Fetch Feed Subscribers from Google Reader with CURL

FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Fetch Feed Subscribers from Google Reader with CURL

example code using PHP - CURL binding that uses libcurlGoogle Reader Logo
This PHP CURL example uses cookies, POST, and SSL options to login to Google Reader and fetch the number of subscribers for a feed.

Online Demo

Curl PHP Demo Fetches Feed Subscribers from GoogleType in any feed and get the subscriber count from google reader.

PHP Curl Code

Note: If you don’t have a Google Reader account you need to get one for this script to function.

Download

<?php
/*
01/08/2008 By AskApache
http://www.askapache.com/security/curl-google-post-feed.html
 
This script will login to google reader with the $username and
$password variables and fetch the number of subscribers for
the $feedurl variable that you specify.
 
DEMO: http://www.askapache.com/online-tools/curl-google-feed/
*/

 
/****************************************
   SETTINGS
****************************************/
// if($_SERVER['REMOTE_ADDR'] !== '1.1.1.1')die();// only allow IP 1.1.1.1
$username=urlencode('youremail@gmail.com');
$password="yourpassword";
$feedurl=urlencode('thefeedurl'); //http://feeds.askapache.com/apache/htaccess
 
// create cookie file
$google_cookie=tempnam("./","XX");
 
$url="http://www.google.com/reader/directory/search?q=$feedurl&ck=1199813768546&client=scroll";
$postdata="Email=$username&Passwd=$password&GA3T=5AS_gBsvDHI&nui=15&".
"fpui=3&service=reader&ifr=true&askapache=lovesgoogle&rm=hide&itmpl=true&hl=en&alwf=true&continue=".
$url."&null=Sign in";
 
$ch = curl_init("https://www.google.com/accounts/ServiceLoginBoxAuth");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $google_cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $google_cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$askapache_curl_google_result = curl_exec ($ch);
curl_close($ch);
$s=array('@<noscript[^>]*?>.*?</noscript>@si','@<script[^>]*?>.*?</script>@si','@<style[^>]*?>.*?</style>@siU','@<![\s\S]*?--[ \t\n\r]*>@');
$g=preg_replace($s, '', $askapache_curl_google_result);
$g=preg_match('@href="([^"]*?)"@si',$g,$m);
 
$ch = curl_init($m[1]);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $google_cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $google_cookie);
$askapache_curl_google_result = curl_exec ($ch);
curl_close($ch);
$g=preg_match('@href="([^"]*?)"@si',$askapache_curl_google_result,$j);
 
$ch = curl_init("http://www.google.com/reader/directory/search?q=$feedurl");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $google_cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $google_cookie);
$askapache_curl_google_result = curl_exec ($ch);
curl_close($ch);
$y=preg_match('@class="feed-result-stats"><span class="number">([^<]*?)<\/span>@si',$askapache_curl_google_result,$s);
 
// output results
header("Content-type: text/plain");
echo "(".$s[1].") Subscribers for feed: ".urldecode($feedurl);
 
// delete cookie file
unlink($google_cookie);
exit 0;
exit;
/*
"Google AdSense Automatic Login with PHP and CURL"
http://www.askapache.com/webmaster/login-to-google-adsense-using-php.html
 
"Follow your Adsense earnings with an RSS reader"
http://curl.askapache.com/libcurl/php/examples/rss-adsense.html 

"Auto-Login to Google Analytics to impress Clients"
http://www.askapache.com/webmaster/login-google-analytics.html
*/
?>

About Google Reader

Google Reader TourGoogle also owns FeedBurner, which is one of my favorite products and is also free.

Thanks Google, You Rock!

How many people are subscribed to my feed?

Google Reader reports subscriber counts when we crawl feeds (within the “User-Agent:” header in HTTP). Currently, these counts include users of both Google Reader and iGoogle, and over time will include subscriptions from other Google properties.

The “User-Agent:” header of our crawler includes the name of our crawler (“FeedFetcher-Google”) along with its associated URL, the subscriber count, and a unique 64-bit feed identifier (“feed-id”). You might see multiple requests for the same feed with distinct “feed-id” values. This happens if the same feed is referenced through multiple URLs (for example, because of redirects). In that case, you’ll need to sum up the subscribers to a feed that have distinct “feed-id” values to determine the total number of Google subscribers to the feed.

Below is an example of the contents of the “User-Agent:” header:

User-Agent: Feedfetcher-Google; (+http://www.google.com/feedfetcher.html; 4 subscribers; feed-id=1794595805790851116)

CURL PHP Links

  1. Using libcurl with PHP
  2. PHP cURL examples
  3. Google AdSense Automatic Login with PHP and CURL
  4. Follow your Adsense earnings with an RSS reader
  5. Auto-Login to Google Analytics to impress Clients
  6. Daniel Stenberg Blog on libcurl and curl

«
»

Leave your own comment

Reader Comments

  1. nurah ~

    Thank you

  2. Andre Nunes da Silva ~

    Hello! Good Morning…

    I would like to use your script to read the RSS Google Groups, but I am not able. You can help me?

    Thank you

Go for it!


It's very simple - you read the protocol and write the code. -Bill Joy

HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

Site Map | Contact Webmaster | Glossary | License and Disclaimer | Terms of Service

↑ TOP
Main