【发布时间】:2013-06-14 15:17:50
【问题描述】:
我在 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 格式上失败。
我有两个问题:
- 也许我做了一些 php 不喜欢的事情。好的,但是我可以得到某种错误消息而不是“错误 500(内部服务器错误)”吗?
- 我做错了什么?
【问题讨论】:
-
heredoc 语法要求结束行的第一件事是结束标记 - 你不能在它前面放置空格或制表符。
标签: php error-handling