@Perokar

Как выполнить часть php по условию?

Суть такая: с index.html отправляю post через axios:
axios.post('Bot/index_bot_test.php', {
name:name,
phone:phone
})
.then(function (response) {
console.log('this is response ->>>>>', response);
const { data } = response;
console.log('data ->>>>', data);

а тут ловлю
$send_name = "TEST";
$is_log = true;

	function create($request)
{
		return $request;
}
$request = file_get_contents("php://input");
	if ($request) // <b>а вот тут должно быть условие что бы поймать запрос и получить данные в переменную, которые дальше уйдут запросом на viber api</b>
{
$input = json_decode($request, true);
echo json_encode(create($request));
$send_data = $input["name"].' '.$input["phone"];
die;
}

function put_log_in($data)
{
	global $is_log;
	$data_to_log = $data;
	$localtime = localtime();
	$data_to_log["time"] = $localtime[3]."-".$localtime[4]."-".$localtime[5]." ".$localtime[2]." : ".$localtime[1]." : ".$localtime[0];
	if($is_log) {file_put_contents("tmp_in.txt", $data_to_log."\n", FILE_APPEND);}
}

function put_log_out($data)
{
	global $is_log;
	$data_to_log = $data;
	$localtime = localtime();
	$data_to_log["time"] = $localtime[3]."-".$localtime[4]."-".$localtime[5]." ".$localtime[2]." : ".$localtime[1]." : ".$localtime[0];
	if($is_log) {file_put_contents("tmp_out.txt", $data_to_log."\n", FILE_APPEND);}
}

function sendReq($data)
{
	$request_data = json_encode($data);
	put_log_out($request_data);
	
	//here goes the curl to send data to user
	$ch = curl_init("https://chatapi.viber.com/pa/send_message");
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $request_data);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
	$response = curl_exec($ch);
	$err = curl_error($ch);
	curl_close($ch);
	if($err) {return $err;}
	else {return $response;}
}

function sendMsg($sender_id, $text, $type, $tracking_data = Null, $arr_asoc = Null)
{
	global $auth_token, $send_name;
	$data = json_encode("{}");
  
	$data['auth_token'] = $auth_token;
	$data['receiver'] = $sender_id;
	if($text != Null) {$data['text'] = $text;}
	$data['type'] = $type;
	$data['min_api_version'] = 1;
	$data['sender']['name'] = $send_name;
	//$data['sender']['avatar'] = $input['sender']['avatar'];	
	return sendReq($data);
}

function sendMsgText($sender_id, $text, $tracking_data = Null)
{
	return sendMsg($sender_id, $text, "text", $tracking_data);		
}

$request = file_get_contents("php://input");
$input = json_decode($request, true);
put_log_in($request);
$webhook_response = json_encode("{}");

$type = $input['message']['type']; //type of message received (text/picture)
$text = $input['message']['text']; //actual message the user has sent
$sender_id = $input['sender']['id']; //unique viber id of user who sent the message
$sender_name = $input['sender']['name']; //name of the user who sent the message

if($input['event'] == 'webhook') 
{
  $webhook_response['status'] = 0;
  $webhook_response['status_message'] = "ok";
  $webhook_response['event_types'] = 'delivered';
  echo json_encode($webhook_response);
  die;
}
else if($input['event'] == "delivered") // <b>не понимаю почему не работает</b>
{
	$webhook_response['status'] = 200;
	echo json_encode($webhook_response);
	die;
	}
else if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
  sendMsgText('53vrnYtD+GldJdz02Z+BuQ==', $send_data,  $tracking_data = Null);
}


По нажатию одной кнопки, axios переправляет телефон и имя на сервер, где они пересылаются через viber бота одному конкретному человеку.
Но скрипт падает.
Ещё не отрабатывает доставка на viber api.
  • Вопрос задан
  • 136 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы