GET切换到字符串

时间:2017-12-14 14:16:55

标签: php switch-statement swiftmailer

所以我从我的$_GET获取了一个值,例如403,我正在邮寄它。但我想如果得到返回403,它将在邮件中返回sent

   <?php
require_once 'vendor/autoload.php';

switch($_GET['status'])
{
    case 403:
        echo 'sent';
        break;
}
switch($_GET['status'])
{
    case 404:
        echo 'delivered';
        break;
}

if (!empty($_GET['MsgId'])) {
    $transport = (new Swift_SmtpTransport('xxx', xxx))
        ->setUsername('xxx')
        ->setPassword('xxx');

    $mailer = new Swift_Mailer($transport);

    $message = (new Swift_Message('Status SMS ' . $_GET['MsgId'] . '.'))
        ->setFrom(['xxx' => 'xxx'])
        ->setTo(['xxx' => 'xxx', 'xxx' => 'xxx'])
        ->setBody("ID Wiadomości : " . $_GET['MsgId'] . "Status Wiadomości" . $_GET['status'] . ".");

    $result = $mailer->send($message);

    echo 'OK';
}
else {
    echo 'Coś nie śmigło.';
}
exit;

Switch对我不起作用。

2 个答案:

答案 0 :(得分:2)

您没有使用$_GET['status']更改全局变量echo,我认为您不应该使用变量:

require_once 'vendor/autoload.php';
// we set a variable to hold the status code
$status = '';
switch($_GET['status'])
{
    case 403:
        $status =  'sent';
        break;
    case 404:
        $status = 'delivered';
        break;
}


if (!empty($_GET['MsgId'])) {
    $transport = (new Swift_SmtpTransport('xxx', xxx))
      ->setUsername('xxx')
      ->setPassword('xxx');

    $mailer = new Swift_Mailer($transport);

    $message = (new Swift_Message('Status SMS ' . $_GET['MsgId'] . '.'))
      ->setFrom(['xxx' => 'xxx'])
      ->setTo(['xxx' => 'xxx', 'xxx' => 'xxx'])
      // and here you use that variable we prepared :)
      ->setBody("ID Wiadomości : " . $_GET['MsgId'] . "Status Wiadomości" . $status . ".");

    $result = $mailer->send($message);

    echo 'OK';
 }
 else {
     echo 'Coś nie śmigło.';
 }
 exit;

答案 1 :(得分:0)

你在这里:

将从$ _GET收到的值转换为整数,并添加default个案值。

switch((int)$_GET['status'])
{
  case 403:
    echo 'sent';
    break;

  case 404:
    echo 'delivered';
    break;

 default:
    echo 'unknown status';
    break;
}