pusher php,Linux 下php使用 Pusher 遇到一个难题
最近在給賞金獵人做新的網(wǎng)站的時候想使用 Pusher 做一個首頁的吐嘈,我在本地測試都ok之后將它部署到了Linux上,可是打開之后卻發(fā)現(xiàn)沒法實現(xiàn)消息發(fā)出,打開Pusher的控制臺察看信息是顯示通道連接了,但是發(fā)送消息的事件沒有執(zhí)行。我打開本地的(本地的和部署的使用同一個Pusher應用),本地發(fā)的東西可以顯示在部署的那個上面。看了半天都沒找到問題所在。
整個項目的結構如下:
main.js 文件內(nèi)容:
(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);
post.php 文件內(nèi)容:
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 => "跑龍?zhí)?#34;,22 => "主角",23 => "我是女主",24 => "我只是奴婢",25 => "請叫我容嬤嬤",26 => "我會降龍十八掌",27 => "小白",28 => "無語了!-_-",29 => "喬峰",30 => "不良人",
);
$index = rand(0, count($name_array)-1);
return $name_array[$index];
}
因為我在 main.js 代碼中讓其輸出日志,打開瀏覽器的控制臺看到:
打開Pusher的控制臺:
現(xiàn)在問題來了,我把通過 svn 把代碼部署到了Linux服務器上,通過如下域名訪問:
http://testgondar.helloarron.com/
點擊發(fā)送后沒有效果,Pusher控制臺和瀏覽器控制臺信息如下:
雖然連接了但是消息沒有發(fā)送成功。
但是我用本地的 http://127.0.0.1/imgondarv8_test/ 發(fā)消息可以在部署好的那個什么接收到(畢竟使用的是同一個Pusher 應用),在瀏覽器控制臺顯示接受到的信息:
測試了半天就是post.php里面的$pusher->trigger('imgondar', 'send_message', $data);在Linux下面沒有執(zhí)行。
查看了Pusher.php文件的trigger函數(shù):
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;
}
}
我覺得可能是linux下的php環(huán)境沒有開啟curl模塊,于是自己寫了一個簡單的抓取百度首頁的例子,發(fā)現(xiàn)服務器上的curl是開啟的。實在是找不到其他原因了,還請大神們給支支招。謝謝^^
總結
以上是生活随笔為你收集整理的pusher php,Linux 下php使用 Pusher 遇到一个难题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原子锁操作(全部)
- 下一篇: matlab平摆线曲率参数方程,参数方程