[英]PHP cause the page HTTP Error 500 (Internal Server Error):
I have simple function in PHP that gives me 我有简单的PHP函数给我
HTTP Error 500 (Internal Server Error): HTTP错误500(内部服务器错误):
When I comment it, the simple echo does get printed. 当我评论它时,简单的回声会被打印出来。
Here is the function: 这是功能:
error_reporting(E_ALL);
ini_set('display_errors', '1');
function invokeAuthenticationServiceAPI()
{
$json = <<<"JSON"
{
"auth":
{
"username":"foo",
"password":"bar"
}
}
JSON;
$data_string = json_decode($json);
echo $data_string;
/*
$ch = curl_init('https://apirestserverdemo/api');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
echo $result;
*/
}
I call it in the HTML file like this: 我在HTML文件中将其称为:
<?php
invokeAuthenticationServiceAPI();
?>
As you can see, I need to send it to rest api server. 如您所见,我需要将其发送到rest api服务器。 But it does fail only on the string to json formatting. 但它只对字符串到json格式化失败。
I have two questions: 我有两个问题:
You should check the web-server error log for the details of the error, but judging by the code you have posted, the problem is probably that there are spaces before the end of the heredoc JSON;
您应该检查Web服务器错误日志以获取错误的详细信息,但是根据您发布的代码判断,问题可能是在heredoc JSON;
结束之前有空格JSON;
and the first time you use JSON
it should not be quoted. 并且第一次使用JSON
时不应该引用它。
You should use this: 你应该用这个:
$json = <<<JSON
{
"auth":
{
"username":"foo",
"password":"bar"
}
}
JSON; // no spaces before JSON;
instead of this: 而不是这个:
$json = <<<"JSON"
{
"auth":
{
"username":"foo",
"password":"bar"
}
}
JSON;
Although personally I would generate an array or object in php and use json_encode
to generate the correct output. 虽然我个人会在php中生成一个数组或对象,并使用json_encode
生成正确的输出。
Remove double quotes around JSON
and remove extra spaces which invalidate the PHP heredoc syntax 删除JSON
周围的双引号并删除使PHP heredoc语法无效的额外空格
$json = <<<"JSON"
should be 应该
$json = <<<JSON
Like 喜欢
<?php
$str = <<<JSON
{
"auth":
{
"username":"foo",
"password":"bar"
}
}
JSON;
echo $str;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.