php - 使用 PHP 解析推送通知

标签 php parsing post curl

我使用 Parse 向我的 iOS 和 Android 应用程序发送通知。但我想从我的网站上做到这一点。

我在 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. A simple alert can be sent to Android devices on the global broadcast channel using the following command:

curl -X POST \

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

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

-H "Content-Type: application/json" \

-d '{ "channel": "", \

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

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

谁能帮我制作一个 PHP 文件来发布这个? 谢谢!

最佳答案

将你的命令行 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);

更新

<?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>

有了这个,您未说明的问题应该得到回答。如果您仍然不知道如何做到这一点,您应该认真考虑学习一些 webdev 或换工作。这是您可以做的最基本的事情。

关于php - 使用 PHP 解析推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9443932/

相关文章:

php - CSS - IE7 无法正确/完整地呈现文本框和一些下拉字段

c - 需要帮助使用 lex 和 yacc 生成三个地址代码

php - 添加到我的 cookie 名称的前缀

PHP:不可变的公共(public)成员字段

php - symfony2 : ContextErrorException on production server "Erroneous data format for unserializing"

c++ - 指向使用 vector 地址的临时指针

parsing - 如何加快pandas read_csv的速度?

php - 表单提交不发送任何 POST 数据

ruby-on-rails - 由于 :remote=>true,表单提交了两次

java - Restful : return error code in place of JSON