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?
1
  • heredoc syntax requires that the first thing on the closing line is the closing tag - you can't put whitespace or tabs in front of it. Commented Jun 14, 2013 at 15:28

3 Answers 3

2

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; and the first time you use JSON it should not be quoted.

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.

Sign up to request clarification or add additional context in comments.

Comments

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;
?>

Comments

0
  1. If you have a 500 Internal Server Error you almost certainly have a fatal error in 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.
  2. You don't need quotes around JSON. Other than that there's nothing immediately wrong with your code. However you should generate JSON using json_encode.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.