<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Konstantin Kovshenin &#187; php</title>
	<atom:link href="http://kovshenin.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://kovshenin.com</link>
	<description>WordPress, Automattic and Open Source</description>
	<lastBuildDate>Mon, 21 May 2012 15:59:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>How To: Show Off Your Social Counters in WordPress</title>
		<link>http://kovshenin.com/2011/how-to-show-off-your-social-counters-in-wordpress/</link>
		<comments>http://kovshenin.com/2011/how-to-show-off-your-social-counters-in-wordpress/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 07:21:04 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[social media]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[feedburner]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=3191</guid>
		<description><![CDATA[You know it&#8217;s all about social these days, and this post is about showing off how social you are on your WordPress website. I&#8217;m sure you&#8217;re using tonnes of social networks and all of them have friends, followers, fans and subscribers metrics, but let&#8217;s start from simple: Twitter, Facebook and RSS. We&#8217;ll write code in [...]]]></description>
			<content:encoded><![CDATA[<p>You know it&#8217;s all about social these days, and this post is about showing off how social you are on your WordPress website. I&#8217;m sure you&#8217;re using tonnes of social networks and all of them have friends, followers, fans and subscribers metrics, but let&#8217;s start from simple: Twitter, Facebook and RSS.</p>
<p>We&#8217;ll write code in this post, and you may ask me why. Of course there are loads of copy-paste javascript widgets available, but the first problem I encountered with those is they&#8217;re all different! What we&#8217;re going to do here is grab the numbers on the server, so then you&#8217;ll be able to display them however you like, with your own icons and captions.</p>
<p>I wrote a couple of posts on this topic last year:</p>
<ul>
<li><a href="http://kovshenin.com/2010/counting-facebook-fans-in-php-the-graph-api-way/">How To Count Facebook Fans in PHP using the Graph API</a></li>
<li><a href="http://kovshenin.com/2010/facebook-fans-count-using-python-and-the-graph-api/">Facebook Fans Count Using Python and the Graph API</a></li>
<li>And an older one <a href="http://kovshenin.com/2010/how-to-count-facebook-fans-in-php/">How to Count Facebook Fans in PHP</a> using an FQL query</li>
</ul>
<p>We&#8217;ll be using some of those snippets in our code today, together with a few additions for Twitter followers count and RSS subscribers too. We will also adapt these to WordPress. Okay, enough talk, let&#8217;s get to the coding!</p>
<p><img class="size-full wp-image-3208 alignnone" src="http://kovshenin.com/files/2011/02/social-metrics.png" alt="" width="628" height="196" /><br />
Icons Credit: <a href="http://icontexto.blogspot.com/2009/02/drink-web-20.html">Drink Web 2.0</a> by Bruno Maia</p>
<h2>Twitter Followers Count</h2>
<p>This one&#8217;s quite simple, since the Twitter API allows a non-authenticated call to public users. Assuming we&#8217;ve got JSON functions available (PHP5 or JSON Services in PHP4):</p>
<pre>public function get_twitter_followers_count( $screen_name ) {
	if ( false === ( $followers = get_transient( 'twitter-followers-count' ) ) ) {
		$response = wp_remote_get( "http://api.twitter.com/1/users/show.json?screen_name={$screen_name}" );

		if ( is_wp_error( $response ) ) {
			$followers = 91;
		} else {
			$json = json_decode( wp_remote_retrieve_body( $response ) );
			$followers = $json-&gt;followers_count;
		}
		set_transient( 'twitter-followers-count', $followers, 60*60*24 );
	}
	return $followers;
}</pre>
<p>As you can see I&#8217;m using the <a href="http://codex.wordpress.org/Transients_API">Transients API</a> to cache the followers count for 24 hours, this reduced the load on the hosting server and serves pages faster. I&#8217;ll be using transients for Facebook fans and RSS subscribers too.</p>
<p>Line 5 might seem a little strange, we&#8217;re explicitly setting followers to 91. We do that when there&#8217;s an error with the API call so that we don&#8217;t show off a zero-count. You can go forward and extend it to double-cache values with a <code>get_option</code> and <code>set_option</code> call so that 91 is actually a figure retrieved from Twitter API and not hard-coded. But that&#8217;s extra, I&#8217;m fine with 91 ;)</p>
<p>The rest seems straightforward &#8212; we parse the response using <code>json_decode</code> and grab the followers count. If you&#8217;re into grabbing something else, go ahead and inspect the <code>$json</code> object using <code>print_r</code>, there&#8217;s so much interesting stuff there ;)</p>
<p>When the followers count is in our hands, we set a transient for it, so that next time it is served from cache, and finally return the count. Simple as that ;) The usage is easy &#8212; just pass a screen name to the function and you&#8217;ll get the results, as long as it is not a protected profile.</p>
<h2>RSS Subscribers Count</h2>
<p>As I already mentioned, transients will be used here as well, same method. Additionally we&#8217;ll be using Feedburner&#8217;s <a href="http://code.google.com/apis/feedburner/awareness_api.html">Awareness API</a>. In order for that to work you&#8217;ll have to have your RSS feed setup with Feedburner and the Awareness API activated. Take a look at the &#8220;AwAPI Activation and the FeedCount Chicklet&#8221; section following the link above.</p>
<pre>public function get_rss_subscribers_count($feed_url) {
	if (false === ($subscribers = get_transient('rss-subscribers-count'))) {
		$feed_url = urlencode($feed_url);
		$response = wp_remote_get("http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri={$feed_url}&amp;dates=" . date('Y-m-d', strtotime('-2 days', time())));

		if (is_wp_error($response)) {
			$subscribers = 97;
		} else {
			$body = wp_remote_retrieve_body($response);
			$xml = new SimpleXMLElement($body);

			$status = $xml-&gt;attributes();

			if ($status == 'ok') {
				$subscribers = $xml-&gt;feed-&gt;entry-&gt;attributes()-&gt;circulation;
			} else {
				$subscribers = 98;
			}
		}

		$subscribers = (int) $subscribers;
		set_transient('rss-subscribers-count', $subscribers, 60*60*24);
	}
	return $subscribers;
}</pre>
<p>As you can see, we&#8217;re fetching the RSS feed data using the feed URL via an HTTP request to the Feedburner API. The Awareness API returns an XML object, which is then parsed using the <a href="http://php.net/manual/en/book.simplexml.php">Simple XML</a> methods. The <code>circulation</code> attribute is the one we&#8217;re looking for &#8212; it indicates the approximate subscribers count.</p>
<p>Again, I&#8217;ve hard-coded the 98 figure there for fallback purposes, you might want to extend it. The date magic simply asks Feedburner for stats of two days ago, since it sometimes yields 0 for &#8220;today&#8221; and &#8220;yesterday&#8221;. Could be changed according to your Feedburner stats if that matters.</p>
<p>Finally, we&#8217;ve set the transient and returned the subscribers count, voila. Usage: pass on your Feedburner feed URL to <code>get_rss_subscribers_count()</code>, an integer is returned.</p>
<h2>Facebook Fans Count</h2>
<p>Last but definitely not least &#8212; Facebook. We&#8217;ll use the <a href="https://github.com/facebook/php-sdk/">Facebook PHP SDK</a>, I&#8217;m using version 2.1.2 which seems to be the latest stable at the time of writing. We&#8217;ll be using the Graph API way, so no more FQL queries please. Here&#8217;s the code:</p>
<pre>public function get_facebook_fans_count($page_name) {
	if (false === ($fans = get_transient('facebook-fan-count'))) {
		require_once("facebook/facebook.php");
		$facebook = new Facebook(array(
			'appId' =&gt; 'your-application-id',
			'secret' =&gt; 'your-application-secret'
		));

		$graph_obj = $facebook-&gt;api("/{$page_name}");
		$fans = $graph_obj['fan_count'];

		set_transient('facebook-fan-count', $fans, 60*60*24);
	}
	return $fans;
}</pre>
<p>Note, that unlike the old-fashioned way, the Graph API uses your application ID, not your API key. You can obtain these when registering a new application at the <a href="http://www.facebook.com/developers/">Facebook Developers</a> page. There&#8217;s a button that says &#8220;Set Up New App&#8221; which takes you through the rest of the process.</p>
<p>So once your app is in place, credentials are fine and the connection is good, we use facebook&#8217;s <code>api</code> method to request for a page name. Try &#8220;mashable&#8221; for example.</p>
<p>The returned Graph object contains quite a lot of stuff, including the number of fans. Use <code>print_r</code> on <code>$graph_obj</code> to learn more. So the fan count is obtained, a transient is saved, and the number is returned.</p>
<h2>Conclusion</h2>
<p>Note that if you&#8217;re going to use these for different profiles, you&#8217;ll have to change the names of your transients to include that data, otherwise there will only be one cache. I kept the code as simple as possible here, but I&#8217;ve illustrated the extra stuff in the final source that I did. I also wrapped these up in a single class called <code>$social</code>, I know it&#8217;s too generic for WordPress, but I&#8217;m not writing a plugin out of this (yet). The source could be downloaded <a href="http://kovshenin.com/core/wp-content/uploads/2011/02/socialcounters.php_.txt">right here</a>. Feel free to use and extend the code with your own methods for other social networks and services.</p>
<p>Okay well, thank you so much for reading and retweeting this, oh and by the way&#8230;</p>
<p><strong>Question</strong>: what other 3-5 services would you like to get your numbers from?</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2011/how-to-show-off-your-social-counters-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Happy 2011: In 10+ Different Programming Languages</title>
		<link>http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/</link>
		<comments>http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#comments</comments>
		<pubDate>Thu, 23 Dec 2010 13:44:02 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[go]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=2905</guid>
		<description><![CDATA[The end of the year 2010 is near so I&#8217;ve prepared this post for all you coders, developers and other geeks. You know when you build applications (especially web applications) you often leave a copyright notice on every page saying &#8220;Copyright 2010, All Rights Reserved&#8221; or whatever? Right, but what developers tend to forget is [...]]]></description>
			<content:encoded><![CDATA[<p>The end of the year 2010 is near so I&#8217;ve prepared this post for all you coders, developers and other geeks. You know when you build applications (especially web applications) you often leave a copyright notice on every page saying &#8220;Copyright 2010, All Rights Reserved&#8221; or whatever? Right, but what developers tend to forget is that time passes by and that they have to go back and change 2010 to 2011 in January &#8212; so we often browse websites, especially in January and February that have last year&#8217;s copyrights.</p>
<p>The best trick is not to hardcode the year inside your templates and skins, but to write the current year dynamically using date functions, so below is a list of printing out the current year in 10 different programming languages.</p>
<ul>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#python">Python</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#php">PHP</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#cpp">C/C++</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#js">JavaScript</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#perl">Perl</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#ruby">Ruby</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#java">Java</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#shell">Unix Shell</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#go">Go (golang)</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#asm">x86 Assembly</a> (no kidding)</li>
</ul>
<p>And here&#8217;s a list of ones contributed by commentators:</p>
<ul>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#lua">Lua</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#clojure">Clojure</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#objc">Objective-C</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#csharp">C# (C-Sharp)</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#tcl">Tcl</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#flex">Adobe Flex</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#delphi">Delphi</a></li>
<li><a href="http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/#haskell">Haskell</a></li>
</ul>
<p><span id="more-2905"></span></p>
<h2><a class="bookmark" name="python" href="#python"></a>Python</h2>
<pre>from datetime import date
print date.today().year</pre>
<h2><a class="bookmark" name="php" href="#php"></a>PHP</h2>
<p>Definitely one of the easiest ways.</p>
<pre>echo date("Y");</pre>
<h2><a class="bookmark" name="cpp" href="#cpp"></a>C and C++</h2>
<pre>#include &lt;stdio.h&gt;
#include &lt;time.h&gt;

int main() {
        time_t now = time(NULL);
        struct tm* local = localtime(&amp;now);
        printf("%d", local-&gt;tm_year + 1900);
        return 0;
}</pre>
<h2><a class="bookmark" name="js" href="#js"></a>JavaScript</h2>
<pre>document.write(new Date().getFullYear());</pre>
<h2><a class="bookmark" name="perl" href="#perl"></a>Perl</h2>
<pre>my $year = (localtime)[5];
print $year + 1900;</pre>
<h2><a class="bookmark" name="ruby" href="#ruby"></a>Ruby</h2>
<pre>puts Time.now.year</pre>
<h2><a class="bookmark" name="java" href="#java"></a>Java</h2>
<pre>import java.util.*;
import java.text.*;

public class Apollo {
	public static void main(String[] args) {
		Date date = new Date();
		SimpleDateFormat simpleDateformat=new SimpleDateFormat("yyyy");
		System.out.println(simpleDateformat.format(date));
	}
}</pre>
<h2><a class="bookmark" name="shell" href="#shell"></a>Unix Shell</h2>
<p>Seems to be the shortest one of all.</p>
<pre>date +%Y</pre>
<h2><a class="bookmark" name="go" href="#go"></a>Go (golang)</h2>
<p>Interesting fact that when I ran this code on the <a href="http://golang.org/">Go website</a> I ended up with 2009 &#8212; guess their servers are running a little slow?</p>
<pre>package main

import ("fmt"
	"time")

func main() {
	var t = time.LocalTime()
	fmt.Println(t.Year)
}</pre>
<h2><a class="bookmark" name="asm" href="#asm"></a>x86 Assembly</h2>
<p>Here&#8217;s a special one, donated by my brother @SoulSeekah.</p>
<pre>jmp start

year db "0000$"

start:
	mov ah, 04h
	int 1Ah

	mov bh, ch
	shr bh,4
	add bh,30h
	mov [year], bh
	mov bh,ch
	and bh,0fh
	add bh,30h
	mov [year+1],bh

	mov bh, cl
	shr bh,4
	add bh,30h
	mov [year+2], bh
	mov bh,cl
	and bh,0fh
	add bh,30h
	mov [year+3],bh

	mov ah,09h
	mov dx, offset year
	int 21h

	mov ah,4Ch
	mov al,00
	int 21h</pre>
<h2><a class="bookmark" name="lua" href="#lua"></a>Lua</h2>
<p>Code by Vesa Marttila (@ponzao).</p>
<pre>print(os.date("*t").year)</pre>
<h2><a class="bookmark" name="clojure" href="#clojure"></a>Clojure</h2>
<p>Once again thanks to Vesa Marttila (@ponzao).</p>
<pre>(let [date (java.util.Date.)
      sdf  (java.text.SimpleDateFormat. "yyyy")]
  (println (.format sdf date)))</pre>
<h2><a class="bookmark" name="objc" href="#objc"></a>Objective-C</h2>
<p>Donated by Lowell via the comments section.</p>
<pre>#import &lt;Foundation/Foundation.h&gt;

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSLog (@"%d", [[NSCalendarDate date] yearOfCommonEra]);

    [pool drain];
    return 0;
}</pre>
<h2><a class="bookmark" name="csharp" href="#csharp"></a>C# (C-Sharp)</h2>
<p>Contributed by a certain Paul via the comments.</p>
<pre>using System;

class Program {
    static void Main(string[] args) {
        Console.WriteLine(DateTime.Now.Year);
    }
}</pre>
<h2><a class="bookmark" name="tcl" href="#tcl"></a>Tcl</h2>
<p>Contributed by Robert.</p>
<pre>set year [clock format [clock seconds] -format {%Y}]
puts $year</pre>
<h2><a class="bookmark" name="flex" href="#flex"></a>Adobe Flex</h2>
<p>Seems to work with both Flex 3 and 4, contributed by <a href="http://www.reddit.com/user/Uber_Nick">Uber_Nick</a> via reddit.com comments.</p>
<pre>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"&gt;
    &lt;mx:Label text="{new Date().fullYear}" /&gt;
&lt;/mx:Application&gt;</pre>
<h2><a class="bookmark" name="delphi" href="#delphi"></a>Delphi</h2>
<p>Thanks to Rejn via comments and Reddit.</p>
<pre>ShowMessage( IntToStr(YearOf(Now)) );</pre>
<h2><a class="bookmark" name="haskell" href="#haskell"></a>Haskell</h2>
<p>Thanks to Pezezin via comments and Onmach via Reddit.</p>
<pre>import System.Time
main = getClockTime &gt;&gt;= toCalendarTime &gt;&gt;= print . ctYear</pre>
<p>Oh, and here&#8217;s a special one in <a href="http://www.reddit.com/r/programming/comments/es0l3/happy_2011_in_10_different_programming_languages/c1aioqh">LOLCODE</a> by Danita via Reddit.</p>
<p>That&#8217;s about it! Feel free to contribute to the list in any other programming language, preferably well-known and easy to test. Use <a href="http://pastebin.com/">pastebin.com</a> and leave links to your code in the comments section below. Have a Merry Christmas and a Happy New Year. Welcome to twenty-eleven! See you next year! Oh, and thank you all for tweeting this!</p>
<p>P.S. Christmas is on January 7th in Russia, so we&#8217;re not going to have holidays until the 31st.</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2010/happy-2011-in-10-different-programming-languages/feed/</wfw:commentRss>
		<slash:comments>65</slash:comments>
		</item>
		<item>
		<title>October Quickie: A Little Bit of Everything</title>
		<link>http://kovshenin.com/2010/october-quickie-a-little-bit-of-everything/</link>
		<comments>http://kovshenin.com/2010/october-quickie-a-little-bit-of-everything/#comments</comments>
		<pubDate>Wed, 13 Oct 2010 07:52:34 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[personal]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=2579</guid>
		<description><![CDATA[Well yeah, it started snowing today here in Moscow, but this post is not about snow. I remember I&#8217;ve done some quickie before, but couldn&#8217;t find it. Perhaps it was something I mentioned inside the blog post with a different title. But anyways, this is a short roundup of what I&#8217;ve been up to lately. [...]]]></description>
			<content:encoded><![CDATA[<p>Well yeah, it started snowing today here in Moscow, but this post is not about snow. I remember I&#8217;ve done some quickie before, but couldn&#8217;t find it. Perhaps it was something I mentioned inside the blog post with a different title. But anyways, this is a short roundup of what I&#8217;ve been up to lately.</p>
<p>Let&#8217;s start with Python.. After reading a very popular book called <a href="http://oreilly.com/catalog/9780596529321">Programming Collective Intelligence</a> by Toby Segaran (review coming later), I had no choice but to peek at how Python is doing. I didn&#8217;t carry out much of the excercizes in the book while reading it, but I did run a few experiments afterwards. Some of the code examples were translated into PHP to immediately implement into my <a href="http://twibots.com">Twibots</a> project, the juice was left for later.</p>
<p>This weekend I decided to gather some data from the Twitter Streaming API for some experimental purposes, and I decided to do this in Python. Of course I hadn&#8217;t had much time to read good books (still waiting from Amazon), but the plenty online tutorials, guides and articles out there are quite okay. So with a little trial and error I managed to write a script which gathers a little over 10,000 tweets in 30 minutes and dumps them into a database. That&#8217;s 20,000 tweets an hour, ~ half a million tweets a day. What am I going to do with those? I&#8217;ll give you an insight on this project a little bit later ;)</p>
<p>Twitter. I finally got #newtwitter. I thought it was a joke. It&#8217;s not a secret that I have multiple Twitter accounts, but I use them for development purposes, and guess what, those accounts were switched to the new Twitter three weeks before my account was. So yeah, I&#8217;m a little mad at Twitter, but their new look really rocks. They also need some updates to their mobile version.</p>
<p>.NET MVC. Some of you might be wondering (or not) what happened to that .NET MVC project of mine. Well it&#8217;s 99% finished, but we&#8217;re constantly coming up with changes to the content part, switching images around and other dirty work. Latest comment from the client was &#8211; okay so I open up Safari, click View and check the Zoom Text Only option. Then I zoom in and the whole design breaks! You guys promised me nice-looking XHTML in Opera, Safari, Chrome, IE and Firefox, what the heck?</p>
<p>PHP. I wrote an app in PHP that allows me to turn off my PC from my iPhone. I extended it to include volume up/down features a few days later. These are the kind of apps you write when you&#8217;re lazy ;)</p>
<p>And finally some goodies from all around the web for web designers and developers:</p>
<ul>
<li>A new CSS framework is in town: <a href="http://thesquaregrid.com/">The Square Grid</a></li>
<li><a />60 Exclusive Free Icons: &#8220;Childish&#8221;</a></li>
<li><a href="http://techcrunch.com/2010/10/12/kontagent-unleashes-new-version-of-facebook-analytics-platform/">Kontagent Unleashes New Version Of Facebook Analytics Platform</a></li>
<li><a href="http://sixrevisions.com/html/canvas-element/">HTML5 Canvas Element Guide</a> by Louis Lazaris</li>
<li><a href="http://www.smashingmagazine.com/2010/10/07/5-useful-coding-solutions-for-web-designers/">Five Useful Design Techniques and Coding Solutions For Web Designers</a></li>
<li><a href="http://www.clusterdb.com/mysql-cluster/get-mysql-replication-up-and-running-in-5-minutes/">Get MySQL Replication up and running in 5 minutes</a></li>
<li><a href="http://www.1stwebdesigner.com/development/object-oriented-basics-javascript/">The Ultimate Guide to Object Oriented Basics of JavaScript</a> by Joseph McCullough</li>
</ul>
<p>That&#8217;s it for today. Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2010/october-quickie-a-little-bit-of-everything/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A 60-Day Trip from PHP to .NET MVC</title>
		<link>http://kovshenin.com/2010/a-60-day-trip-from-php-to-net-mvc/</link>
		<comments>http://kovshenin.com/2010/a-60-day-trip-from-php-to-net-mvc/#comments</comments>
		<pubDate>Wed, 29 Sep 2010 07:15:39 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=2528</guid>
		<description><![CDATA[This is quite an unusual post I guess, something that came in to my mind a few days ago. If you&#8217;re familiar with my biography you know I&#8217;m in love with php, unix and the open source world. The reason I&#8217;m writing this post (or maybe set of posts) is that I found quite a [...]]]></description>
			<content:encoded><![CDATA[<p>This is quite an unusual post I guess, something that came in to my mind a few days ago. If you&#8217;re familiar with <a href="http://kovshenin.com/about/">my biography</a> you know I&#8217;m in love with php, unix and the open source world. The reason I&#8217;m writing this post (or maybe set of posts) is that I found quite a good client, who&#8217;s very strict about technology, so the very basic website that they&#8217;re looking for should be written in .NET in a little less than two months.</p>
<p>I haven&#8217;t finished the whole project yet, but I&#8217;d like to give you a short overview of .NET MVC from a PHP developer&#8217;s perspective, as well as share some coding thoughts in general. Don&#8217;t treat me as a Microsoft hater though, I love what they&#8217;re doing, and there are a bunch of very rich web applications driven by Microsoft&#8217;s technology! But since I&#8217;m used to what I&#8217;m used to, I feel a little bit weird and disappointed.</p>
<p>Why did I pick MVC? Well, perhaps because it&#8217;s quite new compared to the rest, and a bit of a challange since I don&#8217;t have any friends familiar with this monster. So the only places to find help are <a href="http://google.com">Google</a> and <a href="http://stackoverflow.com">Stackoverflow</a>. <a href="http://msdn.microsoft.com/">MSDN</a> is pretty much useless, especially the localized versions.</p>
<p>There are several reasons why I called .NET MVC a monster, and here&#8217;s a short list based on the first few weeks of my experience:</p>
<ul>
<li>Too many abstractions (week 1)</li>
<li>No SQL code at all (week 2)</li>
<li>Models are built using drag &#8216;n drop (week 3)</li>
<li>Name conventions are silly (week 3)</li>
<li>Confusing (week 4)</li>
</ul>
<h2>Getting used to LINQ</h2>
<p>I started my trip by exploring the <a href="http://www.asp.net/mvc/samples/mvc-music-store">MVC Music Store Tutorial</a> on the ASP website, which gives quite a clear overview of how to setup a basic website structure, handle some user input and work with the database using <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query">LINQ</a>. Wow, I never thought SQL was so complicated that they had to reinvent it. I&#8217;ve seen some abstractions over SQL (PDO, Zend_Db) but LINQ is something different:</p>
<pre>var shop = (from s in db.Shops where s.Slug == id select s).First();
var workingHours = (from m in shop.ShopMetas where m.Key == "working-hours" select m.Value).ToList();
var comments = (from c in db.Comments orderby c.PublishDate descending select c).Skip(page * 5).Take(5).ToList();</pre>
<p>Third line shows my implementation of pagination on a page full of comments. Groups and joins are even more &#8220;fun&#8221;. I came across a handy resource for studying LINQ called <a href="http://www.hookedonlinq.com/">Hooked on LINQ</a> with an awesome section called 5 minute overviews.</p>
<p>But anyways, quoted from Wikipedia: Some benchmark on simple use cases tend to show that LINQ to Objects performance has a large overhead compared to normal operation.</p>
<p>Haven&#8217;t tested, haven&#8217;t benchmarked, maybe it&#8217;s different. But from what I&#8217;ve seen, same old SQL is under the hood. Now, a few words about models.</p>
<h2>Building Models and Relations</h2>
<p>We all know <abbr title="Model-View-Controller">MVC</abbr> doesn&#8217;t go around without models, yet, I never thought models could be designed using only your mouse! I was quite surprised when I saw the models being generated from a database schema in the Music Store tutorial, yet, that&#8217;s not as deep as it goes. More interesting is inheritence, mapping, foreign keys and relations, all this using only your mouse! I felt kinda dumb, really. For a second I thought this was programming for kids, but hey, it really makes life simpler! Kudos to Microsoft ;)</p>
<p><img class="aligncenter size-full wp-image-2538" src="http://kovshenin.com/files/2010/09/visual-studio-data-model.png" alt="Visual Studio Data Model" width="680" height="243" /></p>
<p>Worth mentioning is that there&#8217;s a flag called Singularize model names when building models from a database schema, which converts the Shops table to a Shop model, a Comments table to a Comment model, (quite cool this one) a News table to a News model, and a (surprise) Movies table to a Movy model.. Whaa? ;)</p>
<p>Next is something that took me quite some time to sort out, so to save you that time, I&#8217;ll give you a quick tip.</p>
<h2>The Master View</h2>
<p>Master views are cool, but according to Google, one of the most frequently asked questions by .NET MVC newbies is &#8220;how on earth do I pass data into my master view&#8221;? Yup, I asked that question too, but didn&#8217;t get too much responses. So here&#8217;s a newbie response (how I see it): Master views are very similar to normal news, so they need data passed by a controller. You can go ahead and pass the same data for the master view with each and every controller that you create, but it would be difficult to manage, you need something more centralized.</p>
<p>So the answer would be to create an abstract class (OOP, remember?) which would derive from the standard Controller class, then change your controllers to derive from your newly created class. Easy as that:</p>
<pre>public abstract class ApplicationController : Controller
{
    public ApplicationController()
    {
        // This is how you pass some data to your master view
        ViewData["my-key"] = "my value";
    }
}</pre>
<p>Then make sure you derive your controllers from the new ApplicationController. Here&#8217;s my Home controller:</p>
<pre>public class HomeController : ApplicationController
{
    public ActionResult Index()
    {
        return View(viewModel);
    }
}</pre>
<p>Note that I dropped the namespace for both code pieces of code to make them shorter. The namespace used is MyApplication.Controllers. Let&#8217;s switch over to the actual Master view and here&#8217;s how you capture the data:</p>
<pre>Response.Write(ViewData["my-key"].ToString());</pre>
<p>That wasn&#8217;t very difficult, was it? You can pass all the data/objects you want in ViewData as well as run LINQ queries inside the Application Controller for your sidebar widgets, menus and other stuff that comes from the database.</p>
<p>Well folks! That&#8217;s about it. I&#8217;ll keep you posted on my experience in .NET MVC but honestly, I&#8217;m getting tired of it already, can&#8217;t wait to ride home and get my hands back onto php ;) Thanks for reading and sharing!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2010/a-60-day-trip-from-php-to-net-mvc/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>WordPress and Magic Quotes</title>
		<link>http://kovshenin.com/2010/wordpress-and-magic-quotes/</link>
		<comments>http://kovshenin.com/2010/wordpress-and-magic-quotes/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 05:57:09 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[facebook]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[facebook api]]></category>
		<category><![CDATA[graph api]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=2429</guid>
		<description><![CDATA[This is crazy, and based on a post called WordPress and PHP magic quotes: you want to run me crazy! by Stefano Lissa. I&#8217;m writing a plugin prototype for WordPress that uses the new Facebook Graph API to post stuff to my wall on Facebook (upcoming blog post). The original Facebook PHP SDK comes in [...]]]></description>
			<content:encoded><![CDATA[<p>This is crazy, and based on a post called <a href="http://www.satollo.net/wordpress-and-php-magic-quotes-you-want-run-me-crazy">WordPress and PHP magic quotes: you want to run me crazy!</a> by Stefano Lissa. I&#8217;m writing a plugin prototype for WordPress that uses the new Facebook Graph API to post stuff to my wall on Facebook (upcoming blog post). The original <a href="http://github.com/facebook/php-sdk">Facebook PHP SDK</a> comes in very handy when working with the Facebook API, and I had quite some fun using it, but..</p>
<p>I&#8217;ve been trying to figure this out for hours! I had code working outside WordPress and once I pumped it into a plugin it suddenly stopped authorizing me. I had to dig through the facebook.php code to figure out what&#8217;s happening, and here it is. The getSession() method uses the <a href="http://php.net/manual/en/function.get-magic-quotes-gpc.php">get_magic_quotes_gpc</a> function and strips the slashes from the $_COOKIE superglobal if it&#8217;s switched on. Of course, that&#8217;s the correct logic supporting both php 5 and php 6, but not WordPress.</p>
<p>I looked through the latest (3.0.1) WordPress core code and was quite surprised to see a function called wp_magic_quotes(). Oh my god, thought I! Commented as: Add magic quotes and set up $_REQUEST ( $_GET + $_POST ).</p>
<p>What the hell is that? Okay, let&#8217;s see:</p>
<pre>$_GET = add_magic_quotes($_GET);
$_POST = add_magic_quotes($_POST);
$_COOKIE = add_magic_quotes($_COOKIE);
$_SERVER = add_magic_quotes($_SERVER);</pre>
<p>How does that sound? So all my apps, plugins, external libraries working with server variables (like Facebook does with cookies) are not allowed to use the magic quotes function? This means that working with WordPress, we must initially assume that all these are quoted, no matter what the php settings are. I don&#8217;t even know what question to ask here, perhaps: Is this the way things are done? Why?</p>
<p>To be honest this is getting me a little frustrated. Not by the fact that they&#8217;re slashing the whole input (although I don&#8217;t see a reason to) but, heh, I&#8217;ve been coding based on WordPress for over two years now, and never came across anything like this. Did I miss something in the Getting Started guide? ;) Anyways, the easiest way to get this working is to replace your get_matic_quotes_gpc function with 1, which says it is always switched on.</p>
<p>Eh, Monday morning disappointment ;) Cheers, and thanks for sharing the post!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2010/wordpress-and-magic-quotes/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Counting Facebook Fans in PHP: The Graph API Way</title>
		<link>http://kovshenin.com/2010/counting-facebook-fans-in-php-the-graph-api-way/</link>
		<comments>http://kovshenin.com/2010/counting-facebook-fans-in-php-the-graph-api-way/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 05:53:19 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[facebook]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[facebook api]]></category>
		<category><![CDATA[graph api]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=2446</guid>
		<description><![CDATA[In a previous blog post called How to Count Facebook Fans in PHP I&#8217;ve shown a code snippet of how to count the number of fans on a fan page using PHP. Times have changed, the Graph API has been introduced, and due to some responses I introduce here the new way of retrieving your [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous blog post called <a href="http://kovshenin.com/2010/how-to-count-facebook-fans-in-php/">How to Count Facebook Fans in PHP</a> I&#8217;ve shown a code snippet of how to count the number of fans on a fan page using PHP. Times have changed, the Graph API has been introduced, and due to some responses I introduce here the new way of retrieving your fans count using the new Graph API and php.</p>
<p>Before you copy and paste, flush my comments with &#8216;my code is not working&#8217; posts, I&#8217;d like to get your attention to versioning of the Facebook PHP SDK which we&#8217;ve been using all this time. The SDK has changed and of course the old method doesn&#8217;t work with the new SDK which is mostly tuned to Graph API, therefore, my previous code still works on a dozen on websites, because I have the old SDK installed back there. So please, be careful to what you download and use, read release notes and change logs, it will save you hours of googling.</p>
<p>The following snippet is based on the <a href="http://github.com/facebook/php-sdk/">Facebook PHP SDK</a> version 2.1.1 (use the Switch Tags option on github to browse through different tags). So get a fresh copy of facebook.php and have it somewhere nearby.</p>
<p>Unlike the old FBQL way, the new Graph API is much easier to work with, and retrieving the fans count is literally two lines of code (initialization doesn&#8217;t count). Here&#8217;s the snippet to retrieve the fans count for <a href="http://facebook.com/mashable">Mashable</a> (don&#8217;t forget to replace your application ID and API secret):</p>
<pre>require('facebook.php');

$facebook = new Facebook(array(
	'appId' =&gt; 'your-app-id',
	'secret' =&gt; 'your-api-secret',
));

$mashable = $facebook-&gt;api('/mashable');
echo 'Mashable has ' . $mashable['fan_count'] . ' fans';</pre>
<p>Easy as that! I was also surprised to see that the Graph API is doing so well. Yeah, the documentation is not very rich, but whenever you need to retrieve something from Facebook, you can always print_r the results, which gives you the full picture. Sending data into Facebook is a little trickier and I&#8217;ll show you how in a later blog post.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2010/counting-facebook-fans-in-php-the-graph-api-way/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>How to Generate Quality Data for MySQL</title>
		<link>http://kovshenin.com/2010/generate-quality-data-for-mysql/</link>
		<comments>http://kovshenin.com/2010/generate-quality-data-for-mysql/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 10:35:04 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[apps]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=2423</guid>
		<description><![CDATA[We all had fun with the World Database, Sakila and the others when learning MySQL (see Example Databases), but it sometimes isn&#8217;t enough to run certain experiments, benchmarks within your own schema. Of course you could write a script that would generate junk data based on your column types and populate your database with a [...]]]></description>
			<content:encoded><![CDATA[<p>We all had fun with the World Database, Sakila and the others when learning MySQL (see <a href="http://dev.mysql.com/doc/index-other.html">Example Databases</a>), but it sometimes isn&#8217;t enough to run certain experiments, benchmarks within your own schema. Of course you could write a script that would generate junk data based on your column types and populate your database with a few thousand entries, but as it turns out, Benjamin Keen already did.</p>
<p>Meet <a href="http://www.generatedata.com/">Generate Data</a> &#8211; a free and open source script written in php, generates quality data for your databases. Works with MySQL and, well, pretty much with any SQL compliant database I guess. What I liked about Generate Data is that you get to pick your columns, their types, and the sample data like names, last names, integer between two values, lorem ipsum (my favorite) and a bunch of others. This is why I said &#8220;quality data&#8221;.</p>
<p>There are a few issues I encountered, like trying to get 5000 rows gave me only 200, but such issues could easily be solved by downloading the source code and launching it locally with a few fixing (I wonder why Benjamin did such a bad job at documenting the whole thing). It took me a few minutes to fill up 30,000 rows of sample data, so who needs the World database anyway?</p>
<p>The script is being updated from time to time and new features are being added, not too fast, but they are. Let&#8217;s see where Benjamin takes this by the end of this year ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2010/generate-quality-data-for-mysql/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Highcharts: Pure Javascript Charts API</title>
		<link>http://kovshenin.com/2010/highcharts-javascript-charts-api/</link>
		<comments>http://kovshenin.com/2010/highcharts-javascript-charts-api/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 18:51:45 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[charts]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=2368</guid>
		<description><![CDATA[I was working on a project lately that involved charts and graphs which had to be interactive, lightweight and somewhat complicated. I looked through quite a lot of different chart APIs, and for some time thought that I&#8217;d have to go with Open Flash Chart which is good and simple, but it was Flash. There&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on a project lately that involved charts and graphs which had to be interactive, lightweight and somewhat complicated. I looked through quite a lot of different chart APIs, and for some time thought that I&#8217;d have to go with <a href="http://teethgrinder.co.uk/open-flash-chart-2/">Open Flash Chart</a> which is good and simple, but it was Flash. There&#8217;s nothing wrong with Flash, but Flash is what makes it more complicated to modify and extend. The standard <abbr title="Open Flash Chart">OFC</abbr> functionality seemed to work fine for me, but soon I came across <a href="http://www.highcharts.com/">Highcharts</a>.</p>
<p><img class="aligncenter size-full wp-image-2369" src="http://kovshenin.com/files/2010/07/highcharts.png" alt="Highcharts: Pure Javascript Charts API" width="680" height="368" /></p>
<p><a href="http://www.highcharts.com/">Highcharts</a> is purely written in javascript and uses some advanced <abbr title="Scalable Vector Graphics">SVG</abbr> to render the content. It&#8217;s quite impressive and very well documented. Chart control is all done via javascript, and all the options available make it very flexible and extensible. Since everything&#8217;s done in javascript it fits well with PHP without having to write tonnes of code or use some third-party library. The <a href="http://php.net/manual/en/function.json-encode.php">json_encode</a> function comes in very handy when passing options from PHP to Highcharts.</p>
<p>Highcharts is free for personal use, commercial licenses start from $80, which is quite okay. Check out the <a href="http://www.highcharts.com/demo/">Highcharts Demo</a> page for some terrific usage examples. Happy charting and thanks for sharing!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2010/highcharts-javascript-charts-api/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>5 Awesome Links for Web Designers &amp; Developers</title>
		<link>http://kovshenin.com/2010/5-awesome-links-for-web-designers-developers/</link>
		<comments>http://kovshenin.com/2010/5-awesome-links-for-web-designers-developers/#comments</comments>
		<pubDate>Sat, 17 Jul 2010 11:45:13 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=2354</guid>
		<description><![CDATA[It&#8217;s July and it&#8217;s so damn hot back here! I think all the possible new records have been set last week in Moscow. Thank god we had those new air conditioners install in the office before the heat wave, otherwise it would&#8217;ve been impossible to work. Anyways, haven&#8217;t tweeted much this week, but I did [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s July and it&#8217;s so damn hot back here! I think all the possible new records have been set last week in Moscow. Thank god we had those new air conditioners install in the office before the heat wave, otherwise it would&#8217;ve been impossible to work. Anyways, haven&#8217;t tweeted much this week, but I did go through my RSS feeds, and here are some interesting links for web design and development fans:</p>
<ul>
<li><a href="http://www.browsercover.me/">Browser Cover me</a> &#8211; before you present your designs to your clients, make sure you give them a cool border with this online app. There are several different systems and browsers you can pick from, which makes it even more awesome!</li>
<li><a href="http://code.google.com/p/phpquery/">phpQuery</a> &#8211; how many times did you have to screenscrape a web page and dig something out of it? Yes, XPath is cool, but it doesn&#8217;t work with HTML people write these days ;) phpQuery gives you the power of jQuery selectors and functions in php!</li>
<li><a href="http://webdesignledger.com/freebies/15-must-have-minimalist-icon-sets/">15 Must-Have Minimalist Icon Sets</a> &#8211; an awesome freebie set for your new web designs. It&#8217;s quite important to keep using the fresh stuff, so posts like these are quite handy</li>
<li><a href="http://www.smashingmagazine.com/2010/07/01/10-useful-wordpress-security-tweaks/">10 Useful WordPress Security Tweaks</a> &#8211; a very nice article on Smashing Magazine about WordPress security. Very informative and awesome!</li>
<li><a href="http://tumblr.kovshenin.com/">Konstantin&#8217;s Notes</a> &#8211; okay okay, this is my Tumblr page which I use to post stuff on the fly, sometimes quite interesting ;) which makes it awesome too!</li>
</ul>
<p>This was the short list for today. Stay tuned and thanks for sharing!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2010/5-awesome-links-for-web-designers-developers/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>How To: Retrieve a User ID via SOAP in SugarCRM</title>
		<link>http://kovshenin.com/2010/retrieve-user-id-soap-sugarcrm/</link>
		<comments>http://kovshenin.com/2010/retrieve-user-id-soap-sugarcrm/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 09:09:16 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[sugarcrm]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[soap]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=2313</guid>
		<description><![CDATA[Following the Lead Generation Forms with WordPress and SugarCRM post, I came across the need to assign the generated lead to some person in the CRM. It would be easy if you could do it by simply using usernames in the system, but unfortunately usernames may change, but user IDs do not. User IDs in [...]]]></description>
			<content:encoded><![CDATA[<p>Following the <a href="http://kovshenin.com/2010/lead-generation-forms-with-wordpress-and-sugarcrm/">Lead Generation Forms with WordPress and SugarCRM</a> post, I came across the need to assign the generated lead to some person in the CRM. It would be easy if you could do it by simply using usernames in the system, but unfortunately usernames may change, but user IDs do not.</p>
<p>User IDs in SugarCRM are stored in a 36-byte alphanumeric string with symbols, which is easy to capture from the database, but not very usable that way. I worked a little more on the snippet I gave before and came up with a new function called getUserId, where a search is performed based on the username.</p>
<p>I&#8217;m not sure if this is very secure, but will surely work if you need to simply capture the ID of a certain user and perhaps hard-code it in your script or settings file. Make sure you read the <a href="http://kovshenin.com/2010/lead-generation-forms-with-wordpress-and-sugarcrm/">previous post</a> about SugarCRM to capture the whole Sugar class together with the authentication and lead creation functions. The following function is simply an addition to the class:</p>
<pre>function getUserId($username)
{
	$result = $this-&gt;soap-&gt;call('get_entry_list', array(
		'session' =&gt; $this-&gt;session,
		'module_name' =&gt; 'Users',
		'query' =&gt; "user_name = 'pavel'",
		'max_results' =&gt; 1
	));

	return $result;
}</pre>
<p>Once you&#8217;re done with that, use your sugar object to authenticate and request for a user id:</p>
<pre>$sugar = new SugarCRM('username', 'password');
$sugar-&gt;login();

$result = $sugar-&gt;getUserId('john');
print_r($result);</pre>
<p>So $result will contain an array where you&#8217;ll find the alphanumeric ID of the person with the username &#8220;john&#8221;.</p>
<p>I believe there was some other method for doing that in SugarCRM version prior to 5.5.2 CE, but the new SOAP Web Services are mostly based on a few general methods &#8211; get_entry, get_entry_list, set_entry, etc, which are applicable to basically any module including Users.</p>
<p>Now, in order to assign the new lead to your retrieved user (using the createLead function), just add an extra parameter to the array:</p>
<pre>$result = $sugar-&gt;createLead(array(
	'lead_source' =&gt; 'Web Site',
	'lead_source_description' =&gt; 'Contact form on my website',
	'lead_status' =&gt; 'New',
	'first_name' =&gt; $_POST['firstname'],
	'last_name' =&gt; $_POST['lastname'],
	'phone_work' =&gt; $_POST['phonenumber'],
	'description' =&gt; $_POST['description'],
	'email1' =&gt; $_POST['email'],
	'assigned_user_id' =&gt; $user_id // Your ID goes here
));</pre>
<p>Now your newly created leads will be assigned to your user, which is quite convenient when you have e-mail notifications turned on during assignment. Hope that helped somebody! Cheers, and thanks for sharing!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2010/retrieve-user-id-soap-sugarcrm/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

