当我将它传递给数组时,Php Variable为null

时间:2018-04-15 22:01:54

标签: php onesignal

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

GaussMatrix = np.array(GaussMatrix, dtype='float64')

结果是:

 <?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);

我尝试了很多带/不带配额的变种,json_encode()函数但是无法将该变量传递给该数组。

1 个答案:

答案 0 :(得分:2)

您的变量超出范围。

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

可以通过以下方式解决此问题:

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