【问题标题】:Getting an uncaught exception in PHP when trying to get data from POST API尝试从 POST API 获取数据时在 PHP 中获取未捕获的异常
【发布时间】:2013-10-04 01:32:48
【问题描述】:

我正在尝试使用此 PHP 访问 Stack Overflow 上最近的 cmets 列表:

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

是否有人对可能导致此问题的原因以及如何解决此问题有任何想法?

【问题讨论】:

  • @zerkms 这只是一个错误,目前并不重要。真正的错误是 fopen 失败了,没有人知道为什么因为错误抑制
  • @Phil:重要的是阅读。即使使用@,他的下一个问题也是“为什么我有一个未处理的异常”。直接指向if (!$fp) 条件。以此类推。
  • 打开流失败:HTTP 请求失败! HTTP/1.1 411 长度要求。真的很简单
  • Detailed error on fopen 的可能重复项

标签: php post fatal-error stackexchange-api


【解决方案1】:

在这种情况下,应该从方法 get 调用 API。

如果您确实通过此链接访问 API:https://api.stackexchange.com/2.1/comments?order=desc&amp;sort=creation&amp;site=stackoverflow
您会看到一个漂亮的 JSON,其中包含您想要的所有信息。

如果相反,您修复帖子参数:

$params = array('http' => array(
    'method' => 'POST',
    'content' => $data,
    'header' => 'Content-Length: ' . strlen($data)
));

你会看到这个:

{"error_id":404,"error_name":"no_method","error_message":"this method cannot be called this way"}

希望您知道,您可以简单地使用file_get_contents 以传统方式获取 API。

$json = json_decode(file_get_contents("https://api.stackexchange.com/2.1/comments?order=desc&sort=creation&site=stackoverflow"), true);

【讨论】:

    【解决方案2】:

    正如错误所说,php_errormsg 是你的问题。

    您必须确保在定义 php_errormsg 之前跟踪错误。

    在尝试获取php_errormsg 的值之前,您应该打开track_errors 配置选项,例如:

    <?php
        function do_post_request($url, $data, $optional_headers = null)
        {
          error_reporting(E_ALL);
          ini_set('track_errors', 1);
    
           global $php_errormsg;
    
          $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.ini 配置文件中设置:

    track_errors = On
    

    重要提示:来自PHP website

    虽然$php_errormsg 是全局变量,但它不是超全局变量。

    您必须在函数中使用全局关键字对其进行限定。

    <?php function checkErrormsg() {
        global $php_errormsg;
        @strpos();
        return $php_errormsg; } ?>
    

    编辑 2: 使用全局变量 = 不好的做法 link link

    【讨论】:

    • 这是完全错误的......你不知道你在说什么。问题是我捕获的异常。您不能抛出异常而不捕获它......程序将终止。如果他不使用 catch 块处理异常,他为什么还要抛出异常?问题在于 fopen 失败并且异常被抛出并且没有被捕获。如果您不打算处理它,请不要抛出异常,除非您希望您的程序崩溃。典型的 php 行为
    • 无意冒犯老兄,但您显然根本不知道您在说什么。该变量引起了通知。该异常导致了致命错误。异常会导致堆栈展开,因此必须适当处理。阅读投票赞成的答案。问题是没有正确使用 restful API。除了使程序崩溃之外,还无缘无故地抛出异常。谁扔不接?弱点就在那里
    猜你喜欢
    • 2014-05-11
    • 1970-01-01
    • 2016-10-13
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多