2015-12-17 39 views
0

此功能停止工作后,我改变了我的一些算法,我可能会搞砸。这里是我的代码:无法从PHP发布到卷曲的NodeJS

$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); 

} 

如果$datanotifyNode发送:

$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

,让您的卷曲选择一些修正:
相反的:

$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); 
... 

如果你想从PHP JSON格式将数据发送到的NodeJS然后通过CURLOPT_POSTFIELDS选项json_encoded数据:

... 
$querystring = jsone_encode($data); 
... 
+0

所以,如果我想通过'json'我没有http_query_string的数据?你确定吗? – Fane

+0

无法正常工作。 'curl_getinfo'保持不变 – Fane

+0

你想发送JSON或JSON接收从响应的NodeJS? – RomanPerekhrest