lk.zachem.info/app/Models/Bitrix/SendClient.php
2024-12-11 23:43:54 +08:00

86 lines
2.5 KiB
PHP

<?php
namespace App\Models\Bitrix;
use Illuminate\Support\Facades\Http;
class SendClient
{
private $IBLOCK_TYPE_ID = 'lists';
//CONFIG для Альфы
private $URL = 'https://b24alfa.pro/rest/3165/v90a792nderzu0dj/lists.element.add.json';
private $IBLOCK_ID = 27;
private $ID;
const CLIENT_SECOND_NAME = "PROPERTY_94";
const CLIENT_FIRST_NAME = "PROPERTY_95";
const CLIENT_PHONE = "PROPERTY_96";
const BROKER_SECOND_NAME = "PROPERTY_97";
const BROKER_FIRST_NAME = "PROPERTY_98";
const BROKER_PHONE = "PROPERTY_99";
const OBJECT_NAME = "PROPERTY_100";
const CALLBACK_URL = "PROPERTY_105";
private $data = [];
public function __construct($id, $data) {
$this->ID = $id;
$data = array_change_key_case($data, CASE_UPPER);
$finalData = [];
$refl = new \ReflectionClass(__CLASS__);
$constants = $refl->getConstants();
foreach ( $constants as $constName => $constValue )
{
foreach ($data as $key => $value)
{
if ( $constName == $key ) {
$finalData[$constValue] = $value;
break;
}
}
}
$finalData['NAME'] = $finalData[self::CLIENT_FIRST_NAME] . ' ' . $finalData[self::CLIENT_SECOND_NAME];
$this->data = $finalData;
return;
}
public function send() {
$postdata = http_build_query(
[
'IBLOCK_TYPE_ID' => $this->IBLOCK_TYPE_ID,
'IBLOCK_ID' => $this->IBLOCK_ID,
'ELEMENT_CODE' => $this->ID,
'FIELDS' => $this->data
]
);
$opts = array(
'ssl' => array(
'verify_peer' => false,
'verify_peername' => false
),
'http' => array(
'method' => 'POST',
'header' =>
'Content-type: application/x-www-form-urlencoded'."\r\n".
'',
'content' => $postdata
)
);
return true;
try {
$context = stream_context_create($opts);
$result = file_get_contents($this->URL, false, $context);
$result = json_decode($result, $associative = true);
if (array_key_exists('result', $result)) {
return $result['result'];
} else {
return false;
}
} catch (\Exception $e) {
dd($e);
return false;
}
}
}