Wednesday, May 4, 2011

Google Blogger OAuth API

After getting Oauth token and secret from Google for blogger
Check out this "playground" http://googlecodesamples.com/oauth_playground/index.php
it's so hard to even find the details of how to register your app with Google, what
url to use to request Oauth Token for blogger API.

When I find it again (takes a lot of wasted time to just find the correct url for documentation as Google scattered the blogger API documentation all over their sites - some in Oauth data related pages, some on Blogger API related pages...)

Some piece meal bits of data for Google OAuth API developers:



You will need to register your domain with Google. This included downloading some lame html file from Google then uploading it to root dir of your site and then clicking "verify" button, basically proving to Google that you are the admin of that domain.

Once that is done you need to go here https://www.google.com/accounts/UpdateDomain
and enter some string that you must create yourself - some type of long secret string, into the
"Target URL path prefix"
That will become your app's OAuth Consumer Key and Google will generate the OAuth Consumer Secret value for you. This is the lamest and most unintuitive way I"ve seen in any APIs to get your OAuth Consumer API key and OAuth consumer secret. But that's how it is with Google, it's because they don't really care about the developers(about schmucks like you)

Now you will probably be able to use OAuth protocol, but of cause nothing is "easy" when it comes to Google's implementation of OAuth - remember this kids - Google does not care about developers and does not want to provide clear and easy-to-understand or even easy-to-find documentation.



Some details are here
http://code.google.com/apis/blogger/docs/2.0/developers_guide_protocol.html

can use this url to get list of user's blogs: https://www.blogger.com/feeds/default/blogs

It will be in the superbly fucked-up format written by incompetent fools at Google.
Even though it's in XML format, there is no entry for "blogID" and you must extract it from the tag using regex.

The url for the POST - to post your entry to blogger using api is also not easy to extract. It's buried in the
Why the fuck the type="html"? This is an XML file - supposed to be pure data, why html?
That's not even the worst part.

To extra the url for post you need to find an entry in the "summary" tag that looks like this:


There is no easy way to extract this entry because there are a bunch of other very similar entries. Your only clue to use is that the "rel" attribute must end with #post How do you extract this entry? Is there a way to use XPATH when the only condition you have is that value of an attribute ends with #post?
Maybe if you can be certain that value will be "http://schemas.google.com/g/2005#post" then yes, it's possible to find this entry easily and then use the value of href

But it's probably easier to just extract and store the blog-id by extracting it out of the tag.



Monday, April 11, 2011

getElementsByClassName in PHP DOMDocument

function getElementsByClassName(\DOMDocument $DOMDocument, $ClassName) {
$Elements = $DOMDocument->getElementsByTagName("*");
$Matched = array();

for($i=0;$i<$Elements->length;$i++) {

if($Elements->item($i)->attributes->getNamedItem('class')->nodeValue == $ClassName) {
$Matched[]=$Elements->item($i);
}
}
return $Matched;
}

Monday, January 24, 2011

Nice site for Android developers

An awesome site for Android developers and Android enthusiasts has been launched recently.

It's straight to the point - Questions and Answers, mailing lists and Videos, all about Android phones, tips and tricks and development of Android apps.

If you are interested in learning or trying to develop your Android app, you've got to join it. Here is the link: http://www.androidportal.info

You can use existing Twitter or Facebook or Google account to join.

Monday, November 22, 2010

Борис Гребенщиков

Reminder, good songs:
Psi (Пси) - Аквариум (CD)
Сварог

/////////////////

Bardo - Русско-Абиссинский Оркестр (CD)

/////////////////

Прибежище - БГ, Gabroelle Roth & The Mirrors (CD)

Example of using fastcgi_finish_request() in php 5.3

Warning: this technique will work only when php is run as fastcgi and controlled by php-fpm

http://php-fpm.org/


fastcgi_finish_request() is extremely powerful optimization trick. It's even more powerful if you use it together with closure functions in php 5.3
This is how I started using it now:
suppose I have a method somewhere in the middle of my script that has to call some API, let's say Facebook API, download recent user data and if something changed in user's Facebook profile, then update my database.

Since facebook API response time is unpredictable, I don't want to hold up the page load waiting, so I want to defer this call till after the page is rendered.

But how do I do this if the Facebook api check is somewhere in the middle of my script and the page has not finished rendering yet?

The way this can be done is with closure.

Pass anonymous function as callback to register_shutdown_function() like this:

This script snipped is somewhere inside a class method which is called about half way through the script:
Note: the d() and e() functions are for logging debug and error messages,
you can ignore these...


$oAuthFB = new clsExternalAuthFb($oGlobal, $sAppId, $aCookieParams);
register_shutdown_function(function() use ($oAuthFB){

try{
d('before facebook auth post precessing $oAuthFB: '.$oAuthFB);
$oAuthFB->getFbData()->getFacebookUserObject();
d('after facebook auth post precessing');
} catch (Exception $e){
e('Unable to run post processing of FB data: '.$e->getFile().' '.$e->getLine().' '.$e->getMessage());
}
});


See what's going on here? I am basically cutting out the piece of code and sticking it to be executing all the way at the end of the script, after the php is done parsing everything else.

The only thing left to do is to add

fastcgi_finish_request();
somewhere after the output is sent to browser.

For example, in my index.php I have

$oController = new Controller();
echo $oController->getHtml();
fastcgi_finish_request();


That would ensure that all registered shutdown functions are run after the output is sent.

Because of the closure, the context, the object $oAuthFB is remembered it the state it was just before the callback anonymous function was created, all instance variables are in place, so the shutdown function is executed with the complete context of $oAuthFB object in the same state it was back in the middle of that method where I registered the shutdown callback.

This is extremely powerful way to basically cut out the code from the middle of you script and defer its execution till after the page has been rendered.

Tuesday, November 16, 2010

To update classes to make use of php 5.3 static magic

Something for my own user only, some changes to make to program
Even better have clsEmailRecord::factory($oRegistry)->get($email)
Then we can also create new record this way! it should extend clsMongoDocument

Just extend static class MongoDocument then use get_calling_class()
and call_static() magic
then it will be as easy as just
creating blank classes than extend clsMongoDocument
Heck, we can even make it serializable, just
use clsRegistry::getInstance() in unserialize and we good to go

clsMongoDocument::getByEmail($email)
clsMongoEmail::getByEmail($oMongo, $email)

clsMongoEmail::create($oMongo, $aData);

clsMongoEmail::factory($oMongo, $aData); the normal object

Monday, October 18, 2010

How to setup Apache with php on home Windows PC

If you want to install Apache server and php on Windows for your php development, then the best thing to do is to install WAMP.


It has own installer, so its very easy to download/install

Don't install Apache from official Apache website, MAMP is much easier and it installs php, apache and mysql all at once and has a nice little control panel where you can start and stop the server.

You can still install extra php pecl modules later by just copying the .dll files to appropriate dir, I'll show you how to do it later.