⬆️ ⬇️

Class library for working with the Yandex.Foto API service

The library will allow you to use the Yandex.Photo API service in projects written in PHP and covers all the features that the API currently provides.

The page that is dedicated to the work of the library: code.websaints.net



Using this library you can:



The Yandex.Fotki service should be used because:



Under the cat you will find an example of the work and its code, supplied with brief comments.





Example



Gallery of favorite photos of Vladislav Mamontovich

')

Immediately make a reservation:

- No, you can not create image hosting on the basis of this service, as it will violate clause 1.2.4. User Agreement for the Yandex.Photo API. The example chosen precisely for reasons of clarity, and is ... not entirely correct. I hope that Yandex employees will treat him condescendingly. Or, at least, they will warn you before blocking the unfortunate Vladislav Mamontovich.



The example consists of 2 files:

  1. example.html - gallery display
  2. upload_image.html - upload images


I will not focus attention on anything other than the code, where the work with Yandex.Fotki is in progress. If you have questions related to some code - ask them in the comments. I will answer.



example.html



What key features this page should implement:





1. Create a user on whose behalf we will work with the service. Namely - Vladislav Mamontovich.



  1. $ photo = new yandex_fotki ( "MojoElephant" ) ;




2. Get a general collection of photographs Mamontovich



  1. $ photo -> user ( ) -> add_photo_collection ( "Elephant photo" ) ;




3. Get the last 25 photos uploaded by Mamontovich



  1. $ photo -> user ( ) -> photo_collection ( "Slonofoot" ) -> se ( array ( "limit" => 25 ) ) ;




4. After going through them all we will form a gallery



  1. foreach ( $ photo -> user ( ) -> photo_collection ( "Elephant photos" ) -> photo_list ( 0 ) as $ index => $ photo_element ) {
  2. $ photo_block = new template_class ( "Photo" , "../../templates/photo.html" ) ;
  3. $ photo_block -> point ( "Entry Points" ) ;
  4. $ photo_block -> plug ( "Entry Points" , "Enlarged" , "Enlarged" , $ photo_element -> get_L ( ) ) ;
  5. $ photo_block -> plug ( "Input Points" , "Preview" , "Preview" , $ photo_element -> get_XXS ( ) ) ;
  6. $ gallery -> plug ( "Entry Points" , "Photography" , $ index , $ photo_block -> render ( ) ) ;
  7. }




5. If Mamontovich uploaded a photo, then the ID of the new photo was transferred to the script. We get a freshly uploaded photo by its id



  1. $ new_photo = $ photo -> user ( ) -> photo_collection ( "Elephant photo" ) -> get_photo_by_id ( $ photo_id ) ;




6. Display a list of links



  1. $ link_block = new template_class ( "Links" , "../../templates/link_block.html" ) ;
  2. $ link_block -> point ( "Entry Points" ) ;
  3. $ link_block -> plug ( "Entry Points" , "Original" , "Link" , $ new_photo -> get_orig ( ) ) ;
  4. $ link_block -> plug ( "Entry Points" , "XL" , "Link" , $ new_photo -> get_XL ( ) ) ;
  5. $ link_block -> plug ( "Entry Points" , "L" , "Link" , $ new_photo -> get_L ( ) ) ;
  6. $ link_block -> plug ( "Entry Points" , "M" , "Link" , $ new_photo -> get_M ( ) ) ;
  7. $ link_block -> plug ( "Entry Points" , "S" , "Link" , $ new_photo -> get_S ( ) ) ;
  8. $ link_block -> plug ( "Entry Points" , "XS" , "Link" , $ new_photo -> get_XS ( ) ) ;
  9. $ link_block -> plug ( "Entry Points" , "XXS" , "Link" , $ new_photo -> get_XXS ( ) ) ;
  10. $ link_block -> plug ( "Entry Points" , "XXXS" , "Link" , $ new_photo -> get_XXXS ( ) ) ;




upload_image.html



What key features this page should implement:



1. Since uploading photos is possible only for an authenticated user, we create an object of class yandex_fotki and authenticate it.



  1. $ photo = new yandex_fotki ( "MojoElephant" , "password" ) ;




2. After that, you need to get a collection of photos, in which we will add a new photo of Mamontovich.



  1. $ photo -> user ( ) -> add_photo_collection ( "Elephant photo" ) ;




3. And, in fact, upload a photo to Yandex. Photos. After uploading, we’ll get a photo ID.



  1. $ new_photo = array (
  2. "path" => "../../uploaded/" . $ _FILES [ "photo" ] [ "name" ]
  3. ) ;
  4. $ result = $ photo -> user ( ) -> photo_collection ( "Elephant photos" ) -> up ( $ new_photo ) ;




4. Save the photo ID. You can use any method, at least get it. For this purpose, I use a message system that stores data in a session.



5. We send Mamontovich to example.html, where he will receive links to the uploaded photo and see it in the gallery.



Conclusion



As you can see, everything is extremely simple and pleasant.

Using a minimum of effort, you can embed a gallery on the site.

The only disadvantage of storing photos on Yandex. Photos is that the service takes longer to request metadata than it takes to store them on your service. But sensible use of caching almost nullifies it.



Materials for additional study





thank



Many thanks to ar2r and nickmitin for help with porting the encryption algorithm, proto for clarifying the Yandex.FotI API project, comments and everything to everyone all for their time. And at least a big thank you to those people from the #php RusNet channel who gave my work an expert assessment and advised what can be done better. mz, avz, focusshifter and everything else. Thanks you.



The code is highlighted by highlight.hohli.com which came up with antonshevchuk





What's next?



Next will be searching and correcting errors, refactoring code, writing more detailed articles on the use of the library and the ideology of the work of Yandek. The stream, perhaps, the creation of plug-ins for several CMS. Yes, actually, everything that will be necessary and interesting for me and you.

But in order to continue to have at least something I need to know that someone needs my work. If the library can be useful for you - write about it. Do not be silent.



PS If you like the library and you think about the participation of DEVCONF - vote for my report on the conference website: www.devconf.ru/phpconf/offers

Source: https://habr.com/ru/post/88666/



All Articles