当前位置 : 主页 > 网络编程 > PHP >

'Node'使用express模块无法收到phpCurl的值

来源:互联网 收集:自由互联 发布时间:2021-06-28
test.php //使用node+express开发http服务时可能遇到request.body为空或不是标准json的情况function httpPost($url, $data =null) {$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POSTFIELDS, $data
test.php
//使用node+express开发http服务时可能遇到request.body为空或不是标准json的情况
function httpPost($url, $data =null) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($ch);
	curl_close($ch);
	return $output;
}
//可做兼容
function httpPost2($url, $data =null, $isNode =false) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	if($isNode){
	    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("content-type: application/json"));
	    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
	}else{
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
	}
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($ch);
	curl_close($ch);
	return $output;
}
网友评论