我想知道如何让这个简单的形式与Onesignal Api一起使用。 Html表单将写入消息发布到onesignal php文件中。
<form action="one.php" method="post">
<p>Your Message: <input type="text" name="Message" /></p>
<p><input type="submit" /></p>
</form>
one.php
<?PHP
function sendMessage(){
$content = array(
"en" => 'Message' <-the message field I need to replace via form
);
$fields = array(
'app_id' => "896068a9-2b83-4a1d-9c6a-53300261e7d5",
'included_segments' => array('All'),
'data' => array("foo" => "bar"),
'contents' => $content
);
$fields = json_encode($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',
'Authorization: Basic NTkyZDEyNjktOGJiNS60YmQ5LT2hZDktMWQ5MzA1ZjY3Mjcz'));
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 Message Send");
?>
我知道这里的某些人似乎很难找到方法。
我试过更换消息&#39;使用<?php echo htmlspecialchars($_POST['Message']); ?>
,但我得到的只是错误。
答案 0 :(得分:1)
也许你应该多了解一下函数的工作原理 http://php.net/manual/en/functions.arguments.php
<?php
$array = array(1,2,5,8,10,15);
$number = count($array);
$b = 7; // we don't know yet what $a and $b could be
for ($i=0;$i<$number;$i++) {
if($array[$i]>$b) { // what ever condition you want here
// do smth
} else {
// do smth else or nothing
}
}
?>
答案 1 :(得分:0)
我的回答可能是次优的解决方案,但它可以工作,请先更改您的html以避免混淆。
<!--Lets change input element to: name="mytext" -->
<form action="one.php" method="post">
<p>Your Message: <input type="text" name="mytext" /></p>
<p><input type="submit" /></p>
</form>
现在,PHP文件中的第一个函数上方将'mytext'声明为空变量,然后使用正确的语法替换数组中的变量。
<?php
$mytext = "";
function sendMessage(){
$content = array(
"en" => $_POST['mytext']
);
//etc...
最后让用户知道通知是成功的,使用css给它一个很好的格式,最后但并非最不重要的一点,我建议您包括一些其他重要数据,例如标题,徽章图像,图标,可选的URL,按钮,可能还有一张大图片(您可以通过修改$ fields数组来完成此操作),其中一些值(例如徽章或图标)可以硬编码到您的php文件中,因为您不必替换它们每次。