%PDF- %PDF-
Direktori : /home/bitrix/www/local/php_interface/vk_post/ |
Current File : /home/bitrix/www/local/php_interface/vk_post/vk.php |
<? class vk { private $token; private $count = -1; private static $class = null; public static function create($token){ if(!self::$class){ self::$class = new vk($token); } return self::$class; } private function __clone(){} private function __construct($token){ $this->token = $token; } function get($method, array $data){ $this->count ++; if($this->count >= 3){ $this->count = 0; sleep(1); } $params = array(); foreach($data as $name => $val){ $params[$name] = $val; $params['access_token'] = $this->token; } $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://api.vk.com/method/' . $method); curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params)); $json = curl_exec($curl); curl_close($curl); return json_decode($json); } } ?> <? class post{ private $vk; private $owner; function __construct(vk $vk, $user = null, $group = null){ $this->vk = $vk; if(!$user && !$group){ throw new \Exception('Not found group or user'); } $this->owner = array( 'type' => $user ? 'owner_id' : 'group_id', 'value' => $user ? $user : $group ); $this->owner['value'] = (int)preg_replace('/([^\d]+)/', '', $this->owner['value']); } function post($text, $img = null, $link = null){ if($img) { $data = $this->load($img); $img = $data->response[0]->id; } $data = array( 'message' => $text, 'owner_id' => $this->owner['value'] ); if($img){ $data['attachments'] = $img; } if($this->owner['type'] == 'group_id'){ $data['owner_id'] = '-' . $data['owner_id']; } if ($link) { $data['attachments'] .= ','.$link; } $data = $this->vk->get('wall.post', $data); if(isset($data->error)){ throw new \Exception($data->error->error_msg); } return $data; } function load($src){ $photo = (array)$this->getPhoto($src); $photo[$this->owner['type']] = $this->owner['value']; $data = $this->vk->get('photos.saveWallPhoto', $photo); return $data; } private function getPhoto($src){ $name = $_SERVER['DOCUMENT_ROOT'].'/tmp/1.png'; file_put_contents($name, file_get_contents($src)); $ch = curl_init($this->getServer()); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, ['photo' => new CURLFile($name)]); $response = curl_exec( $ch ); curl_close( $ch ); return json_decode($response); } private function getServer(){ $data = $this->vk->get('photos.getWallUploadServer', array( $this->owner['type'] => $this->owner['value'], )); return $data->response->upload_url; } } ?> <? function vk_posting($vacid) { if (CModule::IncludeModule("iblock")) $mod = true; $token = 'ccca8cb02ecb378f22ff3dcd564f471bb4d03f2eba4ce09a118f91086a06e3aaf1e361bd41151a7aab7ad'; $user_id = null; $group_id = 94614580; $res = CIBlockElement::GetList(Array(), array("IBLOCK_ID" => 12, "ID" => intval($vacid)), false, false, array("ID", "IBLOCK_ID", "DETAIL_PICTURE", "NAME", "PROPERTY_CASTING_TEXT", "PROPERTY_CITY")); if ($ar_res = $res->GetNext()) { $arFilter = Array("ID" => intval($ar_res['PROPERTY_CITY_VALUE'])); $arSelect = Array("ID", "NAME", "IBLOCK_SECTION_ID", "PROPERTY_NAME_RU"); $r = CIBlockElement::GetList(Array(), $arFilter, false, Array(), $arSelect); if ($ar_r = $r->GetNext()) { $nam_reg = (isset($ar_r["PROPERTY_NAME_RU_VALUE"])) ? $ar_r["PROPERTY_NAME_RU_VALUE"] : $ar_r["NAME"]; } else { $nam_reg = ''; }; $text = '[' . $nam_reg . '] ' . $ar_res['NAME'] . ' ' . $ar_res['PROPERTY_CASTING_TEXT_VALUE'] . ' Откликнуться - http://port4lio.pro/castings/#' . $ar_res['ID'] . ' #port4lio #modelapp #model #photomodel #castingcall #casting #модель #модели #кастинг #кастинги @port4lio'; $image_path = CFile::GetPath($ar_res["DETAIL_PICTURE"]); // $image=$_SERVER["DOCUMENT_ROOT"].$image_path; $image=$image_path; $link = 'http://port4lio.pro/castings/#' . $ar_res['ID']; } else { $text = ''; $image = ''; $link = ''; }; try { $vk = \vk::create($token); $post = new \post($vk, $user_id, $group_id); $post->post($text, $image, $link); echo 'Success!'; } catch (Exception $e) { echo 'Error: <b>' . $e->getMessage() . '</b><br />'; echo 'in file "' . $e->getFile() . '" on line ' . $e->getLine(); } } ?>