|
| 1 | +phpZenfolio 1.3 - PHP Wrapper for the Zenfolio API |
| 2 | +================================================== |
| 3 | + |
| 4 | +Written by Colin Seymour |
| 5 | +Project Homepage: [phpzenfolio.com](http://phpzenfolio.com/) |
| 6 | + |
| 7 | +phpZenfolio is a PHP wrapper class for the Zenfolio API and is based on work I |
| 8 | +have done in [phpSmug](http://phpsmug.com). |
| 9 | + |
| 10 | +Released under [GNU General Public License version 3](http://www.gnu.org/licenses/gpl.html) |
| 11 | + |
| 12 | +Copyright (C) 2010 Colin Seymour |
| 13 | + |
| 14 | +For more information about the class and upcoming tools and applications using |
| 15 | +phpZenfolio, visit [phpzenfolio.com](http://phpzenfolio.com/). |
| 16 | + |
| 17 | + |
| 18 | +## Requirements |
| 19 | + |
| 20 | +phpZenfolio is written in PHP and utilises functionality supplied with PHP 5.2 and later and optionally PEAR. |
| 21 | + |
| 22 | +From a PHP perspective, the only requirement is PHP 5.2 compiled with GD and optionally, curl support enabled. |
| 23 | + |
| 24 | +If you wish to use a database for caching, you will also need the following PEAR packages: |
| 25 | + |
| 26 | +* [MDB2 2.5.0b3](http://pear.php.net/package/MDB2) or later. |
| 27 | + |
| 28 | +* The corresponding [MDB2\_Driver_*](http://pear.php.net/search.php?q=MDB2_Driver&in=packages&setPerPage=20) for the database you wish to use. |
| 29 | + |
| 30 | +Please consult the above links for details on installing the PEAR modules. |
| 31 | + |
| 32 | + |
| 33 | +## Installation |
| 34 | + |
| 35 | +Copy the files from the installation package into a folder on your server. They need to be readable by your web server. You can put them into an include folder defined in your `php.ini` file, if you like, though it's not required. |
| 36 | + |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +In order to use phpZenfolio, you will need to instantiate a new instance of the phpZenfolio object and then call the methods you need. phpZenfolio implements all methods and arguments as documented in the Zenfolio API. Accordingly, when calling a Zenfolio method, you will need to ensure that you pass the parameters exactly as they are documented in the [Zenfolio API documentation](http://http://www.zenfolio.com/zf/help/api). |
| 41 | + |
| 42 | +Remember: *ALL* function names and arguments *ARE* case sensitive and the order is important. |
| 43 | + |
| 44 | +To make things easy for developers, phpZenfolio also provides several of it's own methods. These methods are: `login()`, `enableCache()`, `clearCache()`, `upload()`, `setProxy()`, `imageUrl()` and `setAdapter()`. All phpZenfolio methods, and its constructor, take their arguments either as an associative array or as a list of `param=value` strings, unless otherwise documented. These methods are documented in more detail later in this document. |
| 45 | + |
| 46 | +To use phpZenfolio, all you have to do is include the file in your PHP scripts and create an instance. For example: |
| 47 | + |
| 48 | +``` |
| 49 | +require_once("phpZenfolio/phpZenfolio.php"); |
| 50 | +$f = new phpZenfolio(... arguments go here ...); |
| 51 | +``` |
| 52 | + |
| 53 | +The constructor takes two arguments, one obligatory and one optional: |
| 54 | + |
| 55 | +* `AppName` - Required |
| 56 | + |
| 57 | + This is the name, version and URL of the application you have built using phpZenfolio. There is no required format, but something like: |
| 58 | + |
| 59 | + ``` |
| 60 | + "My Cool App/1.0 (http://my.url.com)" |
| 61 | + ``` |
| 62 | + |
| 63 | + ... would be very useful and will allow Zenfolio to identify your application in the event of a problem. |
| 64 | + |
| 65 | +* `APIVer` - Optional |
| 66 | + Default: 1.8 |
| 67 | + |
| 68 | + Use this to set the endpoint you wish to use. The default is 1.8 as this is the latest endpoint provided by Zenfolio. |
| 69 | + |
| 70 | +As the constructor is a phpZenfolio specific method, it can be instantiated using one of the following methods: |
| 71 | + |
| 72 | +* Arguments as strings: |
| 73 | + |
| 74 | + ``` |
| 75 | + $f = new phpZenfolio("AppName=My Cool App/1.0 (http://app.com)", "APIVer=1.8"); |
| 76 | + ``` |
| 77 | + |
| 78 | +* Arguments as an associative array: |
| 79 | + |
| 80 | + ``` |
| 81 | + $f = new phpZenfolio(array( |
| 82 | + "AppName" => "My Cool App/1.0 (http://app.com)", |
| 83 | + "APIVer" => "1.8") |
| 84 | + ); |
| 85 | + ``` |
| 86 | + |
| 87 | +Naturally, you can predefine the array before instantiating the object and just pass the array variable. |
| 88 | + |
| 89 | +With the instance instantiated, you can now interact with the Zenfolio API using Zenfolio's native methods exactly as they're documented. Arguments to all Zenfolio native methods must be provided in the order they are documented in the API documentation. For example, use the following to get all recent sets that are of the PhotoSetType 'Gallery': |
| 90 | + |
| 91 | + ``` |
| 92 | + $f->GetRecentSets('Gallery', 0, 3); |
| 93 | + ``` |
| 94 | + |
| 95 | +Note the method's capitalisation and the arguments, these are as they are documented in the [`GetRecentSets()`](http://www.zenfolio.com/zf/help/api/ref/methods/getrecentsets) method documentation. |
| 96 | + |
| 97 | +Some of the Zenfolio API methods, like `CreatePhotoSet()`, require an object to be passed as one of the arguments. The object can be passed either as an associative array: |
| 98 | + |
| 99 | + ``` |
| 100 | + $photoSetUpdater = array( |
| 101 | + "Title" => "PhotoSet Title", |
| 102 | + "Caption" => "PhotoSet Caption via API", |
| 103 | + "Keywords" => array("Keyword1", "keyword2"), |
| 104 | + "Categories" => array(), |
| 105 | + "CustomReference" => "testing/test-photoset" |
| 106 | + ); |
| 107 | + $f->CreatePhotoSet(12345, 'Gallery', $photoSetUpdater); |
| 108 | + ``` |
| 109 | + |
| 110 | +... or as a standard class object: |
| 111 | + |
| 112 | + ``` |
| 113 | + $photoSetUpdater = new stdClass(); |
| 114 | + $photoSetUpdater->Title = "PhotoSet Title"; |
| 115 | + $photoSetUpdater->Caption = "PhotoSet Caption via Object"; |
| 116 | + $photoSetUpdater->Keywords = array("Keyword1", "keyword2"); |
| 117 | + $photoSetUpdater->Categories = array(); |
| 118 | + $photoSetUpdater->CustomReference = "testing/test-photoset"; |
| 119 | + $f->CreatePhotoSet(12345, 'Gallery', $photoSetUpdater); |
| 120 | + ``` |
| 121 | + |
| 122 | +All data returned by the method call is returned as the API documents it with the exception being objects are actually returned as arrays by phpZenfolio. In the event of an error, phpZenfolio will throw one of two exceptions: `PhpZenfolioException` in the event of a problem detected by phpZenfolio or `HttpRequestException` in the event of a problem detected by the code used to communicate with the API. Your application will need to catch these exceptions. |
| 123 | + |
| 124 | + |
| 125 | +## Authentication |
| 126 | + |
| 127 | +Many of the Zenfolio API methods are open to all users, whether they have Zenfolio accounts or not, however anything private or related to modification requires authentication. |
| 128 | + |
| 129 | +The Zenfolio API provides [two methods of authentication](http://www.zenfolio.com/zf/help/api/guide/auth): Plain-Text and Challenge-Response. Both are equally good with the slightly more secure method being the Challenge-Response method as your password never travels across the wire. |
| 130 | + |
| 131 | +phpZenfolio allows you to use the API methods as documented, however to make things easy, a single `login()` method exists to allow you to authentication using either of these authentication methods: |
| 132 | + |
| 133 | +* Challenge-Response (default): |
| 134 | + |
| 135 | + ``` |
| 136 | + $f->login("Username=<username>", "Password=<password>"); |
| 137 | + ``` |
| 138 | + |
| 139 | +* Plain-Text: |
| 140 | + |
| 141 | + ``` |
| 142 | + $f->login("Username=<username>", "Password=<password>", "Plaintext=TRUE"); |
| 143 | + ``` |
| 144 | + |
| 145 | +The Plain-Text method uses HTTPS/SSL for the authentication step to ensure your username and password are encrypted when transmitted to Zenfolio. |
| 146 | + |
| 147 | + |
| 148 | +## Caching |
| 149 | + |
| 150 | +Caching can be very important to a project as it can drastically improve the performance of your application. |
| 151 | + |
| 152 | +phpZenfolio has caching functionality that can cache data to a database or files, you just need to enable it. |
| 153 | + |
| 154 | +It is recommended that caching is enabled immediately after a new phpZenfolio instance is created, and before any other phpZenfolio methods are called. |
| 155 | + |
| 156 | +To enable caching, use the `enableCache()` function. |
| 157 | + |
| 158 | +The `enableCache()` method takes 4 arguments: |
| 159 | + |
| 160 | +* `type` - Required |
| 161 | + |
| 162 | + This is "db" for database or "fs" for filesystem. |
| 163 | + |
| 164 | +* `dsn` - Required for `type=db` |
| 165 | + |
| 166 | + This a PEAR::MDB2 DSN connection string, for example: |
| 167 | + |
| 168 | + ``` |
| 169 | + mysql://user:password@server/database |
| 170 | + ``` |
| 171 | + |
| 172 | +phpZenfolio uses the MDB2 PEAR module to interact with the database if you use database based caching. phpZenfolio does *NOT* supply the necessary PEAR modules. If you with to use a database for caching, you will need to download and install PEAR, the MDB2 PEAR module and the corresponding database driver yourself. See [MDB2 Manual](http://pear.php.net/manual/en/package.database.mdb2.intro.php) for details. |
| 173 | + |
| 174 | +* `cache_dir` - Required for `type=fs` |
| 175 | + |
| 176 | + This is the folder/directory that the web server has write access to. This directory must already exist. |
| 177 | + |
| 178 | +Use absolute paths for best results as relative paths may have unexpected behaviour. They'll usually work, you'll just want to test them. |
| 179 | + |
| 180 | +You may not want to allow the world to view the files that are created during caching. If you want to hide this information, either make sure that your permissions are set correctly, or prevent the webserver from displaying `*.cache` files. |
| 181 | + |
| 182 | +In Apache, you can specify this in the configuration files or in a .htaccess file with the following directives: |
| 183 | + |
| 184 | +``` |
| 185 | +<FilesMatch "\.cache$"> |
| 186 | + Deny from all |
| 187 | +</FilesMatch> |
| 188 | +``` |
| 189 | + |
| 190 | +Alternatively, you can specify a directory that is outside of the web server's document root. |
| 191 | + |
| 192 | +* `cache_expire` - Optional |
| 193 | + Default: 3600 |
| 194 | + |
| 195 | + This is the maximum age of each cache entry in seconds. |
| 196 | + |
| 197 | +* `table` - Optional |
| 198 | + Default: `phpzenfolio_cache` |
| 199 | + |
| 200 | + This is the database table name that will be used for storing the cached data. This is only applicable for database (db) caching and will be ignored if included for filesystem (fs) caching. |
| 201 | + |
| 202 | + If the table does not exist, phpZenfolio will attempt to create it. |
| 203 | + |
| 204 | +Each of the caching methods can be enabled as follows: |
| 205 | + |
| 206 | +* Filesystem based cache: |
| 207 | + |
| 208 | + ``` |
| 209 | + $f->enableCache("type=fs", "cache_dir=/tmp", "cache_expire=86400"); |
| 210 | + ``` |
| 211 | + |
| 212 | +* Database based cache: |
| 213 | + |
| 214 | + ``` |
| 215 | + $f->enableCache("type=db", "dsn=mysql://USERNAME:PASSWORD_database", "cache_expire=86400"); |
| 216 | + ``` |
| 217 | + |
| 218 | +If you have caching enabled, and you make changes, it's a good idea to call `clearCache()` to refresh the cache so your changes are reflected immediately. |
| 219 | + |
| 220 | + |
| 221 | +## Uploading |
| 222 | + |
| 223 | +Uploading is very easy. You can either upload an image from your local system using the phpZenfolio supplied `upload()` method, or from a location on the web using the API's `CreatePhotoFromUrl()` method. |
| 224 | + |
| 225 | +* Upload Local File: |
| 226 | + |
| 227 | + To upload from your local filesystem using the phpZenfolio `upload()` method, you will need to have logged into Zenfolio via the API and have the `PhotoSetId` or it's `UploadUrl` as returned by the API. |
| 228 | + |
| 229 | + Then it's just a matter of calling the method with the various optional parameters. |
| 230 | + |
| 231 | + * Upload using the PhotoSetId: |
| 232 | + |
| 233 | + ``` |
| 234 | + $f->upload("PhotoSetId=123456", "File=/path/to/image.jpg"); |
| 235 | + ``` |
| 236 | +
|
| 237 | + * Upload using the UploadUrl: |
| 238 | +
|
| 239 | + ``` |
| 240 | + $f->upload("UploadUrl=http://up.zenfolio.com/....", |
| 241 | + "File=/path/to/image.jpg"); |
| 242 | + ``` |
| 243 | +
|
| 244 | + At this time, the only supported options you can pass at the time of uploading are a `filename` the `modified` parameter which takes a RFC2822 formatted date string... |
| 245 | +
|
| 246 | + ``` |
| 247 | + $f->upload("PhotoSetId=123456", |
| 248 | + "File=/path/to/image.jpg", |
| 249 | + "filename=newfilename.jpg", |
| 250 | + "modified=Thu, 14 Jan 2010 13:08:07 +0200"); |
| 251 | + ``` |
| 252 | +
|
| 253 | + If you don't specify a filename, the original filename is used. |
| 254 | +
|
| 255 | +
|
| 256 | +* Upload from the web: |
| 257 | +
|
| 258 | + Uploading to Zenfolio using a URL is done purely by the Zenfolio `CreatePhotoFromUrl()` API method: |
| 259 | +
|
| 260 | + ``` |
| 261 | + $f->CreatePhotoFromUrl(12344, "http://www.example.com/images/image.jpg", null); |
| 262 | + ``` |
| 263 | +
|
| 264 | + You can find full details on the options this method accepts in the [CreatePhotoFromUrl](http://www.zenfolio.com/zf/help/api/ref/methods/createphotofromurl) method documentation. |
| 265 | +
|
| 266 | + Unfortunately, there is no way to pass things like the photo title etc at the time of upload. You will need to set these later using the `UpdatePhoto()` method. |
| 267 | +
|
| 268 | +
|
| 269 | +## Replacing Photos |
| 270 | +
|
| 271 | +In order to replace a photo, you will need to upload a new photo and then replace the old photo with the new using the Zenfolio [`ReplacePhoto()`](http://www.zenfolio.com/zf/help/api/ref/methods/replacephoto) API method. |
| 272 | +
|
| 273 | +
|
| 274 | +
|
| 275 | +## Other Notes |
| 276 | +
|
| 277 | +* By default, phpZenfolio will attempt to use Curl to communicate with the |
| 278 | + Zenfolio API endpoint if it's available. If not, it'll revert to using |
| 279 | + sockets based communication using `fsockopen()`. If you wish to force the |
| 280 | + use of sockets, you can do so using the phpZenfolio supplied |
| 281 | + `setAdapter()` right after instantiating your instance: |
| 282 | +
|
| 283 | + ``` |
| 284 | + $f = new phpZenfolio("AppName=<value>"); |
| 285 | + $f->setAdapter("socket"); |
| 286 | + ``` |
| 287 | +
|
| 288 | + Valid arguments are "curl" (default) and "socket". |
| 289 | +
|
| 290 | +* phpZenfolio implements [Zenfolio objects](http://www.zenfolio.com/zf/help/api/ref/objects) as arrays. This makes implementation and usage easier. |
| 291 | +
|
| 292 | +* Some people will need to use phpZenfolio from behind a proxy server. You can use the `setProxy()` method to set the appropriate proxy settings. |
| 293 | +
|
| 294 | + For example: |
| 295 | +
|
| 296 | + ``` |
| 297 | + $f = new phpZenfolio("AppName=<value>"); |
| 298 | + $f->setProxy("server=<proxy_server>", "port=<value>"); |
| 299 | + ``` |
| 300 | +
|
| 301 | + All your calls will then pass through the specified proxy on the specified port. |
| 302 | +
|
| 303 | + If your proxy server requires a username and password, then add those options to the `setProxy()` method arguments too. |
| 304 | +
|
| 305 | + For example: |
| 306 | +
|
| 307 | + ``` |
| 308 | + $f = new phpZenfolio("AppName=<value>"); |
| 309 | + $f->setProxy("server=<proxy_server>", |
| 310 | + "port=<value>", |
| 311 | + "user=<proxy_username>", |
| 312 | + "password=<proxy_password>"); |
| 313 | + ``` |
| 314 | +
|
| 315 | + Note: Proxy support is currently only available when using the default "curl" adapter. |
| 316 | +
|
| 317 | +* To make it easy to obtain the direct URL to an image, phpZenfolio supplies a `imageURL()` method that takes the Photo object as returned by methods like `LoadPhoto()` and `LoadPhotoSetPhotos()` and an integer for the desired photo size where the integer is one of those documented at http://www.zenfolio.com/zf/help/api/guide/download . |
| 318 | +
|
| 319 | + For example: |
| 320 | +
|
| 321 | + ``` |
| 322 | + $f = new phpZenfolio("AppName=<value>"); |
| 323 | + $photos = $f->LoadPhotoSetPhotos(<photosetID>, <startingIndex>, <numberOfPhotos>); |
| 324 | + foreach ($photos as $photo) { |
| 325 | + echo '<img src="',phpZenfolio::imageUrl($photo, 1),'" />'; |
| 326 | + } |
| 327 | + ``` |
| 328 | +
|
| 329 | +* If phpZenfolio encounters an error, or Zenfolio returns a "failure" response, an exception will be thrown and your application will stop executing. If there is a problem with communicating with the endpoint, a HttpRequestException will be thrown. If an error is detected elsewhere, a PhpZenfolioException will be thrown. |
| 330 | +
|
| 331 | + It is recommended that you configure your application to catch exceptions from phpZenfolio. |
| 332 | +
|
| 333 | +
|
| 334 | +## Examples |
| 335 | +
|
| 336 | +phpZenfolio comes with 3 examples to help get you on your way. |
| 337 | +
|
| 338 | +* `example-popular.php` illustrates how to obtain the 100 most popular galleries and display their title image linking to each individual gallery. |
| 339 | +
|
| 340 | +* `example-login.php` illustrates how to login and display the images in your first photoset or collection. |
| 341 | +
|
| 342 | +* `example-user.php` illustrates how to display the first 100 public photos of the user's the first public photoset found. |
| 343 | +
|
| 344 | +And that's all folks. |
| 345 | +
|
| 346 | +Keep up to date on developments and enhancements to phpZenfolio at |
| 347 | +[phpzenfolio.com](http://phpzenfolio.com/). |
| 348 | +
|
| 349 | +If you encounter any problems with phpZenfolio, please check the list of known |
| 350 | +issues with phpZenfolio in the GitHub repository at https://github.com/lildude/phpZenfolio/issues . |
| 351 | +If your issue is not there, please feel free to open a new issue and provide as many details as you can. |
| 352 | +
|
| 353 | +This document is also available online at [phpzenfolio.com/docs](http://phpzenfolio.com/docs). |
| 354 | +
|
| 355 | +
|
| 356 | +### Change History |
| 357 | +
|
| 358 | +* 1.3 - 14 Apr '16 |
| 359 | + * Changed default API version to 1.8 and made sure the rest of the library takes this API version into account. |
| 360 | + * Changed the README to use Markdown instead of plaintext. |
| 361 | +
|
| 362 | +* 1.2 - 10 June '12 |
| 363 | + * Changed API endpoint to use api.zenfolio.com as requested by Zenfolio |
| 364 | + * Changed default API version to 1.6 |
| 365 | + * Added ability to perform ALL API requests over HTTPS (Ticket #4) |
| 366 | +
|
| 367 | +* 1.1 - 28 Mar '11 |
| 368 | + * Use md5 to generate a uniq ID for each request instead of using `intval()` to ensure correct and consistent behaviour on 32-bit and 64-bit platforms. (Ticket #1) |
| 369 | + * Removed erroneous re-instantiation of processor when setting adapter. |
| 370 | + * Corrected check for safe_dir OR open_basedir so fails over to socket connection correctly (Ticket #2) |
| 371 | + * Cache only successful requests |
| 372 | + * Improved connection settings |
| 373 | +
|
| 374 | +* 1.0 - 01 Oct '10 |
| 375 | + * Initial release. |
0 commit comments