我在PHP中有简单的功能,
HTTP Error 500 (Internal Server Error):
当我评论它时,简单的回显确实会被打印出来。
这是函数:
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
{
"auth":
{
"username":"foo",
"password":"bar"
}
}
JSON; // no spaces before JSON;
代替这个:
$json = <<<"JSON"
{
"auth":
{
"username":"foo",
"password":"bar"
}
}
JSON;
虽然我个人会在php中生成数组或对象,然后使用
json_encode
生成正确的输出。
关于php - PHP导致页面HTTP错误500(内部服务器错误):,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17111823/