gpt4 book ai didi

php - 从 html 表单发布到 onesignal php Api?

转载 作者:行者123 更新时间:2023-11-27 23:25:03 24 4
gpt4 key购买 nike

我想知道如何使这个简单的表单与 Onesignal Api 一起使用。将写入消息发布到 onesignal php 文件的 Html 表单。

<form action="one.php" method="post">
<p>Your Message: <input type="text" name="Message" /></p>
<p><input type="submit" /></p>
</form>

一个.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");
?>

我知道这对这里的人来说可能很简单,但我似乎找不到方法。我尝试用 <?php echo htmlspecialchars($_POST['Message']); ?> 替换“消息”但我得到的只是错误。

最佳答案

也许你应该多了解一下函数的工作原理 http://php.net/manual/en/functions.arguments.php

<?php

function sendMessage($message){

$content = array(
"en" => $message
);

$fields = array(
'app_id' => "5eb5a37e-b458-11e3-ac11-000c2940e62c",
'included_segments' => array('All'),
'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 NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj'
)
);
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($_POST['Message']);

$return["allresponses"] = $response;
$return = json_encode( $return);

print("\n\nJSON received:\n");
print($return);
print("\n");

关于php - 从 html 表单发布到 onesignal php Api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39002419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com