juust ~ php oddities

Unordered list of one element
  • rss
  • begin
  • about
    • vcard
    • WTF is BroJesus
  • php scripts
    • flickr wp widget
    • google multi key serp tool, php script
    • gwt plugin
  • php classes
    • php pagerank class
    • fibonacci class
    • robots.txt parser php class
  • serp
    • serp dashboard wordpress plugin
  • services

bing

juust | 21/08/2009

for completeness : php bing serp scraping :

  1. $query = 'serp';
  2. $page = 1;
  3. $start = ($page-1)*10;
  4. $url = 'http://www.bing.com/search?q='.urlencode($query)."&first=".($start+1);
  5.  
  6. $curl_handle = curl_init();
  7. curl_setopt($curl_handle,CURLOPT_URL, $url);
  8. curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
  9. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  10. $return = curl_exec($curl_handle);
  11. curl_close($curl_handle);
  12.  
  13. $parts = split('<h3>', $return);
  14.  
  15. for($j=1;$j<count ($parts);$j++)
  16. {
  17.     $p=$parts[$j];
  18.     preg_match('#<a\s+.*?href=[\'"]([^\'"]+)[\'"]\s*(?:title=[\'"]([^\'"]+)[\'"])?.*?>((?:(?!).)*)#i', $p, $urls);
  19.     echo "position: ".($start +$j)." url: ".$urls[1]." title: ".$urls[3].'<br />';
  20. }
  21. </count></h3>
Comments
1 Comment »
Categories
php, serp
Tags
php, serp
Comments rss Comments rss
Trackback Trackback

wordpress : fun with pluggable classes

juust | 31/07/2009

I was checking some idea i had about writing a small user class with an option to ‘plug in’ functions for wordpress.

This page covers most of it :
dynamically add functions to php classes @ www.gen-x-design.com. The class construct at the end of the comment thread, Martin Pietschmann’s contribution, is rather useful. This pattern revolves around importing functionality from pluggable classes and exposing it through one object instance (the ‘decorator’ pattern mentioned is mostly used for writing extended classes, different objects with the same base data and functionality, ‘views’ sort of).

I can include the file with base, import and user class into function.php, and on making a user object have it read the directory and import functions modules (or load functionality conditional based on user role/authorization).

For this example I used the wordpress options table. I write a new functions class

  1. class UserBogusPlugin extends MI_Importable
  2. {
  3.  public function the_anchor() {
  4. //user_url and user_nicename are exposed through the user class
  5. //I plug the functions class into, i can use the $this reference
  6. //as if i am writing code in the user class
  7.                 if($this->user_url<>'') {
  8.                     return '<a href="'.$this->user_url.'">'.$this->user_nicename .'</a>, ' . $this->first_name;
  9.                 }
  10. //no url, no anchor….  
  11.   return $this->user_nicename;
  12.  }
  13. }

…store the added class name in the options table…

  1. //load $arr from options table
  2. $modules = get_option('usermodules');
  3. if($modules) $usermodules = json_decode($modules);
  4. //add module
  5. $usermodules[] = 'UserBogusPlugin';
  6. //store back in options
  7. add_option('usermodules', json_encode($usermodules));

…and load the plugin classes when instantiating the user object :

  1. class User extends MI_Base
  2. {
  3.  public function __construct($ID) {
  4.             $modules = get_option('usermodules');
  5.             if($modules) {
  6.                 $usermodules = json_decode($modules);
  7.                 foreach($usermodules as $module) {
  8.                   if(class_exists($module)) $this->import(new $module);
  9.                  //or..
  10.                  //include('plugclass/'.$module.'.class.php');
  11.                  //$this->import(new $module);
  12.                 }
  13.             }
  14.             $this->ID=$ID;
  15.  }
  16. }

In the wordpress template i can use the added functionality through the user instance :

  1.       $my_user = new User($user_id);
  2.       …
  3.       echo $my_user->the_anchor();

Fun with classes :)

