1 回答
TA贡献1815条经验 获得超10个赞
我终于通过使用 csv_string_to_array() 将字符串 $str 转换为数组然后将其分配给 onesigId 使其工作。现在一切看起来都很好。
$userids = array();
$tu = 2;
$on=1;
$sql = $db -> prepare('SELECT id FROM users WHERE regTarget=? AND is_active=?');
$sql -> bind_param('ii', $tu, $on);
$sql -> execute();
$result = $sql->get_result();
while($row = mysqli_fetch_assoc($result) ){
$userids[]= $row['id'];
}
$str = '"' . implode ( '","', $userids ) . '"';
//This is what I used in converting the string to array
function csv_string_to_array($str){
$expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
$results=preg_split($expr,trim($str));
return preg_replace("/^\"(.*)\"$/","$1",$results);
}
//echo $str;
//SEND ONESIGNAL PUSH
$onesigTitle = "$subject";
$onesigMessage = "$message.";
//HERE AM ASSIGNING THE ARRAY TO onesigId
$onesigId = csv_string_to_array($str);
$onesignalUrl = "$link";
function sendMessage(){
global $onesigTitle;
global $onesigMessage;
global $onesignalUrl;
global $onesigId;
$content = array(
"en" => $onesigMessage
);
$fields = array(
'app_id' => "my_app_id",
'headings' => array("en" => $onesigTitle),
'include_external_user_ids' => $onesigId,
'data' => array("foo" => "bar"),
'url' => $onesignalUrl,
'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 MY KEY HERE'));
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);
print("\n\nJSON received:\n");
print($return);
print("\n");
- 1 回答
- 0 关注
- 158 浏览
添加回答
举报