我通过所有php文件传递变量时遇到问题

时间:2019-08-15 20:59:18

标签: php twilio

你好,我用twilio api创建了一个简单的项目,它调用用户并说出他/她的姓名和用户名,因此用户需要通过按aws亚马逊注册页面中看到的数字来验证帐户,但是我有问题,这就是我使用会话的方式

<html><head>
  <title>test</title>
  <link rel="stylesheet" href="uzDIA.css">
</head>
<body><div class="container">  
  <form id="contact" action="jes.php" method="POST">
    <h3></h3>
    <h4></h4>
    <fieldset>
      <input placeholder="name" name="name" type="text" tabindex="1" required="" autofocus="">
    </fieldset>
    <fieldset>
      <input placeholder="name" name="username" type="text" tabindex="2" required="">
    </fieldset>
    <fieldset>
      <input placeholder="Number" name="phone" type="tel" tabindex="3" required="">

 </fieldset>
  <fieldset>
      <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">submit </button>
    </fieldset>
  </form>
</div>

</body></html>

我需要通过所有我使用过的会话不起作用的php文件传递此变量,我使用的include不能工作,当我按下提交无变量共享时,我会使用get方法不起作用

我的操作页面

     <?php
  session_start();
  if($_SERVER['REQUEST_METHOD']=="POST"){

  $_SESSION['name']=$_POST['name'];
  $_SESSION['username']=$_POST['username'];
  $_SESSION['phone']=$_POST['[phone'];

  header("location : /1-outbound-call.php");

}


?>

1-outbound-call.php

    <?php
session_start();
require __DIR__ . '/Twilio/autoload.php';
include 'config.php';
use Twilio\Rest\Client;

$number=$_SESSION['phone'];

$account_sid = 'xxxxxxxxx';
$auth_token = 'xxxxxxx';
// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]
// A Twilio number you own with SMS capabilities
$twilio_number = "+4474xxxxx";
$url="http://example.com/2-call-answered.php";
// Where to make a voice call (your cell phone?)

$to_number = $phone;

$client = new Client($account_sid, $auth_token);
$client->account->calls->create(  
    $to_number,
    $twilio_number,
    array(
        "url" => "$url",
    )
);
?>


2-call-answered.php

<?php
session_start();

// Create a route that will handle Twilio webhook requests, sent as an
// HTTP POST to /voice in our application
require_once 'Twilio/autoload.php';
include 'config.php';
$name=$_SESSION['name'];
$user=$_SESSION['username'];

use Twilio\Twiml;
// Use the Twilio PHP SDK to build an XML response
$response = new Twiml();
// Use the <Gather> verb to collect user input
$gather = $response->gather(array('numDigits' => 6, 'action' => '/3-gather.php?'));
$url="2-call-answered.php";

// use the <Say> verb to request input from the user
$gather->say("hello $name your username is $username please confirm blah blah blah  ");

// If the user doesn't enter input, loop
$response->redirect($url);

// Render the response as XML in reply to the webhook request

header('Content-Type: text/xml');
echo $response;
?>

1 个答案:

答案 0 :(得分:1)

您不能在浏览器请求和webhook请求之间使用会话变量,因为webhook请求不是来自同一浏览器会话。

您应该做的是将变量放入传递给Twilio的URL中。

$url="http://example.com/2-call-answered.php?name={$_SESSION['name']}&username={$_SESSION['username']}";

然后2-call-answered.php可以使用$_GET['username']$_GET['name']

$name = $_GET['name'];
$username = $_GET['username'];
...
$gather = $response->gather(array('numDigits' => 6, 'action' => "/3-gather.php?name=$name&username=$name"));