bubnov/yandex-direct-client

Yandex Direct API 的 PHP 库

1.0.2 2016-04-15 13:20 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:46:06 UTC


README

需要 curl 扩展!

用于通过其 API 操作 Yandex.Direct 服务的包。

  • 已准备好通过 Composer 使用
  • 定义了自动加载器(根据 psr-4)
  • 提供所有 API 方法
  • 在发送前,通过 json-schema 对输入数据进行验证(数据库填充)
  1. 安装 Composer
composer require bubnov/yandex-direct-client
  1. 使用
<?php
require 'vendor/autoload.php';

$authKey = 'MY-AUTH-TOKEN';

$client = new YandexDirectClient\Client($authKey);

try {
    /**
     * Getting Units for users
     */
    $response = $client->GetClientsUnits(['my-user','customer-user']);
    
    /**
     * If Response is an array - you can access to each element as in array
     */
    foreach($response as $item){
        /**
         * If the item has a property (see Yandex Direct API docs) - you can access it by getter
         */
        echo "\n" . $item->getLogin() . " has units: " . $item->getUnitsRest();
    }

    /**
     * Or you can access directly Nth element in array
     */
    echo "\nFirst user units: " . $response->get(0)->getUnitsRest();
    
    /**
     * Generating WordstatReport
     */
    $reportId = $client->CreateNewWordstatReport(['Phrases' => ['Купить холодильник', 'Холодильники недорого']]);
    echo "\nReportId: " . $reportId;
    
    /**
     * Wait for 10 seconds
     */
    sleep(10);
    
    /**
     * Getting full reports list
     */
    $reports = $client->GetWordstatReportList();
    foreach($reports as $report){
        /**
         * Find report with $reportId and status 'Done'
         */
        if($report->getReportID() == $reportId && $report->getStatusReport() === 'Done'){
            echo "\nReport is done, reading";
            break;
        }
    }
    
    /**
     * Get wordstat report by $reportId
     */
    $report = $client->GetWordstatReport($reportId);
    foreach($report as $reportPart){
        foreach($reportPart->getSearchedWith() as $searchedWith){
            echo sprintf(
                "\nPhrase `%s` has %d shows \n", 
                $searchedWith->getPhrase(), 
                $searchedWith->getShows()
            );
        }
    }
    
    /**
     * Deleting wordstat report
     */
    $status = $client->DeleteWordstatReport($reportId);
    echo $status ? "\nReport successfully deleted" : "\nSomething wrong...";
}
catch (\YandexDirectClient\Exceptions\YandexErrorException $e){
    echo "\nYandexErrorException: " . $e->getMessage() . "\nWith details: " . $e->getErrorDetail() . "\n";
}
catch (\Exception $e){
    echo "\nException: " . $e->getMessage() . "\n";
}
  1. 待办事项

为方法提供 json schema 扩展