[英]PHP cause the page HTTP Error 500 (Internal Server Error):
我有簡單的PHP函數給我
HTTP錯誤500(內部服務器錯誤):
當我評論它時,簡單的回聲會被打印出來。
這是功能:
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格式化失敗。
我有兩個問題:
您應該檢查Web服務器錯誤日志以獲取錯誤的詳細信息,但是根據您發布的代碼判斷,問題可能是在heredoc JSON;
結束之前有空格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
生成正確的輸出。
刪除JSON
周圍的雙引號並刪除使PHP heredoc語法無效的額外空格
$json = <<<"JSON"
應該
$json = <<<JSON
喜歡
<?php
$str = <<<JSON
{
"auth":
{
"username":"foo",
"password":"bar"
}
}
JSON;
echo $str;
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.