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

RpSequel : rfc sql crud with wordpress

juust | 23/10/2009

I set out to use MsAccess with xhr/ajax to maintain tables I added to my wordpress database, from my desktop. (Because I suck at html forms backends and consider them a waste of time.)

I used a similar technique ten years ago, setting up msaccess as reporting tool for SAP R/3 with RFC dll’s and ActiveX. That remained stable for eight years without maintenance. Hey, I might get lucky with Wordpress xml-rpc and xhr/ajax.

I called the example RpSequel.

adding sql rpc functions

To sync the data sets, I will duplicate a list with sequel operations from my desktop database as rfc call to my blog’s xmlrpc-endpoint. To handle that list, I plug a sql crud method into the xml-rpc method array in Wordpress :

  1. add_filter( 'xmlrpc_methods', 'rpsequel_methods' );
  2.  
  3. function rpsequel_methods( $methods ) {
  4.     $methods['rpsequel.rpsequelInsert'] = 'rpsequelInsert';
  5.     return $methods;
  6. }
  7.  
  8. function rpsequelInsert($args) {
  9. }

The basic INSERT method itself can be simple:

  1. function rpsequelInsert($args) {
  2.         global $wpdb;
  3.  
  4. //the first parameters
  5.         $blog_id = (int) $args[0];
  6.         $username = $args[1];
  7.         $password = $args[2];
  8.         $tablename      = $args[3];
  9.  
  10. //get the structs
  11.         $structs        = $args[4];
  12.  
  13. //pick the first
  14.         $struct = $structs[0];
  15.  
  16. //compose the mysql insert statement    
  17.         foreach($struct as $key => $value){
  18.             $SqlFields .= " `".$key . "`,";
  19.             $SqlValues .= " '".$value . "',";
  20.         }
  21.  
  22.         $SqlFields=substr($SqlFields, 0, strlen($SqlFields)-1);
  23.         $SqlValues=substr($SqlValues, 0, strlen($SqlValues)-1);
  24.    
  25.         $SqlStatement .= "INSERT INTO ".$wpdb->prefix.$tablename. " (". $SqlFields .  ") VALUES (".$SqlValues . ")";
  26.        
  27. //execute the query        
  28.         $wpdb->query($wpdb->prepare($SqlStatement));
  29.  
  30. //return the record id
  31.        return $wpdb->insert_id;
  32. }

That in itself is not very exciting, it stuffs records in the host’s database. Which is fine, however, I want the blog to respond to individual operations.

I can write rpc-functions for every single function I want the blog to perform, that means truckloads of rpc-functions, on both client and server end. I am incredibly lazy, so I ain’t gonna go there.

adding hooks to sql rpc functions

Lucky for me, Wordpress has hooks, hooks are cool.

Adding action hooks (before_insert and after_insert) to the crud method makes it more powerful. Two hooks are enough to separate the business logic of the desktop database from the blogs reporting logic.

  1. function rpsequelInsert($args) {
  2.         global $wpdb;
  3.  
  4. //the first parameters
  5.         $blog_id = (int) $args[0];
  6.         $username = $args[1];
  7.         $password = $args[2];
  8.         $tablename      = $args[3];
  9.  
  10. //get the structs
  11.         $structs        = $args[4];
  12.  
  13. //here is the first action hook,
  14. //it hands the tablename and the records over to any
  15. //function that 'listens' on the action hook
  16.  
  17. do_action('rpsequel_before_insert',  $tablename, $structs);
  18.  
  19. //the rest of the rather dull method
  20.  
  21.         $struct = $structs[0];
  22. //….
  23. //execute the query        
  24.         $wpdb->query($wpdb->prepare($SqlStatement));
  25.  
  26. //here is the second action hook,
  27. //it hands the table name with the new record id
  28. //to any function that 'listens' on the action hook
  29.  
  30. do_action('rpsequel_after_insert',  $tablename, $wpdb->insert_id);
  31.  
  32. //return the record id
  33.        return $wpdb->insert_id;
  34. }

Now it is more exciting.

  • I can send a list of records and a rfc insert-method to the blogs xmlrpc endpoint
  • the rfc crud-plugin can process the records one by one
  • Before and after each insert operation, the method triggers an action.
  • Before inserting, it exposes the record data.
  • After inserting, it exposes the record id.

And the last two, was exactly what I wanted.

adding functionality with plugins

Now I can add tiny plugins, that ‘listen’ on the action hooks in the rfc-methods. If there is an INSERT into the database, my plugins read which table it affects. They can perform actions, either before the insert, with the new record data, or after the insert, with the new record id.

  1. add_action ( 'rpsequel_before_insert', 'before_insert_logic', 10, 2);
  2. add_action ( 'rpsequel_after_insert', 'after_insert_logic', 10, 2);
  3.  
  4. function before_insert_logic($rpc_tablename, $rpc_array) {
  5.    if($rpc_tablename=="ships") {
  6. //do some stuff before inserting incoming records
  7.    }
  8. }
  9.  
  10. function after_insert_logic($rpc_tablename, $rpc_insert_id) {
  11.    if($rpc_tablename=="ships") {
  12. //do some stuff after inserting incoming records
  13.    }
  14. }

That’s basically all it takes. As technique, it has it’s limitations, but it can come in handy sometimes.

         
Comments
1 Comment »
Categories
php, wordpress, xml-rpc
Tags
xml-rpc
Comments rss Comments rss
Trackback Trackback

bing api with php and simplexml

juust | 17/09/2009

About scraping results off of Bing : Bing use a set of about eight cookies. You can grab 200 results with php curl, as 20 pages of 10, but after the first 200 the Bing server checks for the cookie and for lack of one returns a blank page. I can fidget with the curl cookiejar, but Bing also offer a straighforward API.

