簡體   English   中英

array_key_exists()期望參數2為數組,使用pushWoosh類時為字符串

[英]array_key_exists() expects parameter 2 to be array, string when using pushWoosh Class

我在實現pushwoosh類http://astutech.github.io/PushWooshPHPLibrary/index.html時遇到了一些麻煩。 我已經完成所有設置,但是從類中得到了數組錯誤。

這是我提供給班級的代碼:

<?php
require '../core/init.php';

//get values from the clientside
$sendID = $_POST['sendID'];
$memID = $_POST['memID'];

//get sender name
$qry = $users->userdata($memID);
$sendName = $qry['name'];

//get receiving token
$qry2 = $users->getTokenForPush($sendID);
$deviceToken = $qry2['token'];
//i have testet that $deviceToken actually returns the $deviceToken so thats not the problem

//this is the array that the php class requires.
$pushArray = array(
        'content'   => 'New message from ' . $sendName,
        'devices'   => $deviceToken,
);

$push->createMessage($pushArray, 'now', null);
?>

這是createMessage()方法的實際代碼

public function createMessage(array $pushes, $sendDate = 'now', $link = null, 

$ios_badges = 1)
{
// Get the config settings
$config = $this->config;
// Store the message data
$data = array(
'application' => $config['application'],
'username' => $config['username'],
'password' => $config['password']
);
// Loop through each push and add them to the notifications array
foreach ($pushes as $push) {
$pushData = array(
'send_date' => $sendDate,
'content' => $push['content'],
'ios_badges' => $ios_badges
);
// If a list of devices is specified, add that to the push data
if (array_key_exists('devices', $push)) {
$pushData['devices'] = $push['devices'];
}
// If a link is specified, add that to the push data
if ($link) {
$pushData['link'] = $link;
}
$data['notifications'][] = $pushData;
}
// Send the message
$response = $this->pwCall('createMessage', $data);
// Return a value
return $response;
}
}

是否有一個聰明的頭腦可以告訴我什么地方出了問題?

據我了解,您正在嘗試是否正在讀取devices子陣列。 您應該嘗試這樣:

foreach ($pushes as $key => $push) {
    ...
    // If a list of devices is specified, add that to the push data
    if ($key == 'devices') {
        $pushData['devices'] = $push['devices'];
    }

您遍歷$pushes ,它是array('content' => ..., 'devices' => ...) 您將首先擁有$key = content, $key ='devices'。

看起來createMessage函數需要一組消息,但是您直接傳遞了一條消息。 嘗試以下方法:

$push->createMessage(array($pushArray), 'now', null);

該類期望一個數組數組; 您只是提供一個數組。

你可以做這樣的事情

//this is the array that the php class requires.
$pushArrayData = array(
        'content'   => 'New message from ' . $sendName,
        'devices'   => $deviceToken,
);
$pushArray[] = $pushArrayData

您是否想處理多條消息? 這會改變我的操作方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM