我正在尝试使用此 PHP 访问有关 Stack Overflow 的最新评论列表:
<?php
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
echo do_post_request("https://api.stackexchange.com/2.1/comments?order=desc&sort=creation&site=stackoverflow", "");
?>
但是,当我运行它时,我收到以下错误消息:
$ php index.php
PHP Notice: Undefined variable: php_errormsg in /var/www/secomments/index.php on line 14
PHP Fatal error: Uncaught exception 'Exception' with message 'Problem with https://api.stackexchange.com/2.1/comments?order=desc&sort=creation&site=stackoverflow, ' in /var/www/secomments/index.php:14
Stack trace:
#0 /var/www/secomments/index.php(22): do_post_request('https://api.sta...', '')
#1 {main}
thrown in /var/www/secomments/index.php on line 14
是否有人对可能导致此问题的原因有任何想法,以及如何解决此问题?