pusher / pusher-http-php Goto Github PK
View Code? Open in Web Editor NEWPHP library for interacting with the Pusher Channels HTTP API
Home Page: https://pusher.com/docs/server_api_guide
PHP library for interacting with the Pusher Channels HTTP API
Home Page: https://pusher.com/docs/server_api_guide
Right now there is a lot of branches hanging around. This makes it hard to understand which branch is active and not. Maybe we can simplify them to just develop
and master
?
The 2.x branch has a bunch of acceptance (integration) tests (https://github.com/pusher/pusher-http-php/tree/master/test/acceptance) that have proven highly valuable. These should be ported to the 3.x (new-lib) branch.
Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.
More info: http://getcomposer.org/
So basicly it's just a simple .json file in the root of the repository like this: https://github.com/ruudk/Fresque/blob/master/composer.json
When you've added the composer.json you should publish it on Packagist (http://packagist.org/).
Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":null,"message":"user_info supplied to presence subscription is invalid - if supplied it must be a type of hash"}}}
What type of hash does the library expect?
Our build process throws the following exception
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "Pusher" from the global namespace.
Did you forget a "use" statement for "Pusher\Pusher"? in /builds/acme/vendor/laupifrpar/pusher-bundle/DependencyInjection/PusherFactory.php:26
Stack trace:
#0 /tmp/test/appTestDebugProjectContainer.php(5358): Lopi\Bundle\PusherBundle\DependencyInjection\PusherFactory::create(Array)
#1 /builds/acme/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php(311): appTestDebugProjectContainer->getLopiPusher_PusherService()
#2 /tmp/test/appTestDebugProjectContainer.php(8112): Symfony\Component\DependencyInjection\Container->get('lopi_pusher.pus...')
#3 /builds/acme/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php(311): appTestDebugProjectContainer->getAcme_Entity_AvailabilitySubscriberService()
#4 /tmp/test/appTestDebugProjectContainer.php(2830): Symfony\Component\DependencyInjection\Container->get('scri in /builds/acme/vendor/laupifrpar/pusher-bundle/DependencyInjection/PusherFactory.php on line 26
The laupiFrpar/LopiPusherBundle
does not have the 3.0.0
dependency though:
"pusher/pusher-php-server": ">=2.2.1"
... so, maybe this can be fixed and released as 2.6.5 so the bundle does not break.
What about adding https://styleci.io/ support? Then we will get syntax checks on all commits and PRs made to this repository.
This is the guide to add StyleCI support:
Sign up at https://styleci.io/
Go to the repositories page https://styleci.io/account#repos
Enable this repository.
Press Show Analysis and then Settings.
Add the following settings:
enabled:
- unalign_double_arrow
disabled:
- psr0
- align_double_arrow
- phpdoc_align
Press Save Configuration.
Press Analyze Now.
This has to be done by the owner of the repository.
I'd like to bring this library up to feature parity with pusher-gem
where possible.
can u guys implement the feature of having users info as described in http API documentation?
i cant find this anywhere
I am trying to use this library to set up authentication for my iOS application, but am getting stuck authorizing. Here is the code that I am using to generate my hash:
socket_auth($_POST['channel_name'], $_POST['socket_id']); ?>And this is the response shown in the Pusher debug console:
Invalid signature: Expected HMAC SHA256 hex digest of 44503.19289639:private-test-channel, but got 238c4f523215b154d06646940bf3b763e2e4934c3a61d8c0fe3f654f37f2695c
Am I missing something really stupid?
A cluster ID (eu
/us-east2
) and the URL is formed for the developer. This is how things work across all recently libraries.
$options = array('cluster' => 'eu');
$pusher = new Pusher('app-id', 'app-key', 'app-secret', $options);
The HTTP API ($baseUrl
) will be:
https://api-eu.pusher.com
If `` were provided as the cluster ID then the URL would be:
https://api-useast2.pusher.com
IMO we should drop PHP 5.3 and 5.4 support and go with 5.5.9 as the minimum requirement. PHP 5.4 isn't supported anymore, it wont receive any security fixes as of september 2015.
The README for the 3.0.0 release should adhere to the HTTP library template:
https://github.com/pusher/pusher-http-library-template
Would you accept a PR to move to PSR-2?
Hi,
Someone I spoke to on ZenDesk mentioned that when running the following code:
<?php
require('Pusher.php');
$options = array(
'encrypted' => true
);
$pusher = new Pusher(
'xxx',
'xxx',
'xxx',
$options
);
$data['message'] = 'hello world';
$pusher->trigger('test_channel', 'my_event', $data);
?>
... He or she received the following response:
array(2) { ["body"]=> bool(false) ["status"]=> int(0)
A status code of 0
is quite pecuilar, no?
@jameshfisher and I did some research and found this StackOverflow answer, which states:
If you connect with the server, then you can get a return code from it, otherwise it will fail and you get a 0. So if you try to connect to "www.google.com/lksdfk" you will get a return code of 400, if you go directly to google.com, you will get 302 (and then 200 if you forward to the next page... well I do because it forwards to google.com.br, so you might not get that), and if you go to "googlecom" you will get a 0 (host no found), so with the last one, there is nobody to send a code back.
I have asked the user to enable debugging and to report back. In the meantime, @jameshfisher suggested that I open an issue requesting a more intuitive error in the case that the status
is 0
.
$options = array('encrypted' => true);
$pusher = new Pusher('app-id', 'app-key', 'app-secret', $options);
The default for this options should be true
and thus the URL should be HTTPS by default.
When I provide curl_options array in the pusher constructor, the curl is not configured properly.
Actually, the settings array in Pusher is not affected by curl_options because of this line : https://github.com/pusher/pusher-http-php/blob/master/lib/Pusher.php#L147 (curl_options is not defined in the initial declaration of $settings)
Can you confirmed if it is a bug ? Can I PR ?
See: http://php.net/manual/en/function.curl-error.php
Right now the status code is set to 0 and the body blank. More information should be supplied to make it easier to debug what's going on with the library.
See title.
make test
returns the following error on my system:
1) Pusher\Tests\FileAdapterTest::testRequest
Notice: Undefined index: _SERVER in /Users/zimbatm/code/github.com/pusher/pusher-http-php/vendor/phpunit/phpunit/src/Util/Filter.php on line 28
Pusher\Exception\AdapterException: file_get_contents(https://pusher.com): failed to open stream: operation failed
/Users/zimbatm/code/github.com/pusher/pusher-http-php/src/Http/FileAdapter.php:73
/Users/zimbatm/code/github.com/pusher/pusher-http-php/tests/FileAdapterTest.php:38
/Users/zimbatm/code/github.com/pusher/pusher-http-php/vendor/phpunit/phpunit/phpunit:36
However SSL seem to be enabled and stream_get_contents() to support including remote files:
$ php -a
Interactive shell
php > var_dump(stream_get_wrappers());
array(12) {
[0]=>
string(5) "https"
[1]=>
string(4) "ftps"
[2]=>
string(13) "compress.zlib"
[3]=>
string(14) "compress.bzip2"
[4]=>
string(3) "php"
[5]=>
string(4) "file"
[6]=>
string(4) "glob"
[7]=>
string(4) "data"
[8]=>
string(4) "http"
[9]=>
string(3) "ftp"
[10]=>
string(4) "phar"
[11]=>
string(3) "zip"
}
php > var_dump(ini_get('allow_url_fopen'));
string(1) "1"
php > var_dump(extension_loaded('openssl'));
bool(true)
If I change the test domain to be google.com or github.com I get the same error.
Changing to plain HTTP doesn't have the same issue.
This replaces the current new-lib functionality that allows a string to be passed to the Pusher
constructor.
Dear Team,
I mentioned the triggerBatch function is not working, based on the example:
$batch = array();
$batch[] = array('channel' => 'my-channel', 'event' => 'my_event', 'data' => array('hello' => 'world'));
$batch[] = array('channel' => 'my-channel', 'event' => 'my_event', 'data' => array('myname' => 'bob'));
$pusher->triggerBatch($batch);
The problem with the 'event' key, it should be 'name' based on your documentation : https://pusher.com/docs/rest_api
This snippet is working like charm:
$batch = array();
$batch[] = array('channel' => 'my-channel', 'name' => 'my_event', 'data' => array('hello' => 'world'));
$batch[] = array('channel' => 'my-channel', 'name' => 'my_event', 'data' => array('myname' => 'bob'));
$pusher->triggerBatch($batch);
[RuntimeException]
File at "/var/www/api/vendor/pusher/pusher-php-server/lib//Pusher.php" does not exist, check your classmap definitions
Sorry, not really sure where to put this.
We are using the encrypted eu cluster.
Occasionally we are having errors with not being able to send events to Pusher.
After setting up some custom logging:
/*
* Create a custom logger that throws exceptions we can do something with.
*/
$this->pusher->set_logger(new class() {
public function log(string $message) {
if (\Core\Strings::startsWith($message, "Pusher: ERROR: exec_curl error:")) {
throw new Exception(str_replace("Pusher: ERROR: exec_curl error: ", "", $message));
}
if (\Core\Strings::startsWith($message, "Pusher: INFO: exec_curl response: ")) {
if (strpos($message, "[status] => 20") === false && preg_match("!\\[body] => (.*)!", $message, $matches)) {
throw new Exception($matches[1]);
}
}
}
});
We now know what the exact error is that we are getting:
Resolving timed out after 5514 milliseconds
We have this issue about 50 times per month. We are sending about 120K messages per day so it's not very frequent but still more than we would like.
We have many dozens of suppliers integrated and Pusher is the only one with resolving timeouts.
Yes, our timeout is set to 5 seconds (else it wouldn't really be real time). Our servers are located in Amsterdam close to the AIX.
I am using the following code:
`require('pusher-http-php-master/lib/Pusher.php');
error_reporting(E_ALL);
$app_id = '*';
$app_key = '*';
$app_secret = '*';
class MyLogger {
public function log( $msg ) {
print_r( $msg . "<br />" );
}
}
$pusher = new Pusher($app_key, $app_secret, $app_id);
$logger = new MyLogger();
$pusher->set_logger( $logger );
$data['message'] = 'hello world';
$result = $pusher->trigger('test_channel', 'my_event', array( 'hello' => 'world' ));
$logger->log( "---- My Result ---" );
$logger->log( $result );`
And the answer I have is:
Pusher: ->trigger received string channel "test_channel". Converting to array. Pusher: create_curl( http://api.pusherapp.com:80/apps/*/events?auth_key=*&auth_signature=*&auth_timestamp=*&auth_version=1.0&body_md5=*) Pusher: trigger POST: {"name":"my_event","data":"{\"hello\":\"world\"}","channels":["test_channel"]} Pusher: exec_curl response: Array ( [body] => Unknown auth_key [status] => 400 ) ---- My Result ---
What am I doing wrong?
What I want is to send notifications from the server.
How can we receive the user info that was provided in our auth endpoint when querying a presence channel? In auth, I return user info:
$userInfo = array(
'firstName' => $user->first_name,
'lastName' => $user->last_name,
'email' => $user->email,
'id' => $user->id,
);
return $this->pusher->presence_auth($this->request->post('channel_name'), $this->request->post('socket_id'), $user->id, $userInfo);
When I later call
$pusher->get('/channels/presence-1234/users')
Only the user IDs are returned.
Hii.
Pusher is a paid api is there any opensource?
I realized that with the new version of Pusher (3) it's changed the namespace for Pusher\Pusher and it's not updated in Broadcasts classes.... I solved and answered here: https://stackoverflow.com/questions/45052853/class-pusher-not-found/45325814#45325814
Via support: "It seems that your php library uses cURL which is not supported on GAE PHP"
On further inspection, it seems this is correct. If we want to support this platform, urlfetch
might be a better solution.
I get "Unknown auth key" when I try to use that lib.
But it works when I modify the source and make the api calls this url : http://api-eu.pusher.com(...)
See #40
The composer.json file says that your library supports PHP 5.2+. However, the fix done in #40 uses a private function as callback for array_walk
, which is only working on PHP 5.4+.
There is 3 ways to fix it:
validate
method to be public (and documenting it as internal with @internal
)It would be really nice to see tagged releases that are in sync with the JS version. That way I can stick my project to a specific JS + PHP version.
@zimbatm made a start on a new version of the pusher-http-php library (https://github.com/pusher/pusher-http-php/tree/new-lib) and the branch has a fair amount of people using it, notably because it lets people specify the host / cluster that they want to publish to, which the current version (on master) did not let you do for some time. That's not the only improvement though.
We should decide what we want to do with the new-lib branch and potentially make it the new version of the library on master, and release a new version.
Do not close the cURL handler after request, so that it could reuse the existing connection.
Example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://api.pusherapp.com");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_exec ($ch);
curl_exec ($ch);
Output:
* Rebuilt URL to: http://api.pusherapp.com/
* Hostname was NOT found in DNS cache
* Trying 184.73.245.186...
* Connected to api.pusherapp.com (184.73.245.186) port 80 (#0)
> GET / HTTP/1.1
Host: api.pusherapp.com
Accept: */*
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
* Server thin is not blacklisted
< Server: thin
< Content-Length: 11
< Connection: keep-alive
<
Pusher API
* Connection #0 to host api.pusherapp.com left intact
* Found bundle for host api.pusherapp.com: 0x9f724b0
* Re-using existing connection! (#0) with host api.pusherapp.com <-- !!!
* Connected to api.pusherapp.com (184.73.245.186) port 80 (#0)
> GET / HTTP/1.1
Host: api.pusherapp.com
Accept: */*
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
* Server thin is not blacklisted
< Server: thin
< Content-Length: 11
< Connection: keep-alive
<
Pusher API
* Connection #0 to host api.pusherapp.com left intact
Output for same code but using curl_close($ch);
between requests:
* Rebuilt URL to: http://api.pusherapp.com/
* Hostname was NOT found in DNS cache
* Trying 54.225.207.125...
* Connected to api.pusherapp.com (54.225.207.125) port 80 (#0)
> GET / HTTP/1.1
Host: api.pusherapp.com
Accept: */*
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
* Server thin is not blacklisted
< Server: thin
< Content-Length: 11
< Connection: keep-alive
<
Pusher API
* Connection #0 to host api.pusherapp.com left intact
* Rebuilt URL to: http://api.pusherapp.com/
* Hostname was found in DNS cache
* Trying 54.225.207.125...
* Connected to api.pusherapp.com (54.225.207.125) port 80 (#1)
> GET / HTTP/1.1
Host: api.pusherapp.com
Accept: */*
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
* Server thin is not blacklisted
< Server: thin
< Content-Length: 11
< Connection: keep-alive
<
Pusher API
* Connection #1 to host api.pusherapp.com left intact
Would you accept a PR to use namespaces and PSR-4 autoloading?
It's not worth hitting the same issues when Guzzle already did all the work.
Bonus point for creating a Pusher mock around Guzzle's
Fixes #87
Logging has been essential in debugging interactions with the Pusher HTTP API. The 2.0 logging isn't perfect, but it is really useful. So we need the at least the same level of logging in 3.0.
The preference would be to be able to set a logger as an option when creating the Pusher
instance. The logger would implement to https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
I too am facing this same problem, I was expecting Pusher to be faster than this, 500ms here.. I'm not impressed Pusher.. The world operates faster than 500ms these days.. just ask google.
I say Pusher needs to be at to 80ms or faster per trigger! otherwise whats the point.
We want to keep the constructor as simple as possible. I'd suggest the use of the current Config
object adds additional and unnecessary complexity.
A key part of doing this will be providing default values for things that were otherwise extracted from the URL:
My vote is for one of the following:
$pusher = new Pusher('my-app-id', 'my-app-key', 'my-app-secret');
Additional options are added by a 4th options
parameter:
$pusher = new Pusher('my-app-id', 'my-app-key', 'my-app-secret', array('encrypted' => true));
$pusher = new Pusher(array(appId => 'id_value', appKey' => 'key_value', 'appSecret' => 'secret_value', ...);
Optional parameters are just added to the array:
$pusher = new Pusher(array(appId => 'id_value', appKey' => 'key_value', 'appSecret' => 'secret_value', 'encrypted' => true);
The benefit of this approach is that it reduces the changes of getting key and secret mixed up.
What do others think? @zimbatm
StyleCI requires the connected user to be an admin on the repo. The connected user is not an admin anymore so StyleCI has disabled itself.
The PHPDoc for dome of the functions is outdated or incorrect. Some examples:
$request_method
is missing/**
* Build the required HMAC'd auth string
*
* @param string $auth_key
* @param string $auth_secret
* @param string $request_path
* @param array $query_params
* @param string $auth_version [optional]
* @param string $auth_timestamp [optional]
* @return string
*/
public static function build_auth_query_string($auth_key, $auth_secret, $request_method, $request_path,
$query_params = array(), $auth_version = '1.0', $auth_timestamp = null)
$port
should be an int/**
* ...
* @param string $auth_key
* @param string $secret
* @param int $app_id
* @param bool $debug [optional]
* @param string $host [optional]
* @param int $port [optional]
* @param int $timeout [optional]
*/
public function __construct( $auth_key, $secret, $app_id, $debug = false, $host = 'http://api.pusherapp.com', $port = '80', $timeout = 30 )
$channels
is missing/**
* Trigger an event by providing event name and payload.
* Optionally provide a socket ID to exclude a client (most likely the sender).
*
* @param array $channel An array of channel names to publish the event on.
* @param string $event
* @param mixed $data Event data
* @param int $socket_id [optional]
* @param bool $debug [optional]
* @return bool|string
*/
public function trigger( $channels, $event, $data, $socket_id = null, $debug = false, $already_encoded = false )
The logging functionality is useful but pretty crude. Most importantly, there's no distinction between "log levels". It would be helpful, for example, to distinguish between basic informational event logging and error conditions (such as a failed trigger()).
The best way to do this would be by requiring that the $pusher->logger object implements the LoggerInterface specified in PSR-3:
http://www.php-fig.org/psr/psr-3/
If you're open to this it's quite easy to do and I'd be happy to provide a pull request.
Thanks,
Adam
I think we should bump the minimum supported php version to 5.3.3, or even better, 5.4.0.
Both php 5.2 and 5.3 are not maintained anymore by the php core team so it makes no sense to support them. Even php 5.4 is losing support soon.
Hi Guys,
Pls help me function to get histoty of chanel?
Thank you.
Fatal error: Cannot declare class PusherException (name already in use) when using socket_auth() on PHP 7.1.0.
Code to reproduce:
<?php
$pusher = new Pusher($pusher_key, $pusher_secret, $pusher_app_id);
echo $pusher->socket_auth($_POST['channel_name'], $_POST['socket_id']); ?>
Example output:
{"auth":"f9514363b770207e2644:0072c281df9b3a56e9b20ed498b3eefc19e0fa7b50252795bbd26c3f587360f0"}
Fatal error: Cannot declare class PusherException, because the name is already in use in pusher.php on line 26
I double-checked that this error was not because of my own code. Other pusher functions like sending a message are 100% working.
I guess this is because of namespaces changes in PHP 7.1...? Is there any way to fix this without breaking compatibility with other versions?
Since Laravel (Specifically Illuminate Broadcaster) tightly couples with this integration library, it is referring to Pusher without taking into account the introduction of a namespace.
The simplest workaround is simply change the references so that they are correct - a user has created an automation for this task here: https://stackoverflow.com/questions/45052853/class-pusher-not-found
Another workaround is just to continue using the previous version of this lib until Laravel update their end.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.