2016-08-17 29 views
2

我想知道如何才能使这个简单的窗体与Onesignal Api工作。 Html表单将文字信息发布到onesignal php文件中。从html格式发布到onesignal php Api?

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

我知道这可能是简单的人在这里,我似乎无法找到一种方法。 我试过用<?php echo htmlspecialchars($_POST['Message']); ?>替换'Message',但是我得到的只是错误。

回答

1

也许你应该学习更多的功能是如何工作的 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");