Php 当我将变量传递给数组时变量为空

Php Variable is null when i pass it to the array

我正在尝试使用 onesignal 发送特定于用户的网络通知,我解决了除此变量空问题之外的所有问题。 我多次使用那个变量,除了这些行没有问题:

 <?php
     if(isset($_POST["sub"]))
     {
         $my_variable= $_POST["t1"];

         $SQL = "some sql here";

         if (mysqli_query($db, $SQL)) {
             echo $my_variable;
             echo "<br>";



             function sendMessage(){
                 $content = array(
                     "en" => 'test message'
                 );

                 $fields = array(
                     'app_id' => "5b0eacfc-3ac8-4dc6-891b-xxxxx",
                     'filters' => array(array("field" => "tag", "key" => "key", "relation" => "=", "value" => "$my_variable")),
                     'data' => array("foo" => "bar"),
                     'contents' => $content
                 );

                 $fields = json_encode($fields);
                 print("\nJSON sent:\n");
                 print($fields);

                 $ch = curl_init();
                 curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
                 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic xxxxxxx'));
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                 curl_setopt($ch, CURLOPT_HEADER, FALSE);
                 curl_setopt($ch, CURLOPT_POST, TRUE);
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

                 $response = curl_exec($ch);
                 curl_close($ch);

                 return $response;
             }

             $response = sendMessage();
             $return["allresponses"] = $response;
             $return = json_encode( $return);

结果是:

     1 JSON sent: {"app_id":"5b0eacfc-3ac8-4dc6-891b-xxxxx","filters":[{"field":"tag","key":"key","relation":"=","value":"null"}],"data":{"foo":"bar"},"contents":{"en":"test message"}}

我尝试了很多变体 with/without 配额,json_encode() 函数,但无法将该变量传递给该数组。

您的变量超出范围。

您在函数 sendMessage() 外定义了 $my_variable,但继续尝试在函数内使用它,而不是将其作为参数传递。

这可以通过以下方式解决:

function sendMessage($filterValue) 
{
    $content = array(
        "en" => 'test message'
    );

    $fields = array(
        'app_id' => "5b0eacfc-3ac8-4dc6-891b-xxxxx",
        'filters' => array(array("field" => "tag", "key" => "key", "relation" => "=", "value" => $filterValue)),
        'data' => array("foo" => "bar"),
        'contents' => $content
    );

    $fields = json_encode($fields);
    print("\nJSON sent:\n");
    print($fields);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic xxxxxxx'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

$response = sendMessage($my_variable);
$return["allresponses"] = $response;
$return = json_encode( $return);