php发布请求--data
问题描述:
I try to make a request from my php code to another remote server which has Riak DB running. I need to make a map-reduce request so I have to do it with POST http.
Riak documentation says (for e.g.)
curl -XPOST http://`localhost`:8091/mapred \
-H 'Content-Type: application/json' \
-d '{
"inputs":"training",
"query":[{"map":{"language":"javascript",
"source":"function(riakObject) {
var m = riakObject.values[0].data.match(/pizza/g);
return [[riakObject.key, (m ? m.length : 0 )]];
}"}}]}'
How to do the same with PHP?
我尝试从我的php代码向另一个运行Riak DB的远程服务器发出请求。 我需要制作一个map-reduce请求,所以我必须使用POST http。 p>
Riak文档说(例如) p>
curl -XPOST http://`localhost`:8091 / mapred \
H'Content-Type:application / json'\
d'{
“inputs”:“training”,
“query”:[{ “map”:{“language”:“javascript”,
“source”:“function(riakObject){
var m = riakObject.values [0] .data.match(/ pizza / g);
return [[ riakObject.key,(m?m.length:0)]];
}“}}]}'
code> pre>
如何用PHP做同样的事情? p>
div>
答
http://php.net/manual/en/book.curl.php
You can use the PHP cUrl library for this. Be sure to enable it in the PHP modules.
Edit:
the --data flag tells cUrl to use POST, this is the PHP variant
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body)
Where the body variable can be the data, for example JSon.