public function __construct($options = array()) { $options = array_merge(array('api_key' => '', 'api_secret' => '', 'oauth_token' => '', 'oauth_secret' => ''), $options); $metadata = new Metadata($options['api_key'], $options['api_secret']); if ($options['oauth_token'] && $options['oauth_secret']) { $metadata->setOauthAccess($options['oauth_token'], $options['oauth_secret']); } $this->api = new ApiFactory($metadata, new GuzzleAdapter()); }
/** * 写真をFlickrにアップロード * @param $picture * @param null $title * @param null $description * @param null $tags * @return Flickr * @throws FlickrFailedException */ public function uploadFlickr($picture, $title = null, $description = null, $tags = null) { $metadata = new Metadata(config('const.flickr.apikey'), config('const.flickr.secret')); $metadata->setOauthAccess(config('const.flickr.oauth_token'), config('const.flickr.oauth_token_secret')); $factory = new ApiFactory($metadata, new GuzzleAdapter()); $upload = $factory->upload($picture, $title, $description, $tags, true); if ((string) $upload->attributes()->stat === 'fail') { $errAttr = $upload->err->attributes(); throw new FlickrFailedException((string) $errAttr->msg, (int) $errAttr->code); } $getInfo = $factory->call('flickr.photos.getInfo', array('photo_id' => (string) $upload->photoid)); $photoAttr = $getInfo->photo->attributes(); $flickr = new Flickr(); foreach (['id', 'server', 'farm', 'secret'] as $name) { $flickr->{'flickr_' . $name} = (string) $photoAttr->{$name}; } $flickr->save(); return $flickr; }
/** * @param array $parameters parameters * * @return string */ protected function buildSignature(array $parameters) { ksort($parameters); $sigUnhashed = $this->metadata->getSecret(); foreach ($parameters as $key => $value) { if (!in_array($key, self::$unsignedKeys)) { $sigUnhashed .= $key . $value; } } return md5($sigUnhashed); }
/** * * @param string $apiInfoPath * @param string $tokenPath * @return FlickrApiClient */ public static function getClient($apiInfoPath, $tokenPath) { $apiKeys = json_decode(file_get_contents($apiInfoPath)); $metadata = new Metadata($apiKeys->apiKey, $apiKeys->apiSecret); $factory = new AuthApiFactory($metadata, new ExtendedGuzzleAdapter()); if (file_exists($tokenPath)) { $token = json_decode(file_get_contents($tokenPath)); $metadata->setOauthAccess($token->token, $token->secret); } else { $authUrl = $factory->createAuthUrl(); printf("Open the following link in your browser:\n%s\n", $authUrl); print 'Enter verification code: '; $authCode = trim(fgets(STDIN)); $factory->authenticate($authCode); $token = new \stdClass(); $token->token = $metadata->getAccessToken(); $token->secret = $metadata->getAccessTokenSecret(); file_put_contents($tokenPath, json_encode($token)); } return new FlickrApiClient($factory); }
/** * @param string $service * @param array $parameters * @param string $endpoint * * @return array */ private function buildParams($service, array $parameters, &$endpoint) { if (null === $endpoint) { $endpoint = $this->metadata->getEndpoint(); } $default = array('api_key' => $this->metadata->getApiKey(), 'format' => 'rest'); if ($service) { $default['method'] = $service; } $parameters = array_merge($default, $parameters); $parameters = array_filter($parameters, function ($value) { return null !== $value; }); $parameters['api_sig'] = $this->buildSignature($parameters); $this->addOAuthParameters($endpoint, $parameters); return $parameters; }
/** * Get photo info and sizes * @since Version 3.5 * @param int $photo_id * @return array */ public function getPhotoLegacy($photo_id = false) { if (!$photo_id) { throw new Exception("Cannot fetch photo info and sizes - no photo ID given"); return false; } $mckey = "railpage:railcam.photo.id=" . $photo_id; #deleteMemcacheObject($mckey); if ($return = getMemcacheObject($mckey)) { $return['photo']['time_relative'] = relative_date($return['photo']['dateuploaded']); return $return; } else { $use_rezzza = false; if ($use_rezzza) { $metadata = new Metadata(RP_FLICKR_API_KEY, RP_FLICKR_API_SECRET); $metadata->setOauthAccess($this->flickr_oauth_token, $this->flickr_oauth_secret); $factory = new ApiFactory($metadata, new GuzzleAdapter()); $photo_info = $factory->call('flickr.photos.getInfo', array('photo_id' => $photo_id)); if ($photo_info) { $photo_sizes = $factory->call('flickr.photos.getSizes', array('photo_id' => $photo_id)); } /** * do stuff! */ if ($photo_info && $photo_sizes) { $return = array(); /** * Photo info */ foreach ($photo_info->photo->attributes() as $a => $b) { $return['photo'][$a] = $b->__toString(); } foreach ($photo_info->photo->children() as $element) { foreach ($element->attributes() as $a => $b) { $return['photo'][$element->getName()][$a] = $b->__toString(); } foreach ($element->children() as $child) { foreach ($child->attributes() as $a => $b) { $return['photo'][$element->getName()][$child->getName()][$a] = $b->__toString(); } foreach ($child->children() as $blah) { $return['photo'][$element->getName()][$child->getName()][$blah->getName()][$a] = $b->__toString(); foreach ($blah->attributes() as $a => $b) { $return['photo'][$element->getName()][$child->getName()][$blah->getName()][$a] = $b->__toString(); } } } } /** * Photo sizes */ $i = 0; foreach ($photo_sizes->sizes->size as $key => $element) { foreach ($element->attributes() as $a => $b) { $return['photo']['sizes'][$i][$a] = $b->__toString(); } $i++; } } return $return; } $f = new flickr_railpage(RP_FLICKR_API_KEY); $f->oauth_token = $this->flickr_oauth_token; $f->oauth_secret = $this->flickr_oauth_secret; $f->cache = false; $return = array(); if ($return = $f->photos_getInfo($photo_id)) { $return['photo']['sizes'] = $f->photos_getSizes($photo_id); $return['photo']['time_relative'] = relative_date($return['photo']['dateuploaded']); setMemcacheObject($mckey, $return, strtotime("+2 hours")); } return $return; } }