简体   繁体   English

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

[英]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: 我有两个问题:

  1. Maybe I have done something that php doesn't like. 也许我已经做了一些php不喜欢的事情。 Ok, but can I get some kind of error message and not "Error 500 (Internal Server Error)"? 好的,但是我可以得到某种错误消息而不是“错误500(内部服务器错误)”吗?
  2. What am I doing wrong? 我究竟做错了什么?

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;
?>
  1. If you have a 500 Internal Server Error you almost certainly have a fatal error in PHP. 如果您有500内部服务器错误,您几乎肯定在PHP中有致命错误。 Depending on your code you may find the error in your error logs or you may have to debug your code using for example xdebug . 根据您的代码,您可能会在错误日志中找到错误,或者您可能必须使用xdebug调试代码。
  2. You don't need quotes around JSON. 你不需要JSON周围的引号。 Other than that there's nothing immediately wrong with your code. 除此之外,您的代码没有任何问题。 However you should generate JSON using json_encode . 但是,您应该使用json_encode生成JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM