Heim >Backend-Entwicklung >PHP-Tutorial >php-linux-环境-开发 - Linux 下php使用 Pusher 遇到一个难题
最近在给赏金猎人做新的网站的时候想使用 Pusher 做一个首页的吐嘈,我在本地测试都ok之后将它部署到了Linux上,可是打开之后却发现没法实现消息发出,打开Pusher的控制台察看信息是显示通道连接了,但是发送消息的事件没有执行。我打开本地的(本地的和部署的使用同一个Pusher应用),本地发的东西可以显示在部署的那个上面。看了半天都没找到问题所在。
整个项目的结构如下:
main.js
文件内容:
<code>(function($){ var pusher = new Pusher('18f9924cb7ee44a01ec0'), channel = pusher.subscribe('imgondar'); Pusher.log = function( msg ) { if( console && console.log ) { console.log( msg ); } }; channel.bind('send_message', function (data) { Snarl.addNotification({ title: data.name, text: data.msg, timeout: 5000 }); }); $('form').submit(function(){ var say = $('#message').val(); if ( say ) { $.post('post.php', $(this).serialize()); $('#message').val('').focus(); } return false; }); })(jQuery); </code>
post.php
文件内容:
<code><?php require './lib/Pusher.php'; $app_id = '111087'; $app_key = '18f9924cb7ee44a01ec0'; $app_secret = '09c7dd1a71a92c90ce2d'; $pusher = new Pusher($app_key, $app_secret, $app_id); $data = array( 'name' => htmlentities( getName().':' ), 'msg' => htmlentities(strip_tags($_REQUEST['message'])), ); $pusher->trigger('imgondar', 'send_message', $data); function getName(){ $name_array = array(0 => "小龙女",1 => "杨过",2 => "金庸",3 => "郭靖",4 => "黄蓉",5 => "欧阳锋",6 => "段誉",7 => "语嫣",8 => "虚竹",9 => "牧尘",10 => "Java",11 => "JavaScript",12 => "Python",13 => "C++", 14 => "HTML",15 => "什么鬼~~",16 => "想不出来了^^",17 => "阿列",18 => "就是想吐槽",19 => "路人甲",20 => "路人乙",21 => "跑龙套",22 => "主角",23 => "我是女主",24 => "我只是奴婢",25 => "请叫我容嬷嬷",26 => "我会降龙十八掌",27 => "小白",28 => "无语了!-_-",29 => "乔峰",30 => "不良人", ); $index = rand(0, count($name_array)-1); return $name_array[$index]; } </code>
本地http://127.0.0.1/imgondarv8_test/访问之后点击发送,在浏览器看到如下效果:
因为我在 main.js
代码中让其输出日志,打开浏览器的控制台看到:
打开Pusher的控制台:
现在问题来了,我把通过 svn 把代码部署到了Linux服务器上,通过如下域名访问:
http://testgondar.helloarron.com/
点击发送后没有效果,Pusher控制台和浏览器控制台信息如下:
虽然连接了但是消息没有发送成功。
但是我用本地的 http://127.0.0.1/imgondarv8_test/ 发消息可以在部署好的那个什么接收到(毕竟使用的是同一个Pusher 应用),在浏览器控制台显示接受到的信息:
测试了半天就是post.php里面的$pusher->trigger('imgondar', 'send_message', $data);
在Linux下面没有执行。
查看了Pusher.php
文件的trigger
函数:
<code>public function trigger ( $channel, $event, $payload, $socket_id = null, $debug = false, $already_encoded = false ) { $ch = curl_init(); if ( $ch === false ){ die( 'Could not initialise cURL!' ); } # Add channel to URL.. $s_url = $this->settings['url'] . '/channels/' . $channel . '/events'; # Build the request $signature = "POST\n" . $s_url . "\n"; $payload_encoded = $already_encoded ? $payload : json_encode( $payload ); $query = "auth_key=" . $this->settings['auth_key'] . "&auth_timestamp=" . time() . "&auth_version=1.0&body_md5=" . md5( $payload_encoded ) . "&name=" . $event; # Socket ID set? if ( $socket_id !== null ){ $query .= "&socket_id=" . $socket_id; } # Create the signed signature... $auth_signature = hash_hmac( 'sha256', $signature . $query, $this->settings['secret'], false ); $signed_query = $query . "&auth_signature=" . $auth_signature; $full_url = $this->settings['server'] . ':' . $this->settings['port'] . $s_url . '?' . $signed_query; # Set cURL opts and execute request curl_setopt( $ch, CURLOPT_URL, $full_url ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array ( "Content-Type: application/json" ) ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_POST, 1 ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload_encoded ); curl_setopt( $ch, CURLOPT_TIMEOUT, $this->settings['timeout'] ); $response = curl_exec( $ch ); curl_close( $ch ); if ( $response == "202 ACCEPTED\n" && $debug == false ){ return true; } elseif ( $debug == true || $this->settings['debug'] == true ) { return $response; } else { return false; } } </code>
我觉得可能是linux下的php环境没有开启curl
模块,于是自己写了一个简单的抓取百度首页的例子,发现服务器上的curl
是开启的。实在是找不到其他原因了,还请大神们给支支招。谢谢^^
最近在给赏金猎人做新的网站的时候想使用 Pusher 做一个首页的吐嘈,我在本地测试都ok之后将它部署到了Linux上,可是打开之后却发现没法实现消息发出,打开Pusher的控制台察看信息是显示通道连接了,但是发送消息的事件没有执行。我打开本地的(本地的和部署的使用同一个Pusher应用),本地发的东西可以显示在部署的那个上面。看了半天都没找到问题所在。
整个项目的结构如下:
main.js
文件内容:
<code>(function($){ var pusher = new Pusher('18f9924cb7ee44a01ec0'), channel = pusher.subscribe('imgondar'); Pusher.log = function( msg ) { if( console && console.log ) { console.log( msg ); } }; channel.bind('send_message', function (data) { Snarl.addNotification({ title: data.name, text: data.msg, timeout: 5000 }); }); $('form').submit(function(){ var say = $('#message').val(); if ( say ) { $.post('post.php', $(this).serialize()); $('#message').val('').focus(); } return false; }); })(jQuery); </code>
post.php
文件内容:
<code><?php require './lib/Pusher.php'; $app_id = '111087'; $app_key = '18f9924cb7ee44a01ec0'; $app_secret = '09c7dd1a71a92c90ce2d'; $pusher = new Pusher($app_key, $app_secret, $app_id); $data = array( 'name' => htmlentities( getName().':' ), 'msg' => htmlentities(strip_tags($_REQUEST['message'])), ); $pusher->trigger('imgondar', 'send_message', $data); function getName(){ $name_array = array(0 => "小龙女",1 => "杨过",2 => "金庸",3 => "郭靖",4 => "黄蓉",5 => "欧阳锋",6 => "段誉",7 => "语嫣",8 => "虚竹",9 => "牧尘",10 => "Java",11 => "JavaScript",12 => "Python",13 => "C++", 14 => "HTML",15 => "什么鬼~~",16 => "想不出来了^^",17 => "阿列",18 => "就是想吐槽",19 => "路人甲",20 => "路人乙",21 => "跑龙套",22 => "主角",23 => "我是女主",24 => "我只是奴婢",25 => "请叫我容嬷嬷",26 => "我会降龙十八掌",27 => "小白",28 => "无语了!-_-",29 => "乔峰",30 => "不良人", ); $index = rand(0, count($name_array)-1); return $name_array[$index]; } </code>
本地http://127.0.0.1/imgondarv8_test/访问之后点击发送,在浏览器看到如下效果:
因为我在 main.js
代码中让其输出日志,打开浏览器的控制台看到:
打开Pusher的控制台:
现在问题来了,我把通过 svn 把代码部署到了Linux服务器上,通过如下域名访问:
http://testgondar.helloarron.com/
点击发送后没有效果,Pusher控制台和浏览器控制台信息如下:
虽然连接了但是消息没有发送成功。
但是我用本地的 http://127.0.0.1/imgondarv8_test/ 发消息可以在部署好的那个什么接收到(毕竟使用的是同一个Pusher 应用),在浏览器控制台显示接受到的信息:
测试了半天就是post.php里面的$pusher->trigger('imgondar', 'send_message', $data);
在Linux下面没有执行。
查看了Pusher.php
文件的trigger
函数:
<code>public function trigger ( $channel, $event, $payload, $socket_id = null, $debug = false, $already_encoded = false ) { $ch = curl_init(); if ( $ch === false ){ die( 'Could not initialise cURL!' ); } # Add channel to URL.. $s_url = $this->settings['url'] . '/channels/' . $channel . '/events'; # Build the request $signature = "POST\n" . $s_url . "\n"; $payload_encoded = $already_encoded ? $payload : json_encode( $payload ); $query = "auth_key=" . $this->settings['auth_key'] . "&auth_timestamp=" . time() . "&auth_version=1.0&body_md5=" . md5( $payload_encoded ) . "&name=" . $event; # Socket ID set? if ( $socket_id !== null ){ $query .= "&socket_id=" . $socket_id; } # Create the signed signature... $auth_signature = hash_hmac( 'sha256', $signature . $query, $this->settings['secret'], false ); $signed_query = $query . "&auth_signature=" . $auth_signature; $full_url = $this->settings['server'] . ':' . $this->settings['port'] . $s_url . '?' . $signed_query; # Set cURL opts and execute request curl_setopt( $ch, CURLOPT_URL, $full_url ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array ( "Content-Type: application/json" ) ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_POST, 1 ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload_encoded ); curl_setopt( $ch, CURLOPT_TIMEOUT, $this->settings['timeout'] ); $response = curl_exec( $ch ); curl_close( $ch ); if ( $response == "202 ACCEPTED\n" && $debug == false ){ return true; } elseif ( $debug == true || $this->settings['debug'] == true ) { return $response; } else { return false; } } </code>
我觉得可能是linux下的php环境没有开启curl
模块,于是自己写了一个简单的抓取百度首页的例子,发现服务器上的curl
是开启的。实在是找不到其他原因了,还请大神们给支支招。谢谢^^
那位大神帮我解决一下啊,急需
看下你的php代码是否有执行权限!
curl问题 其实你打开Puser.php调试模式 会发现返回的status=0
只要在Pusher.php里trigger方法curl那块加上
<code>curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);</code>
这样就可以了