-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.php
89 lines (77 loc) · 2.39 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
* Example of usage yandex-direct-client lib.
* @author Bubnov Mihail <[email protected]>.
*/
/*
* Use composer autoloader
*/
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 "\nGot YandexErrorException: " . $e->getMessage() . ", code: " . $e->getCode() . "\nWith details: " . $e->getErrorDetail() . "\n";
}
catch (\Exception $e){
echo "\nGot Exception: " . $e->getMessage() . "\n";
}
echo "\n";