Snippets PHP para interactuar con Twitter

0 - , - 09/05/2012 10:09:06

En catswhocode han publicado una selección de fragmentos de código en PHP con los que podemos interactuar con Twitter. Por ejemplo, la siguiente función PHP permite obtener el número de retweets de una determinada página.

function tweetCount($url) {
    $content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
    $element = new SimpleXmlElement($content);
    $retweets = $element->story->url_count;
    if($retweets){
        return $retweets;
    } else {
        return 0;
    }
}

PHP snippets to interact with Twitter » »

Crear un sencillo sistema de caché con PHP

0 - - 19/01/2012 13:01:59

Pequeño fragmento de código que nos permite crear un sencillo sistema de caché para nuestras páginas web.

<?php
    // define the path and name of cached file
    $cachefile = 'cached-files/'.date('M-d-Y').'.php';
    // define how long we want to keep the file in seconds. I set mine to 5 hours.
    $cachetime = 18000;
    // Check if the cached file is still fresh. If it is, serve it up and exit.
    if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
    include($cachefile);
        exit;
    }
    // if there is either no file OR the file to too old, render the page and capture the HTML.
    ob_start();
?>
    <html>
        output all your html here.
    </html>
<?php
    // We're done! Save the cached content to a file
    $fp = fopen($cachefile, 'w');
    fwrite($fp, ob_get_contents());
    fclose($fp);
    // finally send browser output
    ob_end_flush();
?>

Visto en Wes Bos » »

Obtener un color aleatorio con PHP

0 - , - 26/12/2011 11:10:43

La siguiente función permite obtener un color aleatorio en formato hexadecimal.

function randomColor() {
    $str = '#';
    for($i = 0 ; $i < 6 ; $i++) {
        $randNum = rand(0 , 15);
        switch ($randNum) {
            case 10: $randNum = 'A'; break;
            case 11: $randNum = 'B'; break;
            case 12: $randNum = 'C'; break;
            case 13: $randNum = 'D'; break;
            case 14: $randNum = 'E'; break;
            case 15: $randNum = 'F'; break;
        }
        $str .= $randNum;
    }
    return $str;
}

Ver ejemplo en funcionamiento » »
Visto en PHP Snippets » »

PHP: función para extrar los emails de una cadena

0 - , - 19/12/2011 08:00:00

El siguiente snippet permite extraer todos los emails contenidos dentro de una cadena.

function extract_emails($str){
    // This regular expression extracts all emails from a string:
    $regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
    preg_match_all($regexp, $str, $m);
    return isset($m[0]) ? $m[0] : array();
}

Visto en catswhocode»»

PHP: Mostrar el número de fans de facebook de una página

0 - , , - 19/11/2011 18:37:53

Pequeño snippet que muestra el número de fans de una página de facebook.

<?php
$page_id = "ID LA PAGINA";
$xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot");
$fans = $xml->page->fan_count;
echo $fans;
?>