我的Laravel PHP代码在无家可归的宅基地内运行。这是我的PHP函数,它将向js服务器发送http请求。
private function sendcampaign($data)
{
$client = new Client();
$res = $client->request('POST', 'http://127.0.0.1:8080/sendcampaign', ['json' => $data]);
$response = $res->getBody();
if($res->getStatusCode() == 200)
{
Log::info($response);
} else {
Log::error($response);
}
}
我的NodeJS服务器独立运行。我使用此命令nmp start
运行它。这是我的接受请求的nodejs服务器的代码
app.post("/sendcampaign", function(request, response) {
if (request.body) {
var campaign = JSON.stringify(request.body);
response.status(200);
response.send({
status: "success",
message: "Campaign sent successfully."
});
} else {
response.status(404);
response.send({ status: "error", message: "Campaign failed." });
}
return;
});
但是,当我运行它时,总是出现错误
cURL error 7: Failed to connect to 127.0.0.1 port 8080: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
但是我已经在Postman中尝试过了,回复是
{"status":"success","message":"Campaign sent successfully."}
我不知道使用GuzzleHttp的PHP代码有什么错误。也许我需要添加一些标题选项?我应该添加哪些标题选项?你能帮我吗?