如何使用 PHP 在 Twilio 中传递自定义参数?

How do you pass a custom parameter in Twilio with PHP?

从这里看:https://www.twilio.com/docs/voice/client/twiml 我能得到的最接近的是:

$response = new Twiml();
$callerIdNumber = config('services.twilio')['number'];
$dial = $response->dial(['callerId' => $callerIdNumber]);
$dial->parameter([
     "name" => "firstname",
     "value" => "Test Firstname",
]);

$dial->parameter([
    "name" => "lastname",
    "value" => "Test lastname",
]);

$dial->client('support_agent');
return $response;

但它抛出一个无效的 XML 警告:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Dial callerId="+12055461045">
        <Parameter name="firstname" value="Test Firstname"/>
        <Parameter name="lastname" value="Test lastname"/>
        <Client>support_agent</Client>
    </Dial>
</Response>

在文档中,它应该是这样的:

<Response>
    <Dial>
        <Client>
            <Identity>joey</Identity>
            <Parameter name="firstname" value="Test Firstname"/>
            <Parameter name="lastname" value="Test lastname"/>
        </Client>
    </Dial>
</Response>

我已经尝试了很多东西,但它们都抛出应用程序错误。这是我试过的两个。我试过的其他东西我已经忘记了。

// trial 1
$dial->client->identity('support_agent')
$dial->client->parameter([
    "name" => "firstname",
    "value" => "Test Firstname",
]);

//trial 2
$dial->client->identity('support_agent', array('parameter' => [
    "name" => "firstname",
    "value" => "Test Firstname",
]))

如有任何帮助,我们将不胜感激!

谢谢!

您可以参考下面的PHP代码示例来添加自定义参数,此代码是我在本地系统上测试过的。

        require_once './vendor/autoload.php';
        use Twilio\TwiML\VoiceResponse;

        $response = new VoiceResponse();
        $dial = $response->dial('');
        $client = $dial->client();
        $identity = $client->Identity('user-jane');

        $client->parameter([
             "name" => "firstname",
             "value" => "Test Firstname",
        ]);

        $client->parameter([
            "name" => "lastname",
            "value" => "Test lastname",
        ]);

        echo $response;

Response is below

<Response>
<Dial>
    <Client>
        <Identity>user-jane</Identity>
        <Parameter name="firstname" value="Test Firstname" />
        <Parameter name="lastname" value="Test lastname" />
    </Client>
</Dial>