php

PHP & FlickrAPI – Photo wall

I wrote a simple code/class for automatic get photos from flickr photostream (by service API)  to compose randomly a nice miniature photowall (you can see an example on my homepage).

First of all you can need API code (only key, we don’t need secret authentication).

Now the code, my class file was called lib/fget.php in here we define variable, class and function construct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
class Flickr {
//API Key you can get them on http://www.flickr.com/services/apps/create/apply/
private $apiKey = 'xxxx';
//We define our numeric username http://www.flickr.com/services/api/explore/?method=flickr.people.getInfo
private $NSID = '29479498@N05';
//File per page (max file 200 for free account)
private $ppage = '200';public function __construct() {
}public function retrive() {
//For get data we use REST method and serialize option
$getdata = 'http://flickr.com/services/rest/?method=flickr.photos.search&api_key=' . $this->apiKey . '&user_id=' . $this->NSID . '&per_page=' . $this->ppage . '&format=php_serial';
//Get the data
$result = file_get_contents($getdata);
//De serialize for array use
$result = unserialize($result);
return $result;
}
}
?>

Now include the class into a sample page:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
//Include the class
require_once('lib/fget.php');
//Recall the class
$Flickr = new Flickr;
//Retrive data array
$data = $Flickr->retrive();
//Randomize data
shuffle($data['photos']['photo']);
//Define 0 to the counter (we need this some line down here)
$count = 0;
?>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>deepreflect.net</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.css" rel="stylesheet" type="text/css">
</head>
<body>
<p>
<a href="home.php">
<?php
 
        foreach($data['photos']['photo'] as $photo) { 
        //URL base format for all photo, if you wanna miniature you can modify .jpg into _s.jpg
        // http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}.jpg
 
        //print photo sequence:
	echo '<img alt="click to Enjoy..." src="http://farm' . $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '_s.jpg">'; 
	//Start the counter
	$count++;
	//Every 13 create a new line
	if ($count == 13) echo '<br>';
	if ($count == 26) echo '<br>';
	if ($count == 39) echo '<br>';
	if ($count == 52) echo '<br>';
	if ($count == 65) echo '<br>';
	if ($count == 78) echo '<br>';
	//Limit print results to 91 photo
	if ($count == 91) break;
}
?>
</a>
</p>
</body>
</html>

PHP – Charset

Questo è dedicato a tutti quei programmatorucoli da quattro soldi che credono che si un problema di server quando nelle loro paginette del cavolo non vedono correttamente i caratteri.

Questa cosa:

<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″/>

Solitamente è quello che viene inserito nelle pagine html.

Per chi volesse capire ecco la spiegazione del perché questo non basta, in pratica l’engine legge il codice lo interpreta e restituisce un qualcosa, ovviamente questo qualcosa è definito chiaramente in fase di programmazione se vi dimenticate di inserire la codifica (vedi sotto) come diavolo può sapere l’engine php, che charset avete usato nella pagina?

<?php
header('Content-type: text/html; charset=utf-8');
?>

Quindi ricordate di entrambe le cose anche se la pagina contiene html ma è .php e non rompete ai sistemisti che hanno già tante cose da fare.

Php – Riconoscimento lingua browser

Visto che mi hanno chiesto un sistema per riconoscere la lingua del browser con successiva visualizzazione del rispettivo contenuto, posto il codice, spiego usanto una variabile di ambiente php e comparando le prime due cifre della lingua impostata nel browser le compara con due lingue italiano e tedesco e successivamente se non sono una di queste due rimanda alla pagine inglese, semplice e coinciso:

<?php
if (substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2) == "it")
{
include("index_ita.htm");
}
elseif (substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2) == "de")
{
include("index_deu.htm");
} else {
include("index_eng.htm");
}
?>