Test Twitter Friendships with PHP

By  on  

Another awesome functionality provided by Twitter is the ability to test friendships between two people. I mean testing if two people follow each other, not if one spams the world with crap tweets. Just as with every other Twitter API call, executing this test is easy with PHP.

The PHP

/* persons to test */
$person1 = 'davidwalshblog';
$person2 = 'mootools';

/* send request to twitter */
$url = 'http://twitter.com/friendships/exists';
$format = 'xml';

/* check */
$persons12 = make_request($url.'.'.$format.'?user_a='.$person1.'&user_b='.$person2);
$result = get_match('/<friends>(.*)<\/friends>/isU',$persons12);
echo '<pre>Am I friends with @mootools?'.$result.'</pre>'; // returns "true" or "false"

/* makes the request */
function make_request($url) {
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}

/* gets the match */
function get_match($regex,$content)
{
	preg_match($regex,$content,$matches);
	return $matches[1];
}

That's it. I didn't use SimpleXML in my example because of the small size of the return XML. Too much extra overhead. Here's the XML sample response:

<friends>true</friends>

The Twitter API is fun, isn't it?

Recent Features

  • By
    39 Shirts &#8211; Leaving Mozilla

    In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

  • By
    CSS Tooltips

    We all know that you can make shapes with CSS and a single HTML element, as I've covered in my CSS Triangles and CSS Circles posts.  Triangles and circles are fairly simply though, so as CSS advances, we need to stretch the boundaries...

  • By
    Sexy Album Art with MooTools or jQuery

    The way that album information displays is usually insanely boring. Music is supposed to be fun and moving, right? Luckily MooTools and jQuery allow us to communicate that creativity on the web. The XHTML A few structure DIVs and the album information. The CSS The CSS...

Discussion

  1. What does the isU bit do on the regex?

  2. @ben: i = case insensitive, s = dot matches all (newlines included), U = ungreedy modifier (same as (.*?))

  3. So are there any advantages to using CURL over a simple file_get_contents($url) to make the request?

  4. @Steve: CURL is generally faster and more secure.

  5. Great stuff David, I’ve only had the chance to play with the basics of the Twitter API so you just gave me a few ideas for when I get bored.

    @Steve: I asked the same question to David once and believe he is correct. Check out this post (comments section), and then follow the benchmark link that David links to– http://davidwalsh.name/create-short-urls-unu

  6. Wow that is faster! That’s good to know. I’ve been using file_get_contents for simple tasks since it only takes one line and seems to be more widely supported.

    Anyway, good post. I can see some creative uses coming from this.

  7. saif

    Dear, for me……..showing error message 400 for curl.
    :(

  8. David, if the return value is “false, we’re not friends” is there a way to add them using the API?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!