[英]PHP cause the page HTTP Error 500 (Internal Server Error):
我有简单的PHP函数给我
HTTP错误500(内部服务器错误):
当我评论它时,简单的回声会被打印出来。
这是功能:
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;
*/
}
我在HTML文件中将其称为:
<?php
invokeAuthenticationServiceAPI();
?>
如您所见,我需要将其发送到rest api服务器。 但它只对字符串到json格式化失败。
我有两个问题:
您应该检查Web服务器错误日志以获取错误的详细信息,但是根据您发布的代码判断,问题可能是在heredoc JSON;
结束之前有空格JSON;
并且第一次使用JSON
时不应该引用它。
你应该用这个:
$json = <<<JSON
{
"auth":
{
"username":"foo",
"password":"bar"
}
}
JSON; // no spaces before JSON;
而不是这个:
$json = <<<"JSON"
{
"auth":
{
"username":"foo",
"password":"bar"
}
}
JSON;
虽然我个人会在php中生成一个数组或对象,并使用json_encode
生成正确的输出。
删除JSON
周围的双引号并删除使PHP heredoc语法无效的额外空格
$json = <<<"JSON"
应该
$json = <<<JSON
喜欢
<?php
$str = <<<JSON
{
"auth":
{
"username":"foo",
"password":"bar"
}
}
JSON;
echo $str;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.