JPush API PHP Client

Github Source Code

This is a PHP version development package for the JPush REST API. It is provided by the JPush officially and generally supports the latest API features.

Corresponding REST API documentation: https://docs.jiguang.cn/jpush/server/push/server_overview/

Supported PHP versions:5.3.3 ~ 5.6.x, 7.0.x

If PHP 5.3.3 and lower versions need to be compatible, you can use the v3 branch code. Since running Composer requires PHP 5.3.2+ or later, it does not provide Composer support. You can also click the link to download the source code of v3.4.x version.

Installation

Use Composer to install

  • Add the jpush dependency to the composer.json file in the project
  1. "require": {
  2. "jpush/jpush": "^3.5"
  3. }
  • Perform a $ php composer.phar install or $ composer install for installation.

Directly Download Source Code to Install

Directly downloading the source code is also a method of installing the SDK. However, because of maintenance problems of version updates, this type of installation is not recommended. We provide backups for this situation where Composer cannot be used due to various reasons.

  • Download the source code package and unzip it into the project
  • Introduce autoload in the project
  1. require 'path_to_sdk/autoload.php';

Usage

Initialization

  1. use JPush\Client as JPush;
  2. ...
  3. ...
  4. $client = new JPush($app_key, $master_secret);
  5. ...

OR

  1. $client = new \JPush\Client($app_key, $master_secret);

Simple Push

  1. $client->push()
  2. ->setPlatform('all')
  3. ->addAllAudience()
  4. ->setNotificationAlert('Hello, JPush')
  5. ->send();

Exception Handling

  1. $pusher = $client->push();
  2. $pusher->setPlatform('all');
  3. $pusher->addAllAudience();
  4. $pusher->setNotificationAlert('Hello, JPush');
  5. try {
  6. $pusher->send();
  7. } catch (\JPush\Exceptions\JPushException $e) {
  8. // try something else here
  9. print $e;
  10. }

Examples

Note: This is just a sample and should not be used directly in the actual environment!!

There is a simple example code in the download examples folder, and developers can refer to the sample to quickly understand how to use the library.

Note: The downloaded sample code cannot be used immediately. You need to fill in the examples/config.php file with necessary parameters, or set the relevant environment variables. Otherwise the sample operation will fail. In order to protect developer privacy, the examples/config.php file is not in version control and needs to be manually copied by using the following command:

  1. $ cp examples/config.php.example examples/config.php

Simple Use

To run the sample code in push_example.php:

  1. # Assume that the current directory is the root directory of the JPush source
  2. $ php examples/push_example.php

At the same time can also edit the relevant sample files, and change the parameters to view the effect of the execution

Testing

  1. # Edit the tests/bootstrap.php file and fill in the required variable values
  2. # OR setting the corresponding environment variable
  3. # Run all test cases
  4. $ composer tests
  5. #Run a specific test case
  6. $ composer tests/JPush/xxTest.php

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jpush/jpush-api-php-client.

License

The library is available as open source under the terms of the MIT License.