array_key_exists()期望参数2为数组,使用pushWoosh类时为字符串

时间:2014-09-07 10:15:53

标签: php arrays 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;
}
}

那里有一个聪明的头脑可以告诉我什么是错的吗?

3 个答案:

答案 0 :(得分:0)

如果我理解,如果您当前正在阅读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'];
    }

您遍历$pushesarray('content' => ..., 'devices' => ...)。您将首先获得$key =内容,$key =&#39;设备&#39;。

答案 1 :(得分:0)

看起来createMessage函数需要一组消息,但是您直接传入一条消息。试试这个:

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

答案 2 :(得分:0)

该类期待一组数组;你只是提供一个阵列。

你可以做这样的事情

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

您是否想要处理多条消息?它会对我的行为产生影响。