P
P
Perokar2020-11-19 00:35:31
PHP
Perokar, 2020-11-19 00:35:31

How to execute a php part by condition?

The bottom line is this: from index.html I send a post via 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);

and here I catch

$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);
}


By pressing one button, axios forwards the phone and name to the server, where they are sent via the viber bot to one specific person.
But the script crashes.
Delivery to viber api is not working yet.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question