从html表单发布到onesignal php Api?

时间:2022-10-16 11:03:23

I was wondering how can I make this simple form work with Onesignal Api. Html form to post the writing message into onesignal php file.

我想知道如何使这个简单的表格与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

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

I know it may be simple for someone here I can't seem to find a way. I have tried replacing 'Message' with <?php echo htmlspecialchars($_POST['Message']); ?> but all I get is error.

我知道这里的人可能很简单,我似乎找不到办法。我试过用<?php echo htmlspecialchars($ _ POST ['Message'])替换'Message';但是我得到的只是错误。

2 个解决方案

#1


1  

Perhaps you should learn a bit more how functions work http://php.net/manual/en/functions.arguments.php

也许你应该多了解一下函数的工作原理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");

#2


0  

My answer it is probably a suboptimal solution but it works, first change your html to avoid confusion.

我的答案可能是一个次优的解决方案,但它的工作原理,首先更改您的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>

Now in the PHP file declare 'mytext' as an empty variable above your first function, then replace the variable in your array with the correct syntax.

现在在PHP文件中将'mytext'声明为第一个函数上方的空变量,然后用正确的语法替换数组中的变量。

<?php
    $mytext = "";
    function sendMessage(){    
        $content = array(
            "en" => $_POST['mytext']
        );
//etc...

Finally let the user know the notification was succesful, give it a nice format with css and at last but not the least, I will suggest you to include some other important data such as a title, badge image, icon, an optional URL, buttons, and probably a big picture(you can do this, by modifying the $fields array), some of this values such as the badge or the icon, can be hardcoded in your php file, since you dont have to replace them every time.

最后让用户知道通知是成功的,用css给它一个很好的格式,最后但并非最不重要,我建议你包括一些其他重要的数据,如标题,徽章图像,图标,可选的URL,按钮,并且可能是一张大图(你可以通过修改$ fields数组来做到这一点),这些值中的一些,例如徽章或图标,可以在你的php文件中进行硬编码,因为你不必每次都替换它们。

#1


1  

Perhaps you should learn a bit more how functions work http://php.net/manual/en/functions.arguments.php

也许你应该多了解一下函数的工作原理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");

#2


0  

My answer it is probably a suboptimal solution but it works, first change your html to avoid confusion.

我的答案可能是一个次优的解决方案,但它的工作原理,首先更改您的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>

Now in the PHP file declare 'mytext' as an empty variable above your first function, then replace the variable in your array with the correct syntax.

现在在PHP文件中将'mytext'声明为第一个函数上方的空变量,然后用正确的语法替换数组中的变量。

<?php
    $mytext = "";
    function sendMessage(){    
        $content = array(
            "en" => $_POST['mytext']
        );
//etc...

Finally let the user know the notification was succesful, give it a nice format with css and at last but not the least, I will suggest you to include some other important data such as a title, badge image, icon, an optional URL, buttons, and probably a big picture(you can do this, by modifying the $fields array), some of this values such as the badge or the icon, can be hardcoded in your php file, since you dont have to replace them every time.

最后让用户知道通知是成功的,用css给它一个很好的格式,最后但并非最不重要,我建议你包括一些其他重要的数据,如标题,徽章图像,图标,可选的URL,按钮,并且可能是一张大图(你可以通过修改$ fields数组来做到这一点),这些值中的一些,例如徽章或图标,可以在你的php文件中进行硬编码,因为你不必每次都替换它们。