add. 3-8 (qed) :
a function to load a directory with plugin files, i add a header /* plugin pluginfilename */ and check the files if there is a header.

  1.        public function getPlugins()
  2.         {
  3.             $plugdir = TEMPLATEPATH .'/plug';
  4.             if ($handle = opendir($plugdir)) {
  5.                 $retval = array();
  6.                 while (false !== ($file = readdir($handle))) {
  7.                     if (($file <> ".") && ($file <> "..")) {
  8.                         $fh = fopen($plugdir.'/'.$file, 'r');
  9.                         $contents = '';
  10.                           $contents .= fread($fh, 1024);
  11.                         fclose($fh);
  12.                         if(preg_match('/lugin/', $contents))
  13.                         {   //check for header : plugin, grab pluginname
  14.                             $a = strpos($contents, 'lugin');
  15.                             $a += 6;
  16.                             $b = strpos($contents, ' ', $a);
  17.                             $plugname = substr($contents, $a, $b-$a);
  18.                             $retval[] = array($plugdir.'/'.$file, $plugname);
  19.                 }  }  }
  20.                 closedir($handle);
  21.             }
  22.             return $retval;
  23.         }
  24.  
  25.  
  26.  public function __construct($id) {
  27. //get he array with plugin files
  28.             $usermodules = $this->getPlugins();
  29.             if($usermodules) {
  30.                 foreach($usermodules as $module) {
  31.  
  32.                   //use require to load and import to add the function
  33.  
  34.                    require_once($module[0]);
  35.                    $this->import(new $module[1]);    
  36.                 }
  37.             }
  38.             $this->ID=$id;  
  39.  }
Comments
No Comments »
Categories
php, wordpress
Tags
wordpress
Comments rss Comments rss
Trackback Trackback

ga api sample : get pageviews

juust | 13/05/2009

I was going to put that online : how to get the pageviews out of the google analytics api, using simplexml and php. Google use three namespaces in the output file which make it less easy accessible, so here’s a quick sample of how to get your sites pageviews out of it :

  1. //ids           = site identifier (from the site data feed)
  2. //metrics     = what i want to see
  3. //start-date
  4. //end-date
  5.  
  6. $feedUri = "https://www.google.com/analytics/feeds/data?ids=ga:10516419&metrics=ga:pageviews&start-date=2009-04-01&end-date=2009-05-01";    
  7.  
  8.  $curl = curl_init();
  9.  curl_setopt($curl, CURLOPT_URL, $feedUri);
  10.  curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
  11.  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  12.  
  13.        $headers[] = "Authorization: GoogleLogin auth=".$Authtoken;
  14.  
  15. //for authtoken : see previous post
  16.  curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  17.  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  18.  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  19.  curl_setopt($curl, CURLOPT_VERBOSE, 1);
  20.  
  21. //get the string containing the xml file
  22.  $gA = curl_exec($curl);

the feed has three namespaces (atom, opensearch and dxp/analytics), a simple way is accessing the ENTRY tags (from the Atom namespace), in that tag is one DXP: line and that has the answer to the question.

<dxp:metric confidenceInterval=’0.0′ name=’ga:pageviews’ type=’integer’ value=’755′/>

  1. //load the string into a simple xml object
  2.  $feed = simplexml_load_string($gA);
  3.  
  4. //take the atom namespace
  5.  $children =  $feed->children('http://www.w3.org/2005/Atom');
  6.  
  7. //take the entry tags
  8.  $parts = $children->entry;
  9.  foreach ($parts as $entry) {
  10.  
  11.         //from the entry tag,
  12.         //access the dxp namespace
  13.   $dxp = (object) $entry->children('http://schemas.google.com/analytics/2009');
  14.  
  15.         //METRIC contains the answer to the question
  16.         //grab from the tag METRIC the attribute VALUE
  17.                 echo   (string) $dxp->metric->attributes()->value;
  18.  
  19.         }

Important is using the (string) typecast, normally simplexml returns a simplexml object, when you force a string type, it gives the actual metric ga:pageview value attribute as number.

Comments
No Comments »
Categories
google, php
Tags
analytics, api, ga, google, namespaces, php, simplexml
Comments rss Comments rss
Trackback Trackback

« Previous Entries Next Entries »

Recent Posts

  • Pagerank sculpting session
  • wish you were here
  • interesting : seo panel
  • availability test
  • Mayday

click me!
rss
Comments rss
Blog Directory
Web Developement Blogs - BlogCatalog Blog Directory
Listed in LS Blogs the Blog Directory and Blog Search Engine
Blog Flux Directory
joopita.com free web directory and search engine
design by jide
sitemap
17238 confirmed spam kills