php - PHP导致页面HTTP错误500(内部服务器错误):

标签 php error-handling

我在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格式格式化的字符串上确实失败。

我有两个问题:
  • 也许我做了php不喜欢的事情。好的,但是我可以得到某种错误消息,而不是“错误500(内部服务器错误)”吗?
  • 我在做什么错?
  • 最佳答案

    您应该检查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/

    相关文章:

    javascript - 切换 div 重定向到带有 ID 的链接

    django - 如何显示错误消息Django

    swift - 错误 : "protocol methods may not have bodies" in Swift

    error-handling - RxAndroid-使用Zip运算符处理错误

    php - register_shutdown_function() 仍然输出原始错误信息

    php - 使用 Git ‘去同步化’一个 PHP 配置文件

    asp.net - gridview System.NullReferenceException : Object reference not set to an instance of an object.

    python - 没有名为 'tweetProcesser' 的模块

    php - 使用 php 从动态命名变量中获取值

    php - 并非所有 $_Post 值都会发送到表。