Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

1.06.2012

Yii Caching example

In order to use caching in yii framework you need define in main.php file and choose type of caching. Here are some examples.

Normal data caching
  1. // data cache
  2. public function actionCache()
  3. {
  4.     $cache = Yii::app()->cache;
  5.     //get cache
  6.     $time = $cache->get('cache_key');
  7.     //if cache is gone.
  8.     if (false === $time){  
  9.       //set cache
  10.       $cache->set('cache_key',time(),30);
  11.       $time = $cache->get('cache_key');
  12.     }
  13.     echo $time;
  14. }


Fregment data caching
  1. // fregment data cache
  2. public function actionCache()
  3. {
  4.   if ($this->beginCache('cache_key',
  5.   array('duration'=>60,
  6.         'dependency'=>array(
  7.       'class'=>'system.caching.dependencies.CDbCacheDependency',//you must have db set here
  8.       'sql'=>'SELECT COUNT(*) FROM  `user`',//you must have db set here
  9.   ),
  10.   'varyByParam'=>array('id'),//cache by $_GET['id']
  11.   ))){
  12.       echo 'cache_key'.time();
  13.       echo @$_GET['id'];
  14.       echo ' <hr /> ';
  15.       $this->endCache();
  16.   }
  17. }


Page Cache
  1. public function filters()
  2. {
  3.     return array(
  4.         array('COutputCache + PageCache', 'duration'=>30,'varyByParam'=>array('id')),
  5.     );
  6. }
  7. public function actionCache()
  8. {
  9.     $this->render('pageCache');
  10. }

It's easy to use, just to be a bit change. More about yii caching visit this: http://www.yiiframework.com/doc/guide/1.1/en/caching.overview

12.23.2011

Posting to Blogger using PHP Zend Framework

Posting to Blogger using PHP Zend Framework

Here is a simple code for posting blog to blogger via PHP Zend Framework

  1. <?php
  2. require_once 'Zend/Loader.php';
  3. Zend_Loader::loadClass('Zend_Gdata');
  4. Zend_Loader::loadClass('Zend_Gdata_Query');
  5. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  6. $user = 'name@example.com';
  7. $pass = 'password';
  8. $service = 'blogger';
  9. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service, null,
  10.         Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null,
  11.         Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');
  12.        
  13. $gdClient = new Zend_Gdata($client);
  14. $blogID = 'your blog id'; // like 98744313216546562
  15. $title='Hello, world!',
  16. $content='I am blogging on the internet.'
  17. $uri = 'http://www.blogger.com/feeds/' . $blogID . '/posts/default';
  18. $entry = $gdClient->newEntry();
  19. $entry->title = $gdClient->newTitle($title);
  20. $entry->content = $gdClient->newContent($content);
  21. $entry->content->setType('text');
  22. $createdPost = $gdClient->insertEntry($entry, $uri);
  23. $idText = split('-', $createdPost->id->text);
  24. $newPostID = $idText[2];
  25. echo $newPostID;
  26. ?>

Publishing a blog post using Blogger Data API

Publishing a blog post using Blogger Data API

  1. <?php
  2. $email = "username@gmail.com";
  3. $pass = "password for blogspot you use";
  4. $blogID= urlencode("your blog id"); // like 6304924319904337556
  5. // Do Not Modify Below Code
  6. if(!isset($_SESSION['sessionToken'])) {
  7. $ch = curl_init("https://www.google.com/accounts/ClientLogin?Email=$email&Passwd=$pass&service=blogger&accountType=GOOGLE");
  8. curl_setopt($ch, CURLOPT_POST,1);
  9. curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
  10. curl_setopt($ch, CURLOPT_HEADER,0);
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
  12. //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  13. $result = curl_exec($ch);
  14. $resultArray = curl_getinfo($ch);
  15. curl_close($ch);
  16. $arr = explode("=",$result);
  17. $token = $arr[3];
  18. $_SESSION['sessionToken'] = $token;
  19. }
  20. $entry = "<entry xmlns='http://www.w3.org/2008/Atom'><title type='text'>My </title><content type='xhtml'> This is my testing contnet </content></entry>";
  21. $len = strlen($entry);
  22. $headers = array("Content-type: application/atom+xml","Content-Length: {$len}","Authorization: GoogleLogin auth={$_SESSION['sessionToken']}","$entry");
  23. $ch = curl_init();
  24. curl_setopt($ch, CURLOPT_URL, "https://www.blogger.com/feeds/$blogID/posts/default");
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  26. curl_setopt($ch, CURLOPT_TIMEOUT, 4);
  27. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  28. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  29. curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
  30. curl_setopt($ch, CURLOPT_POST, true);
  31. //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  32. $result = curl_exec($ch);
  33. $ERROR_CODE = curl_getinfo($ch);
  34. curl_close($ch);
  35. echo '<pre>';
  36. print_r($headers);
  37. var_dump($result);
  38. print_r($ERROR_CODE);
  39. ?>