Latest Updates: php RSS

  • Get likes on urls from Facebook

    Ellen 18:01 on November 30, 2010 | 21 Permalink | Reply
    Tags: , php

    Sometimes you might wanna list things on your site order by the popularity on Facebook. It can also be a handy tool compare your different sites Facebook likes and get social stats from it. Facebook offer a very handy service to check this without being logged in to Facebook in any way. Check the service we use here. Facebook will retreive all Facebook likes, share count and total counts of these. For each link, it will display:


    Get total likes on urls

    With a loop in php we can get the information we need:

    function getLikes($arr){
          $urls = "";

          // Add urls to check for likes
          for($i = 0;$i < count($arr);$i++) {
                if($urls != "") $urls .= ",";
                $urls .= $arr[$i];

          // Retreive info from Facebook
          $facebookapi = "" . $urls;
          $xml = simplexml_load_file($facebookapi);

          $likes = array();
          // Loop through the result and populate an array with the likes
          for ($i = 0;$i  < count($arr);$i++) {            
                $url = $xml->link_stat[$i]->url;
                $counts = (int)$xml->link_stat[$i]->like_count;
                $likes[] = array("likes" => $counts, "url" => $url);

          return $likes;


    // Array with links to the URLs we want to get number of likes from
    $array = array("","","");
    $likes = getLikes($array);

    foreach ($likes as $key => $val) {
          echo $key . " => " . $val["url"] . " => " . $val["likes"] . "<br />";

    Sorting the likes

    After retrieving the likes, the array can be sorted by most popular url.

    $likes = array_sort($likes, "likes", SORT_DESC);

    foreach ($likes as $key => $val) {
          echo $key . " => " . $val["url"] . " => " . $val["likes"] . "<br />";

    Download source

  • How to urlencode your XMLHttpRequest request

    Ellen 13:04 on September 28, 2010 | 0 Permalink
    Tags: ajax, encodeURIComponent, , php

    I had problems getting characters like +@*/ to work by posting them with a XMLHttpRequest. The escape method in javascript does not suppert characters like this so I had to use encodeURIComponent and urlencode each one of my parameters. The next step in PHP was to turn characters like åäö into upper case. There are several ways of converting these characters to upper case in php but I found the easiest way was to convert them in JavaScript before sending them to PHP.

    var name = document.getElementById(”name”);
    var url = document.getElementById(”url”);
    var params = “name=” + encodeURIComponent(name.value.toUpperCase()) + “&url=” + encodeURIComponent(url.value);
    var url = “generate.php”;”POST”,url,true); //calling testing.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader(”Content-type”, “application/x-www-form-urlencoded”);
    xmlhttp.setRequestHeader(”Content-length”, params.length);
    xmlhttp.setRequestHeader(”Connection”, “close”);

compose new post
next post/next comment
previous post/previous comment
show/hide comments
go to top