New PHP class to work with webmaster API
Recently a PHP class that implements all Yandex.Webmaster API interfaces appeared for public access. This class will help to migrate to the new API quicker to those who already have experience with the old one or simply want to integrate the desired API functions in your own admin services.
You can find it at https://github.com/yandex/webmaster.api . Keep in mind that you will need curl and json extensions in order for it to work correctly. Inside the class is a webmaster_api.class.php file, which can be used for work.
An example of how to work with the API (example folder).
You'll also find the example folder which contains the functioning code, lay it out and you will be able to assess the practical example of API work. At this time, it features nearly complete functionality, which is available through the API.
To see an example of how it works, you will need to do two things:
- Create a new application for "Yandex.Webmaster" project https://oauth.yandex.ru/client/new . When creating the application, be sure to check both of the available checkboxes related to access rights. The address of where the sample code is located select in the callback url field.
- The example/config.example.php file you will need to copy in example/config.php, do not forget to fill in the variables $client_id и $client_secret, having entered the data which you received after the previous paragraph.
How the class itself functions:
- It is necessary to pass the access token during the initialization of the class. To get the tokens in lie with the code a static method webmasterApi::getAccessToken was used.
- First instruments of the class are an interface to work with the API. It’s get, post, delete and additional methods used inside. All methods work in line with the usual principle – you should indicate the API handle, to which you wish to connect to, as well as an array of variables that we have to pass. We should make a request to the API, and the result appears. If something is wrong, the API will return the object with error_code и error_message fields.
- It is followed by the method, which is responsible for the preparation of certain data through the API. There is no point to list them in details - they are commented directly in the code.
In general, it is all you need to know - anyone who even slightly knows php, will be able to handle things quickly.
Note that this code was originally written for internal API testing. Therefore, if you have found an error or malfunctioning, it is best to try to fix everything yourself. Do not be lazy and send in comments about the fixes into the repository - it's easy and can be very useful to others.











