2

根据 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/>

请让我知道缺少什么或我的错误谢谢

4

1 回答 1

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);
于 2017-03-25T09:30:26.687 回答