通过指定格式的curl发送数据? [英] Sending data through curl in a specified format?

查看:131
本文介绍了通过指定格式的curl发送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函数可以在php和nodejs之间通信:

I've the following function to communicate between php and nodejs:

function toNode($data){

    //$data is array
    //$data example
    //$data = array("one"=>"yes","two"=>"no","three"=>array("yet"=>"another"))

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:'.$socket_port.'/posts');

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);

    curl_setopt($ch, CURLOPT_POST, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_exec($ch);

    $feedback = curl_getinfo($ch);

}



这里是nodejs console.log 收到的数据:

 { one: 'yes',
 two: 'no',
'three[0][yet][0]':'another'}         //<---


b $ b

这里是在nodejs端的数据显示方式:

And here is how I want the data to appear in nodejs side:

{ one: 'yes',
two: 'no',
three: [ { yet: 'another' } ] }       //<---

我该如何使用curl实现这一点?我已经尝试在收到的信息nodejs侧使用此函数

How exactly can I make this happen with curl? I've tried using this function on received information nodejs side

function urldecode(str) {
   return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}

但它失败,输出什么如果我记得正确。此外,用于进一步处理数据的函数在来自php curl或来自nodejs(不是url编码)的内容之间共享,因此,最好的方法是在php端解决问题...有人可以帮我吗?非常感谢...

but it fails, outputing nothing If I remember correctly. Also, the function used further to treat the data is shared between what comes from php curl or natively in nodejs (which is not url encoded) so, the best would be to solve the problem right in php side... Could someone help me here? Thank you very much...

编辑:包括解析nodejs侧的信息:

including parsing of information nodejs side:

app.post('/posts', function(req, res){

   if(req.headers.host == '127.0.0.1:'+socket_port) {
        if(req.method == 'POST') {
            var form = new formidable.IncomingForm();
            form.parse(req, function(err, fields, files) {

                res.writeHead(200, [[ "Content-Type", "text/plain"]
                        , ["Content-Length", 0]
                        ]);
                res.write('');
                res.end();

                furtherFunction(fields);                
            });
        }
    }

});


推荐答案

我理解,你想要这样发送json数据。我没有看到任何json数据。我googgled它。在这里 send-json-post-using-php ,我找到了如何发送json数据在php。希望它会帮助。

What i understand, you want so send json data. I haven't seen any json data here. I googgled it. Here send-json-post-using-php, i found how to send json data in php. Hope it will help.

这篇关于通过指定格式的curl发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