Angular 8到PHP后端
我正在用PHP后端设置一个Angular应用程序。 正文是在我的应用程序中生成的。但是如果我想通过http和file_get_contents将主体发送到我的PHP文件中('php://input'); 我收到一个HTTP错误。如果我vardump$json,我会得到一个空字符串。如果我通过PHP文件中的$elasticBody='{}'手动执行Body查询,那么Body查询工作得很好。 这是我的http.service.tsAngular 8到PHP后端,php,angular,
elasticsearch,Php,Angular,
elasticsearch,我正在用PHP后端设置一个Angular应用程序。 正文是在我的应用程序中生成的。但是如果我想通过http和file_get_contents将主体发送到我的PHP文件中('php://input'); 我收到一个HTTP错误。如果我vardump$json,我会得到一个空字符串。如果我通过PHP文件中的$elasticBody='{}'手动执行Body查询,那么Body查询工作得很好。 这是我的http.service.ts elastic_url: '../elastic.php'
elastic_url: '../elastic.php'
Search(body: any): Observable<any> {
const proxyBody = {
body: body
};
return this.http.post('/path to elastic.php', proxyBody, {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
});
}
elastic_url:'../elastic.php'
搜索(主体:任何):可观察{
常量代理体={
身体:身体
};
返回此.http.post('/path to elastic.php',proxyBody{
标题:新的HttpHeaders({
“内容类型”:“应用程序/json”
})
});
}
elastic.php
<?php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
// $elasticBody = $request;
// $elasticBody = $data['body'];
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');
$ch = curl_init();
$method = "POST";
$url = "http://localhost:9210/fud_fret/_search";
$elasticBody = '{
"query": {
"multi_match" : {
"query" : "test",
"fields" : ["136_title"]
}
}
}';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, 9210);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $elasticBody);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
您在PHP中遇到的错误是什么?PHP致命错误:未捕获错误:无法将stdClass类型的对象用作第16行elastic.PHP中的数组。。。。第16行是这里的$elasticBody=$data['body'];如果我删除/注释掉$elasticbody,一切正常。但是为什么呢?@SathishkumarRakkiasamy我用php错误编辑了我的问题。