用curl请求workerStart的信息,是不是无法获取到处理结果

nwh

workerStart的信息

$io->on('workerStart', function () use ($io) {
                $inner_http_worker = new Worker('http://0.0.0.0:' . (config('app.socket_port') - 1));
                $inner_http_worker->onMessage = function ($http_connection, $data) use ($io) {
                    $post = $data->post();
                    $get = $data->get();
                    $notice_type = $post['notice_type'] ?? 0;
                    if ($notice_type > 0) {
                        if ($notice_type == 1) {//全体
                            $io->to('pcUrl')->emit('new_notice', $post);
                        } else {
                            $to = $post['to'] ?? 0;
                            if ($to > 0) {
                                unset($post['to']);
                                $io->to($to)->emit('new_notice', $post);
                            }
                        }
                    return ['code'=>1];
                    }

                };
                $data = $inner_http_worker->listen();

            });

我的curl请求函数

    static function request_socket($url,$data=[],$is_decode=true,$data_encode=1){

        $curl = curl_init();
        $this_header = array(
            "content-type: application/x-www-form-urlencoded;charset=UTF-8;Accept’:'Application/json"
        );
        curl_setopt($curl,CURLOPT_HTTPHEADER,$this_header);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
//            $data=$data_encode==1?json_encode($data,JSON_UNESCAPED_UNICODE):$data;
//            return $data;
                curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);//请求时间 单位秒
        $output = curl_exec($curl);
        curl_close($curl);
        if($is_decode){
            return json_decode($output,true);
        }

        return $output;
    }

return Helpers::request_socket(127.0.0.1:2119,['to'=>408,'notice_type'=>2,'msg'=>'ceshi'],true,0);
结果不是 ['code'=>1]

阅读 120
2个回答

six

curl 加个 curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Expect:")); 试下

  • nwh 2021-11-13

    我把curl_setopt($curl,CURLOPT_HTTPHEADER,$this_header);换成curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Expect:"));还是无法得到,

  • nwh 2021-11-13

    我是本地测试的, 有影响吗

xiuwang

$inner_http_worker->onMessage 里你没有向客户端send数据啊,没发送数据,浏览器就一直转圈圈等数据。
$inner_http_worker->onMessage调用$http_connection->send(json_encode(['code'=>1])); 给浏览器返回数据试下吧

  • nwh 2021-11-13

    谢谢, 我以为这个不适用curl的请求就给删除了,成功了