【问题标题】:What's the mistake in a code written in PHP for push notification to be sent to iPhone?用 PHP 编写的将推送通知发送到 iPhone 的代码有什么错误?
【发布时间】:2015-06-01 18:42:13
【问题描述】:

我已经使用 composer 在本地机器上的以下位置安装了 php-pushwoosh

/var/www/gomoob-php-pushwoosh

现在我想通过 PHP 代码向 iPhone 发送推送通知。

所以,我自己尝试并在位于/var/www/gomoob-php-pushwoosh/sample.php 的名为sample.php 的文件中编写了PHP 代码。以下是文件sample.php中的PHP代码

代码参考链接:You can get the code reference here

php-pushwoosh 安装参考链接:php-pushwossh installation reference link

<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

require __DIR__.'/vendor/autoload.php';

define('PW_AUTH', 'API TOKEN');
define('PW_APPLICATION', 'APPLICATION CODE');
define('PW_DEBUG', true);

function pwCall($method, $data) { 
    $url = 'https://cp.pushwoosh.com/json/1.3/' . $method;
    $request = json_encode(['request' = $data]);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

    $response = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);

    if (defined('PW_DEBUG') && PW_DEBUG) {
        print "[PW] request: $request\n";
        print "[PW] response: $response\n";
        print "[PW] info: " . print_r($info, true);
    }
}

pwCall('createMessage', array(
    'application' => PW_APPLICATION,
    'auth' => PW_AUTH,
    'notifications' => array(
            array(
                'send_date' => 'now',
                'content' => 'test',
                'data' => array('custom' => 'json data'),
                'link' => 'http://pushwoosh.com/'
            )
        )
    )
);
?>

通过点击 URL localhost/gomoob-php-pushwoosh/sample.php 在浏览器中执行此代码后,我只得到空白页,没有错误、没有警告、没有通知。

为什么会这样?有人可以通过给出正确的解释来纠正我在代码中所犯的错误并使我的代码可行吗?

如果您需要有关我所面临问题的更多信息,请告诉我。

如果您有任何建议、评论、回答、批评等,我们都非常欢迎。任何形式的帮助将不胜感激。

谢谢。

【问题讨论】:

  • 将您的错误选项放入 php.ini。在脚本中设置它们毫无意义,因为它们只有在脚本实际运行时才能生效。任何致命的解析错误都不会运行,包括让您实际看到该解析错误的指令。

标签: php iphone notifications push-notification pushwoosh


【解决方案1】:

在这一行:

$request = json_encode(['request' = $data]);
                                  ^

显然应该是:

$request = json_encode(['request' => $data]);

您没有看到任何通知,因为必须在脚本之外设置 display_errors 设置才能捕获诸如此类的解析错误。

【讨论】:

  • 首先感谢杰克的帮助。但我已将 display_errors 设置放在打开 PHP 标记中,即
  • @user2839497 是的,但正如我在回答中提到的那样,必须在脚本外部定义,否则无法捕获解析错误。
  • 通过“外部”脚本是指在单独的一对单独的 PHP 标记中吗?
  • @user2839497 不,我的意思是应该在 .htaccess 或 php.ini 中定义
猜你喜欢
  • 2015-03-02
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 2011-04-25
相关资源
最近更新 更多