1

I have simple function in PHP that gives me

HTTP Error 500 (Internal Server Error):

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:

<?php
    invokeAuthenticationServiceAPI();
?>

As you can see, I need to send it to rest api server. But it does fail only on the string to json formatting.

I have two questions:

  1. Maybe I have done something that php doesn't like. Ok, but can I get some kind of error message and not "Error 500 (Internal Server Error)"?
  2. What am I doing wrong?
4

3 回答 3

2

您应该查看web-server错误日志以了解错误的详细信息,但从您发布的代码来看,问题可能是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生成正确的输出。

于 2013-06-14T15:21:52.500 回答
1

Remove double quotes around JSON and remove extra spaces which invalidate the PHP heredoc syntax

 $json = <<<"JSON"

should be

$json = <<<JSON

Like

<?php
$str = <<<JSON
{
                "auth":
                    {
                    "username":"foo",
                    "password":"bar"
                    }
            }

JSON;
echo $str;
?>
于 2013-06-14T15:22:52.803 回答
0
  1. 如果你有一个 500 Internal Server Error 你几乎肯定在 PHP 中有一个致命错误。根据您的代码,您可能会在错误日志中找到错误,或者您可能必须使用例如xdebug来调试您的代码。
  2. 您不需要在 JSON 周围加上引号。除此之外,您的代码没有任何问题。但是,您应该使用json_encode生成 JSON 。
于 2013-06-14T15:24:55.793 回答