Skip to content

Instantly share code, notes, and snippets.

@Retter241
Forked from andronex/yawebmaster.php
Created January 18, 2024 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Retter241/0b60791145fc09982b954191d16f5db2 to your computer and use it in GitHub Desktop.
Save Retter241/0b60791145fc09982b954191d16f5db2 to your computer and use it in GitHub Desktop.
Яндекс Вебмастер, добавление поддоменов в панель (юзать на MODX)
<?php
if (!isset($modx)) {
define('MODX_API_MODE', true);
while (!isset($modx) && ($i = isset($i) ? --$i : 10)) {
if (($file = dirname(!empty($file) ? dirname($file) : __FILE__) . '/index.php') AND !file_exists($file)) {
continue;
}
require_once $file;
}
if (!is_object($modx)) {
exit('{"success":false,"message":"Access denied"}');
}
$modx->getService('error', 'error.modError');
$modx->getRequest();
$modx->setLogLevel($is_debug ? modX::LOG_LEVEL_INFO : modX::LOG_LEVEL_ERROR);
$modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
$modx->error->reset();
$modx->lexicon->load('default');
$modx->lexicon->load('minishop2:default');
$modx->lexicon->load('minishop2:manager');
}
//функ-ия коннекта к Я.Маркету, ключ урезан
function httpRequestYa($url, $data = [], $method = 'GET'){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization: OAuth y0_AgAAAABpUvNMAAlNtAA8ZV4RAomdGa8'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$returned = curl_exec($ch);
curl_close ($ch);
return $returned;
}
$q = $modx->newQuery('modContext');
$q->where(['key:NOT IN' => ['moscow', 'mgr', 'orel', 'abakan', 'almetevsk']]);
$q->sortby('name','ASC');
$ctxs = $modx->getIterator('modContext', $q); //условия по потребности
foreach($ctxs as $ctx){
$settings = $ctx->getMany('ContextSettings');
foreach($settings as $set){
switch($set->key){
case 'site_url':
$url = rtrim($set->value, '/');
break;
}
}
// ID_USER заменить на числовое ID юзера
$response = httpRequestYa('https://api.webmaster.yandex.net/v4/user/ID_USER/hosts',
array('host_url' => $url),
'POST'
);
if($response){
$response = json_decode($response, true);
print_r($response);
if($response['host_id']){
$host_id = $response['host_id'];
$response = httpRequestYa('https://api.webmaster.yandex.net/v4/user/ID_USER/hosts/'.$host_id.'/verification');
if($response){
$response = json_decode($response, true);
print_r($response);
if($response['verification_uin']){
$verification_uin = $response['verification_uin'];
$settings = $ctx->getMany('ContextSettings');
$newSetting = $modx->newObject('modContextSetting');
$newSetting->fromArray(array('context_key' => $ctx->get('key'), 'value' => $verification_uin));
$newSetting->set('key', 'yandex_verification');
if($newSetting){
$settings[] = $newSetting;
$ctx->addMany($settings);
$ctx->save();
$modx->cacheManager->deleteTree(MODX_CORE_PATH.'cache/context_settings/'.$ctx->get('key').'/',array(
'skipDirs' => true,
'deleteTop' => false,
'extensions' => array('.cache.php')
));
$modx->cacheManager->deleteTree(MODX_CORE_PATH.'cache/resource/'.$ctx->get('key').'/',array(
'skipDirs' => true,
'deleteTop' => false,
'extensions' => array('.cache.php')
));
httpRequestYa('https://api.webmaster.yandex.net/v4/user/ID_USER/hosts/'.$host_id.'/verification?verification_type=META_TAG',
[],
'POST'
);
}
}
}
}
}
// break;
}
/*print_r(
httpRequestYa('https://api.webmaster.yandex.net/v4/user/ID_USER/hosts',
array('host_url' => 'https://orel.natural-floor.ru'),
'POST'
)
);*/
/*print_r(
httpRequestYa('https://api.webmaster.yandex.net/v4/user/ID_USER/hosts/https:orel.natural-floor.ru:443/verification'
)
);*/
/*print_r(
httpRequestYa('https://api.webmaster.yandex.net/v4/user/ID_USER/hosts/https:orel.natural-floor.ru:443/verification?verification_type=META_TAG',
[],
'POST'
)
);*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment