简体   繁体   English

用PHP编写的用于将推送通知发送到iPhone的代码有什么错误?

[英]What's the mistake in a code written in PHP for push notification to be sent to iPhone?

I've installed php-pushwoosh using composer at following location on my local machine : 我已经在本地计算机上的以下位置使用composer安装了php-pushwoosh

/var/www/gomoob-php-pushwoosh

Now I want to send push notifications to iPhone through the PHP code. 现在,我想通过PHP代码向iPhone发送推送通知。

So, I tried by myself and written PHP code in a file titled sample.php which is located at /var/www/gomoob-php-pushwoosh/sample.php . 因此,我自己尝试了一下,并将PHP代码写入了一个名为sample.php的文件,该文件位于/var/www/gomoob-php-pushwoosh/sample.php Following is the PHP code from file sample.php 以下是来自sample.php文件的PHP代码

Code Reference Link : You can get the code reference here 代码参考链接: 您可以在此处获取代码参考

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

<?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/'
            )
        )
    )
);
?>

After executing this code into browser by hitting the URL localhost/gomoob-php-pushwoosh/sample.php I'm getting blank page only, no errors, no warnings, no notices. 在通过localhost/gomoob-php-pushwoosh/sample.php URL localhost/gomoob-php-pushwoosh/sample.php在浏览器中执行此代码后,我只得到空白页,没有错误,没有警告,没有通知。

Why so? 为什么这样? Can someone please correct the mistakes I'm making into my code by giving proper explanation and make my code workable? 有人可以通过给出正确的解释来纠正我在代码中犯的错误并使我的代码可行吗?

If you need any further information regarding the issue I'm facing please do let me know. 如果您需要有关我所面临问题的更多信息,请告诉我们。

If you have any kind of suggestion, comment, answer, criticism, etc. you are most welcome. 如果您有任何建议,评论,答案,批评等,欢迎您。 any kind of help would be highly appreciated. 任何帮助将不胜感激。

Thanks. 谢谢。

It's in this line: 在这行中:

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

It should obviously be: 显然应该是:

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

You didn't see any notice because the display_errors setting must be set outside of the script to catch parse errors such as this one. 您没有看到任何通知,因为必须在脚本之外设置display_errors设置以捕获诸如此类的解析错误。

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

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