Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * 呼叫 flickr.people.getPublicPhotos
- * @param string $user_id 要傳回照片的user id
- * @return string json格式的字串
- */
- function getPublicPhotos($user_id) {
- $ch = curl_init();
- //產生API網址,有部分參數在此寫死,例如輸出格式這邊是直接設定為JSON
- $api_key = '3dd490cec911376eb3356af84efc05e6';
- $url = "https://api.flickr.com/services/rest/?method=flickr.people.getPublicPhotos&api_key={$api_key}&user_id={$user_id}&format=json&nojsoncallback=1";
- $options = array(
- CURLOPT_URL => $url,
- CURLOPT_HEADER => false,
- CURLOPT_RETURNTRANSFER => true,
- //CURLOPT_SSL_VERIFYHOST => 0,
- //CURLOPT_SSL_VERIFYPEER => 0,
- );
- curl_setopt_array($ch, $options);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- //檢查是否有GET的user_id參數
- if (isset($_GET['user_id']) and !empty($_GET['user_id'])) {
- $user_id = $_GET['user_id'];
- $data = getPublicPhotos($user_id);
- $json = json_decode($data, true);
- $photos = $json['photos']['photo'];
- } else {
- $photos = array();
- }
- ?>
- <html>
- <head></head>
- <body>
- <form method="get">
- <label>user_id <input type="text" name="user_id" value="<?php echo isset($_GET['user_id']) ? $_GET['user_id'] : ''; ?>"></label>
- <input type="submit">
- </form>
- <div>
- <?php
- foreach ($photos as &$photo) {
- //產生flickr的連結
- $url = "https://www.flickr.com/photos/{$photo['owner']}/{$photo['id']}";
- //產生照片的連結
- $img = "http://www.flickr.com/photos/{$photo['id']}_{$photo['secret']}.jpg";
- //另一種產生照片連結的方法
- //$img = "https://farm{$photo['farm']}.staticflickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}.jpg";
- echo "<a href=\"$url\" target=\"_blank\"><img src=\"{$img}\" border=\"0\"></a>\n";
- }
- ?>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement