因项目需求,网站需要在线获取facebook、twitter等第三方平台用户的头像,整理了一个PHP方法:
/** * 获取第三方平台头像 * @param $platformType string 平台类型 facebook/twitter */ public function getThirdPlatformAvatar($platformId, $platformType = '') { if ($platformType == 'facebook') { $imgUrl = 'https://graph.facebook.com/' . $platformId . '/picture?type=large'; } else if ($platformType == 'twitter') { $imgUrl = 'http://twitter.com/api/users/profile_image?id=' . $platformId; } $img = file_get_contents($imgUrl); $fileName = $platformId . time() .'.jpg'; $file = APP_PATH . 'data/avatar/' . $fileName; file_put_contents($file, $img); $result = array( 'status' => 1, 'src' => DOMAIN . 'data/avatar/' . $fileName, 'id' => $platformId ); return $result; }