Using the Bing API to list search results is easier.

Bing TOS : not for seo rank checks

In the last paragraph of the api guide, Bing give a quick recap of their TOS, you can do max 7 queries per second, and using the results for SEO rank checks is explicitly prohibited.

These following snippets (text source) are hence explicitly not to be used for bing search engine result page (’serp’) rank checks.

bing api with simplexml

So here is one for web results using php simplexml. The web api (which uses namespaces) allows for retrieving max 1000 results per term at max 50 results per query, you can specify the number of results and the offset, where to start grabbing results.

  1. $Appid="A_VERY_LONG_STRING";
  2. $Query = "seo rank check";
  3. $Numres = 50; //max 50
  4. $Offset = 1;    //up to 1000
  5.  
  6. $url = 'http://api.search.live.net/xml.aspx?
  7. Appid='.$Appid.'
  8. &query='.$Query.'
  9. &sources=web
  10. &web.count='.$Numres.'
  11. &web.offset='.$Offset;
  12.  
  13. $feed = simplexml_load_file($url);
  14. //use the web: namespace
  15.  $children =  $feed->children('http://schemas.microsoft.com/LiveSearch/2008/04/XML/web');
  16.       foreach ($children->Web->Results->WebResult as $d) {
  17.                 echo $d->Title.'<br />';
  18.                 echo $d->Description.'<br />';
  19.                 echo $d->Url.'<br />';
  20.                 echo $d->DisplayUrl.'<br />';
  21.    }

..and one for the pictures using php simplexml :

  1. $Appid="A_VERY_LONG_STRING";
  2. $Query = "alkmaar";
  3. $Numres = 10;
  4. $Offset = 1;
  5.  
  6. $url = 'http://api.search.live.net/xml.aspx?';
  7. $url .= 'Appid='.$Appid;
  8. $url .= '&query='.$Query;
  9. $url .= '&sources=image';
  10. $url .= '&image.count='.$Numres;
  11. $url .= '&image.offset='.$Offset;
  12.  
  13. $feed = simplexml_load_file($url);
  14.  
  15. //use the mms: namespace      
  16.   $children =  $feed->children('http://schemas.microsoft.com/LiveSearch/2008/04/XML/multimedia');
  17.  
  18.     echo('<ul ID="resultList">');
  19.  
  20.     foreach ($children->Image->Results->ImageResult as $d) {
  21.                 echo('<li class="resultlistitem"><a href="' . $d->DisplayUrl . '">' . $d->Title . '</a><br />');
  22.                 echo('<img src="' . $d-/>Thumbnail->Url. '" /><br />
  23.                      '.$d->Thumbnail->ContentType.'<br />
  24.                     '.$d->Thumbnail->Height.'<br />
  25.                     '.$d->Thumbnail->Width.'<br />
  26.                     '.$d->Thumbnail->FileSize.'<br />
  27.                     </li>');
  28.        }
  29.     echo("</ul>");

I actually like that api, I am going to use that.

bing api with json

Bing seem to prefer you use json, less bandwidth usage. After their example in the api basics guide :

  1.  
  2. $Numres = 10;
  3. $Offset = 1;
  4. $Query='alkmaar';
  5.  
  6. $url = 'http://api.search.live.net/json.aspx?';
  7. $url .= 'Appid='.$Appid;
  8. $url .= '&query='.$Query;
  9. $url .= '&sources=image';
  10. $url .= '&image.count='.$Numres;
  11. $url .= '&image.offset='.$Offset;
  12.  
  13.  
  14. $response = file_get_contents($url);
  15. $jsonobj = json_decode($response);
  16. echo('<ul ID="resultList">');
  17. foreach($jsonobj->SearchResponse->Image->Results as $value)
  18. {
  19.     echo('<li class="resultlistitem"><a href="' . $value->Url . '">');
  20.     echo('<img src="' . $value-/>Thumbnail->Url. '"></a></li>');
  21. }
  22. echo("</ul>");

Of course there is the old RSS-option, which doesnt require an appid but also falls under the api 2.0 tos, and a soap option.

other sources :
There is a bing api php class made over at routecafe, and a jquery bing plugin using json over at Einar Otto Stangvik’s blog.

         
Comments
1 Comment »
Categories
php, seo
Tags
bing, php, seo
Comments rss Comments rss
Trackback Trackback

replace_ereg

juust | 15/09/2009

Posix ereg which will not be included in PHP6 core, you can optionally include it in the install, otherwise you’d use pcre preg_match. In Wordpress, ereg is still used in the squirrelmail POP3 class (trunk), afaik wordpress as it is distributed up till now will fail if it tries to load the POP3-class with a standard PHP 6 install.

I don’t so much have a problem with that, there is not even a release date set for PHP6.

I was pondering more in general on backward compatibility and legacy function support, ereg serves as a nice example.

Does it make sense to make a plugin with general php patches ? A central plugin would make it easier for some coders (me of course) to check what functions are dropped, and update code without code breaks caused by updates of php or wordpress core.

So I don’t have to reinvent the wheel. Something simple like :

  1. if(!function_exist('ereg_replace') {
  2.     function ereg_replace($pattern, $replace, $source) {
  3.        return preg_replace("/". $pattern."/", $replace, $source);  
  4.     }
  5. }

Anyways, I’ll have to read about it some more I think.

         
Comments
No Comments »
Categories
php
Comments rss Comments rss
Trackback Trackback

« Previous Entries

Recent Posts

  • p2p with wordpress xml-rpc
  • Tweets on Google’s frontpage
  • happy new year
  • metaWeblog.newPost posting to Wordpress from Word
  • IE is retarded

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
8096 confirmed spam kills