Archive

Archive for the ‘PHP’ Category

Querying Google Contacts API via PHP

March 23rd, 2011

For the last few days I have been going through Google API documentations for one of the projects I have been working on to get Google Contact details of a Google user. To the developer’s luck, the documentation about Google API is very well prepared but due to the fact that authentication with oAuth is a painful process, things get scary while accessing Google API. (Even requesting access is a nightmare :)

I have handled the authentication by Zend Framework oAuth component which made things pretty easy. But it took me almost 2 days to figure out how to fetch contact details of the user via Google Contacts API with the access token that I receive. oAuth playground is a nice utility to compare the values you create and should be created. It was the time that I realized that access token was url encoded twice while generating base string. I do not know why, but you can check the playground and the slash character is encoded two times. First encoding translates / to %2F, and second one translates %2F to %252F.

Anyway, here is a sample code in PHP to make things easier for anyone interested. Good luck!

$timestamp = time();
 
$contactsURL = "https://www.google.com/m8/feeds/contacts/default/full/";
$oauth_nonce = md5(uniqid(rand(), true));
$sign_method = 'HMAC-SHA1';
$oauth_version = '1.0';
 
$oauth_consumer_key = 'aaaaaaaaaaa'; // the key you receive from Google
$oauth_consumer_secret = 'bbbbbbbbbbbb';  // the secret you receive from Google
$oauth_token = 'cccccccccccccccc'; // the token you receive at the end of authentication
$oauth_token_secret = 'ddddddddddddd';  // the token secret you receive at the end of authentication
 
// be careful that access token is urlencoded here, 
// and going to be urlencoded once more while creating base_string
 
$baseParams = array("oauth_consumer_key" => $oauth_consumer_key,
                    "oauth_nonce" => $oauth_nonce,
                    "oauth_signature_method" => $sign_method,
                    "oauth_timestamp" => $timestamp,
                    "oauth_token" => urlencode($oauth_token),
                    "oauth_version" => $oauth_version
                    );
 
$baseArr = array();
foreach ($baseParams as $key => $value)
    $baseArr[] = $key . "=" . $value;
 
$base_string = "GET&".urlencode($contactsURL)."&".urlencode(implode("&", $baseArr));
 
$key = urlencode($oauth_consumer_secret) . '&' . urlencode($oauth_token_secret);
$sig = base64_encode(hash_hmac('sha1', $base_string, $key, true));
 
$queryParams = array("oauth_version" => $oauth_version,
                        "oauth_nonce" => $oauth_nonce,
                        "oauth_timestamp" => $timestamp,
                        "oauth_consumer_key" => $oauth_consumer_key,
                        "oauth_token" => $oauth_token,
                        "oauth_signature_method" => $sign_method,
                        "oauth_signature" =>$sig
                    );
 
// http_build_query url encodes every parameter and creates well-formed query string
 
$queryURL = $contactsURL."?".http_build_query($queryParams);
 
echo file_get_contents($queryURL);

PHP, Programming , , , ,

Exploding by multiple delimiter values in PHP

January 26th, 2011

There are 2 ways to explode a string into array by multiple delimiter values. First method includes an intermediate step to convert all delimiters to a uniform delimiter value, then exploding by it.

$arrDelimiters = array("&", "or", "and");
$inputText = "a or b & c and d";
 
$uniformText = str_replace($arrDelimiters, "-|-", $inputText);
$arrValues = explode("-|-", $uniformText);
 
print_r($arrValues);

prints out

Array
(
    [0] => a 
    [1] =>  b 
    [2] =>  c 
    [3] =>  d
)

Second method is faster than the first as it uses preg_split to split the text into pieces.

print_r(preg_split("/and|or|&/", "a or b & c and d"));

prints out

Array
(
    [0] => a 
    [1] =>  b 
    [2] =>  c 
    [3] =>  d
)

PHP, Programming , ,

Flix Cloud and S3 makes video sharing really easy

March 22nd, 2010

Being quite busy with implementing a video sharing feature on Sheet Music Trade, I have found that On2 had done a good job with Flix Cloud. Its REST based API makes video processing a real simple job and with Amazon S3 storage and Flow Player it seems that anyone with some level of programming can create a video sharing community nowadays. The days of struggling with FFMpeg (and php-ffmpeg for image extraction) are not very far away (still using this setup for one of the largest video sharing communities in Turkey), I feel that I won’t think about using Flix Cloud twice from now on.

Flix Cloud PHP is a great PHP sample library for encoding videos on Flix Cloud and Amazon S3 PHP Class is also a great source for managing files programmatically on S3.

Internet, PHP, Programming , , , , ,