将数据从Drupal发送到node.js

我是一个drupal的新手。

我试图从Drupal发送数据到node.js函数,并将节点中的数据保存到mongo中。

我在php中写了一个函数

function sample_working_submit($form, &$form_state) { //Function to connect with node and save into Mongo using node $Name = check_plain($form_state['values']['Name']); $Age = check_plain($form_state['values']['Age']); $request_url = "http://10.20.5.112:3001/save/$Name/$Age "; $response = drupal_http_request($request_url); } 

只要名称(input)之间没有“空格”,这就行得通了。 如何保存input与空格。为什么这个问题来了?

我如何重写函数作为后?

 <?php $url = http://localhost:8080/myservlet; $data = array ( 'name' => check_plain($form_state['values']['Name']), 'age' => check_plain($form_state['values']['Age']) ); $response = drupal_http_request($url, $headers, 'POST', json_encode($data)); ?> 

这将数据发送到URL。 请注意,您需要更改服务器端的逻辑以接收POST数据而不是GET数据。

请参阅这里了解更多信息

如果名称之间的空格是问题,请尝试使用urlencode ()。

代码将如下所示:

 $Name = urlencode(check_plain($form_state['values']['Name'])); 

对于POST请求,我使用SimpleBrowser而不是drupal_http_request()。 这很容易使用,你将能够以任何forms传递string。