- Step 1:
Create an APP to Get your API Key
Got:
API_KEY
SECRET
*callback URL (where we write code to get & save auth token: http://yoursite/path/auth.php)
- Step 2: Authentication
http://flickr.com/services/auth/?api_key=[API_KEY]&perms=[perms]&api_sig=[api_sig]
[perms] = read or write or delete
[api_sig] = md5(SECRET + api_key[API_KEY]perms[perms])
- Step 3: Write your code on your server! File auth.php
Input: frob append by flickr
Output: auth token
$url = signUrl('http://www.flickr.com/services/rest/?method=flickr.auth.getToken&api_key=' . API_KEY . '&frob=' . $_GET['frob']);
$ch = curl_init();
$timeout = 100; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
# parse $file_contents to get auth token
function signUrl($url) {
$arr = parse_url($url);
$query = $arr['query'];
$arr = explode('&', $query);
$params = array();
foreach ($arr as $kv) {
list($key, $value) = explode('=', $kv);
$params[$key] = $value;
}
ksort($params);
$sec = SECRET;
foreach ($params as $key => $value) {
$sec .= $key . $value;
}
$sig = md5($sec);
$url .= '&api_sig=' . $sig;
return $url;
}
- Step 4: upload.php
$token = '**1*7626055692081-40035c7***90286c'; # from step 3
$url = 'http://api.flickr.com/services/upload/';
$ch = curl_init();
$timeout = 100; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$post = array(
"photo"=>"@" . realpath('images/huypv.net.jpg'),
"api_key" => API_KEY,
"auth_token" => $token
);
signParams($post);
#var_dump($post);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$file_contents = curl_exec($ch);
curl_close($ch);
echo htmlspecialchars($file_contents);
# result: <rsp stat="ok"> <photoid>5494723162</photoid> </rsp>