在php中使用curl与faye发布数据

时间:2012-04-08 21:55:39

标签: php node.js faye

根据faye的作者,我可以从任何平台发送消息 使用curl发布消息的格式为:

curl -X POST http://192.168.1.101:8000/faye -H 'Content-Type:
    application/json' -d '{"channel":"/foo","data":{"hello":"world"}}'

我格式化了在php中使用的上一行

$data = array("channel" => "/one", "result" => "Hello World from PHP!!");  
$data_string=json_encode($data);
$ch = curl_init('http://192.168.1.101:8000/faye');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);                                                          
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);                                                              
curl_setopt($ch,CURLOPT_HTTPHEADER,array(
'Content-Type:application/json','Content-Length:'strlen($data_string)));
$result = curl_exec($ch);

不知何故,通道一的用户无法获得结果

java脚本中的发布函数可以无缝工作(参见下面的行)

var publication = client.publish('/one',{ result: 'Hello World from JS'});<br/>

请告诉我遗漏的内容或我的错误,谢谢

1 个答案:

答案 0 :(得分:0)

这个适用于我:

$data = array("channel" => "/one", "data" => array ("result"=>"HelloWorld from PHP!!"));
$data_string=json_encode($data);
$ch = curl_init('http://localhost:8000/faye');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);                                                          
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);                                                              
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:application/json'));
echo $result = curl_exec($ch);