Answer the question
In order to leave comments, you need to log in
Why does the script not work on Kron?
Hello everyone)
Do not judge, I found a lot of such questions, but I did not find the answer.
I made a PHP script in Bitrix that bypasses the infoblock and, when an element appears, adds a post to VK, and then adds the VK_ID property.
If I run through the browser and from anonymous and from the admin, everything works out perfectly. But when I add a hoster to the cron scheduler, it does not work.
in the scheduler I write:
GET ' https://reg-inet.ru/test/vk.php?key_post=7777 '
u0462200$ GET ' https://reg-inet.ru/test/vk.php?key_post=7777 ' >/dev/null 2>&1
<?
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");
$site_url="https://".$_SERVER['SERVER_NAME'];
$token_public = "7777";
if (isset($_GET['key_post']))
{
if ($_GET['key_post'] == $token_public)
{
$access_token = 'ключ VK';
$version = '5.122';
$arFields = "";
$date = date('Y-m-d');
$timestamp = MakeTimeStamp($date, 'YYYY-MM-DD');
$convertDateTime = ConvertTimeStamp($timestamp);
$arSelect = Array("ID",
"NAME",
"DATE_ACTIVE_FROM",
"DETAIL_PICTURE",
"DATE_CREATE",
"PROPERTY_VK_ID",
"PROPERTY_zp",
"PROPERTY_gorod",
"PROPERTY_MORE_PHOTO",
"PROPERTY_organization",
"PROPERTY_adres_raboty",
"PROPERTY_opyt",
"PROPERTY_zanyatost",
"PROPERTY_grafik",
"PROPERTY_obyazannosti",
"PROPERTY_trebovaniya",
"PROPERTY_usloviya",
"PROPERTY_tel",
"PROPERTY_kontakt",
"CANONICAL_PAGE_URL"
);
$el = new CIBlockElement;
$arFilter = Array("IBLOCK_ID"=>16, "PROPERTY_VK_ID" => false, "ACTIVE" => "Y", ">=DATE_ACTIVE_FROM" => $convertDateTime);
$res = CIBlockElement::GetList(Array(), $arFilter, false, Array("nPageSize"=>100), $arSelect);
while($ob = $res->GetNextElement())
{
$arFields = $ob->GetFields();
$photoPath = CFile::GetPath($arFields["DETAIL_PICTURE"]);
$arProps = $ob->GetProperties();
$link = $arFields['CANONICAL_PAGE_URL'];
$group_id = '132737239';
$photoPath = CFile::GetPath($arFields["DETAIL_PICTURE"]);
//2. УЗНАЕМ СЕРВЕР ДЛЯ КАРТИНКИ
$url1 = 'https://api.vk.com/method/photos.getWallUploadServer?group_id='.$group_id.'&access_token='.$access_token.'&v=' . $version;
$oServer = json_decode(file_get_contents($url1));
//3. ОТПРАВКА POST КАРТИНКИ НА СЕРВЕР ВКОНТАКТЕ
$data = array("photo"=>new CurlFile($_SERVER['DOCUMENT_ROOT'].$photoPath));
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $oServer->response->upload_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
$response = json_decode($response);
curl_close($ch);
//4. СОХРАНЕНИЕ ФОТО ВКОНТАКТЕ
$url2 = 'https://api.vk.com/method/photos.saveWallPhoto?server='.$response->server.'&group_id='.$group_id.'&photo='.$response->photo.'&hash='.$response->hash.'&access_token='.$access_token.'&v=' . $version;
$oUrl2 = json_decode(file_get_contents($url2));
$photoID = $oUrl2->response[0]->id;
$message = $arFields['NAME']. "\n" . 'Зарплата: '. $arFields['PROPERTY_ZP_VALUE']." руб". "\n" ."Адрес работы: ".$arFields['PROPERTY_ADRES_RABOTY_VALUE']. "\n" ."Опыт работы: ". $arFields['PROPERTY_OPYT_VALUE']. "\n" ."Занятость: ". $arFields['PROPERTY_ZANYATOST_VALUE']. "\n" . "График работы: ". $arFields['PROPERTY_GRAFIK_VALUE']. "\n" ."Подробнее о вакансии смотрите на портале " . $link;
// Отправляем пост в группу.
$params = array(
'v' => $version,
'access_token' => $access_token,
'owner_id' => '-' . $group_id,
'from_group' => '1',
'message' => $message,
'attachments' => 'photo' . $oUrl2->response[0]->owner_id . '_' . $oUrl2->response[0]->id.',' . $link
);
$result = file_get_contents('https://api.vk.com/method/wall.post?' . http_build_query($params));
$result = json_decode($result);
?><pre><?print_r($result)?></pre><?
$VK_ID = $result->response->post_id;
//Если пост отправлен меняем VK_ID
if (isset($VK_ID)) {
$PROP = array();
$PROP['VK_ID'] = $VK_ID; // свойству с кодом 12 присваиваем значение "Белый"
$ELEMENT_ID = 16; // код элемента
$PROPERTY_CODE = "VK_ID"; // код свойства
$PROPERTY_VALUE = $VK_ID; // значение свойства
// Установим новое значение для данного свойства данного элемента
$resultat = CIBlockElement::SetPropertyValuesEx($arFields['ID'], false, array($PROPERTY_CODE => $PROPERTY_VALUE));
?><pre><?print_r($resultat)?></pre><?
}
}
}
else
{
echo "не верный ключ";
}
}
else
{
echo "нет ключа доступа";
}
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question