我改变了一些算法后,这个功能停止了工作,我可能搞砸了。这是我的代码:
$data = (
[field1] => some_message
[field2] => some_message
[field3] => Array
(
[more] => information
)
)
function curlRequest($url,$querystring,$encoded_json = false){
$ch = curl_init();
$ch_options = array("Expect:");
if($encoded_json)
array_push($ch_options,"Content-type: application/json");
curl_setopt($ch, CURLOPT_HTTPHEADER, $ch_options);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $querystring);
curl_getinfo($ch);
curl_exec($ch);
$feedback = curl_getinfo($ch);
curl_close($ch);
return $feedback;
}
function notifyNode($data,$encoded_json = false) {
$node_url = 'http://127.0.0.1:'.$socket_port.'/posts';
if(is_string($data))
$data = array($data);
$querystring = http_build_query($data);
//querystring seems fine
$feedback = curlRequest($node_url,$querystring,$encoded_json);
}
如果在$data
中发送notifyNode
:
$querystring
似乎很好
curl_getinfo($ch);
(在exec
之前)转储此内容:
var_dump:
array(26) {
["url"]=>
string(27) "http://127.0.0.1:port/posts" (this is correct)
["content_type"]=>
NULL
["http_code"]=>
int(0)
["header_size"]=>
int(0)
["request_size"]=>
int(0)
["filetime"]=>
int(0)
["ssl_verify_result"]=>
int(0)
["redirect_count"]=>
int(0)
["total_time"]=>
float(0)
["namelookup_time"]=>
float(0)
["connect_time"]=>
float(0)
["pretransfer_time"]=>
float(0)
["size_upload"]=>
float(0)
["size_download"]=>
float(0)
["speed_download"]=>
float(0)
["speed_upload"]=>
float(0)
["download_content_length"]=>
float(-1)
["upload_content_length"]=>
float(-1)
["starttransfer_time"]=>
float(0)
["redirect_time"]=>
float(0)
["redirect_url"]=>
string(0) ""
["primary_ip"]=>
string(0) ""
["certinfo"]=>
array(0) {
}
["primary_port"]=>
int(0)
["local_ip"]=>
string(0) ""
["local_port"]=>
int(0)
}
我希望通过nodejs方面以json格式接收数据,所以我真的不明白发生了什么。 Tyvm求助
答案 0 :(得分:0)
对你的卷曲选项进行一些修正:
而不是:
$ch_options = array("Expect:");
if($encoded_json)
array_push($ch_options,"Content-type: application/json");
curl_setopt($ch, CURLOPT_HTTPHEADER, $ch_options);
使用
$ch_options = array("Expect:");
if($encoded_json){
array_push($ch_options,"Content-Type: application/json");
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $ch_options);
curl_setopt($ch, CURLOPT_HEADER, 1);
...
如果要以json格式将数据从PHP发送到NodeJS,则传入CURLOPT_POSTFIELDS
选项json_encoded data:
...
$querystring = jsone_encode($data);
...