2016-08-17 25 views
2

私はこの単純なフォームをOnesignal Apiでどのように動作させることができるのだろうと思っていました。 書式メッセージをonesignal phpファイルに投稿するHTMLフォーム。htmlフォームからoneignal php Apiへの投稿?

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

one.php

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

私はそれが私が道を見つけることができないよう、ここで誰かのために簡単なものでも知っています。 私は<?php echo htmlspecialchars($_POST['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"); 
関連する問題