简体   繁体   English

使用PHP解析推送通知

[英]Parse Push Notifications with PHP

I've use Parse to give my iOS and Android App notifications. 我使用Parse来提供iOS和Android App通知。 But I want to do that from my website. 但我想从我的网站上做到这一点。

I've found this in de docs of Parse: 我在Parse的文档中找到了这个:

To send a push notification, send a POST request to https://api.parse.com/1/push with the Content-Type header set to application/json. 要发送推送通知,请将POST请求发送到https://api.parse.com/1/push ,并将Content-Type标头设置为application / json。 A simple alert can be sent to Android devices on the global broadcast channel using the following command: 可以使用以下命令将简单警报发送到全局广播信道上的Android设备:

curl -X POST \\ 卷曲-X POST \\

-H "X-Parse-Application-Id: ${APPLICATION_ID}" \\ -H“X-Parse-Application-Id:$ {APPLICATION_ID}”\\

-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \\ -H“X-Parse-REST-API-Key:$ {REST_API_KEY}”\\

-H "Content-Type: application/json" \\ -H“内容类型:application / json”\\

-d '{ "channel": "", \\ -d'{“频道”:“”,\\

  "type": "android", \\ "expiry": 1451606400, \\ "data": { "alert": "greetings programs" } }' \\ 

https://api.parse.com/1/push https://api.parse.com/1/push

Who can help me to make a PHP-file to post this ? 谁可以帮我制作一个PHP文件来发布这个? Thanks! 谢谢!

Translating your command line curl to PHP you get something along the lines of 将命令行curl转换为PHP,你会得到一些东西

<?php
$url = 'https://api.parse.com/1/push';
$data = array(
    'channel' => '',
    'type' => 'android',
    'expiry' => 1451606400,
    'data' => array(
        'alert' => 'greetings programs',
    ),
);
$_data = json_encode($data);
$headers = array(
    'X-Parse-Application-Id: ' . $APPLICATION_ID,
    'X-Parse-REST-API-Key: ' . $REST_API_KEY,
    'Content-Type: application/json',
    'Content-Length: ' . strlen($_data),
);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_exec($curl);

UPDATE UPDATE

<?php

$APPLICATION_ID = "your-app-id";
$REST_API_KEY = "your-api-key";
$MESSAGE = "your-alert-message";

if (!empty($_POST)) {

    $errors = array();
    foreach (array('app' => 'APPLICATION_ID', 'api' => 'REST_API_KEY', 'body' => 'MESSAGE') as $key => $var) {
        if (empty($_POST[$key])) {
            $errors[$var] = true;
        } else {
            $$var = $_POST[$key];
        }
    }

    if (!$errors) {
        $url = 'https://api.parse.com/1/push';
        $data = array(
            'channel' => '',
            'type' => 'android',
            'expiry' => 1451606400,
            'data' => array(
                'alert' => $MESSAGE,
            ),
        );
        $_data = json_encode($data);
        $headers = array(
            'X-Parse-Application-Id: ' . $APPLICATION_ID,
            'X-Parse-REST-API-Key: ' . $REST_API_KEY,
            'Content-Type: application/json',
            'Content-Length: ' . strlen($_data),
        );

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($curl);
    }
}
?><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Parse API</title>
</head>
<body>
    <?php if (isset($response)) {
        echo '<h2>Response from Parse API</h2>';
        echo '<pre>' . htmlspecialchars($response) . '</pre>';
        echo '<hr>';
    } elseif ($_POST) {
        echo '<h2>Error!</h2>';
        echo '<pre>';
        var_dump($APPLICATION_ID, $REST_API_KEY, $MESSAGE);
        echo '</pre>';
    } ?>

    <h2>Send Message to Parse API</h2>
    <form id="parse" action="" method="post" accept-encoding="UTF-8">
        <p>
            <label for="app">APPLICATION_ID</label>
            <input type="text" name="app" id="app" value="<?php echo htmlspecialchars($APPLICATION_ID); ?>">
        </p>
        <p>
            <label for="api">REST_API_KEY</label>
            <input type="text" name="api" id="api" value="<?php echo htmlspecialchars($REST_API_KEY); ?>">
        </p>
        <p>
            <label for="api">REST_API_KEY</label>
            <textarea name="body" id="body"><?php echo htmlspecialchars($REST_API_KEY); ?></textarea>
        </p>
        <p>
            <input type="submit" value="send">
        </p>
    </form>
</body>
</html>

With this, your unstated question should be answered. 有了这个,你应该回答你未说明的问题。 If you still can't figure out how to do this, you should seriously consider learning yourself some webdev or switch jobs. 如果你仍然无法弄清楚如何做到这一点,你应该认真考虑自己学习一些webdev或切换工作。 This is the most basic thing you can do. 这是你能做的最基本的事情。

For me it look like you have to do a simple HTTP request in json format. 对我来说,看起来你必须以json格式做一个简单的HTTP请求。 You may do it in javascript via ajax or by using curl extension in php. 你可以通过ajax或在php中使用curl扩展在javascript中完成。

'-H' is probably header, -d stands for data in json format. '-H'可能是标题,-d代表json格式的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM