Beispiel #1
0
 /**
  * Create an Api from settings saved into a file by saveAs().
  *
  * If the file does not exist, is invalid, or cannot be loaded, then you're
  * going to get an exception.
  *
  * The format of the file is:
  * <code>
  * api_key=0123456789abcdef0123456789abcedf
  * api_secret=abcedf0123456789
  * api_token=123-abcdef0123456789
  * cache_file=c:\temp\flickr.tmp
  * </code>
  * The token and cache filename settings are optional. The label of each
  * setting is defined by a constant in this class.
  *
  * @param   string $fileName Name of the file containing the saved Api
  *          settings.
  * @return  object Phlickr_Api
  * @since   0.2.4
  * @see     saveAs()
  * @uses    SETTING_API_KEY Label of the API key setting.
  * @uses    SETTING_API_SECRET Label of the API secret setting.
  * @uses    SETTING_API_TOKEN Label of the API token setting.
  * @uses    SETTING_API_CACHE Label of the cache filename setting.
  * @uses    setCacheFilename() to assign the cache filename if one is
  *          present in the settings file.
  */
 public static function createFrom($filename)
 {
     // create an array for the settings
     $config = array();
     // load the file
     $contents = file_get_contents($filename);
     // parse the key=value pairs into an associative array.
     preg_match_all('/([-_a-zA-Z]+)=(.+)/', $contents, $matches, PREG_SET_ORDER);
     foreach ($matches as $match) {
         $config[strtolower($match[1])] = $match[2];
     }
     // build an api object from the settings
     $api = new Phlickr_Api($config[self::SETTING_API_KEY], $config[self::SETTING_API_SECRET], $config[self::SETTING_API_TOKEN]);
     // set the cache filename
     if (isset($config[self::SETTING_API_CACHE])) {
         $api->setCacheFilename($config[self::SETTING_API_CACHE]);
     }
     return $api;
 }