【问题标题】:Can't post cURL from php to nodejs无法将 cURL 从 php 发布到 nodejs
【发布时间】:2015-12-17 14:31:07
【问题描述】:

我更改了一些算法后,此功能停止工作,我可能搞砸了。这是我的代码:

$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 为您提供帮助

【问题讨论】:

    标签: php node.js curl


    【解决方案1】:

    在您的 curl 选项中进行一些更正:
    而不是:

    $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 数据:

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

    【讨论】:

    • 所以如果我想传递json 我不 http_query_string 数据?你确定吗?
    • 你想发送json还是接收json作为nodejs的响应?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2018-02-12
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    相关资源
    最近更新 更多