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:
- 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)"?
- What am I doing wrong?