-2

I'm trying to access a list of most recent comments on Stack Overflow using this PHP:

<?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", "");
?>

However, when I run it, I get this error message:

$ 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

Does anyone have any ideas as to what might be causing this, and what one would do to fix it?

hichris123
  • 10,145
  • 15
  • 56
  • 70
Undo
  • 25,519
  • 37
  • 106
  • 129
  • 1
    @zerkms That's only one error and not an important one at this time. The real error is that `fopen` is failing and nobody knows why because of error supression – Phil Oct 04 '13 at 02:48
  • 1
    @Phil: the important is to **read**. Even with `@` his next question would be "Why do I have an unhandled exception". Which points directly to `if (!$fp)` condition. And so on. – zerkms Oct 04 '13 at 02:49
  • I get *failed to open stream: HTTP request failed! HTTP/1.1 411 Length Required*. Pretty simple really – Phil Oct 04 '13 at 02:51
  • possible duplicate of [Detailed error on fopen](http://stackoverflow.com/questions/2470217/detailed-error-on-fopen) – Phil Oct 04 '13 at 02:53

2 Answers2

5

The API, in this situation, should be called from the method get.

If you do visit the API at this link: https://api.stackexchange.com/2.1/comments?order=desc&sort=creation&site=stackoverflow
You are presented with a nice JSON with all the information you want.

If instead, you fix up the post params:

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

You are shown this instead:

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

Hopefully you know that you can simply just use file_get_contents to contact the API with the traditional get.

$json = json_decode(file_get_contents("https://api.stackexchange.com/2.1/comments?order=desc&sort=creation&site=stackoverflow"), true);
Dave Chen
  • 10,887
  • 8
  • 39
  • 67
-1

As the error said php_errormsg is your problem.

You have to make sure errors are tracked before assuming php_errormsg is defined.

you should turn the track_errors configuration option on before trying to get the value of php_errormsg, example :

<?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", "");

EDIT:

Also in the php.ini conficuration file you can set :

track_errors = On

IMPORTANT: From the PHP website

While $php_errormsg is a global, it is not a superglobal.

You'll have to qualify it with a global keyword inside a function.

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

EDIT 2 : USE of Global variable = bad practice link link

Community
  • 1
  • 1
Jeffrey Nicholson Carré
  • 2,950
  • 1
  • 26
  • 44
  • This is completely wrong... You have no idea what your talking about. The problem was an I caught exception. You can't throw an exception and not catch it... The program will terminate. Why is he even throwing exceptions if he is not handling them with catch blocks? The problem was with fopen failing and the exception being thrown and not caught. Don't throw an exception if your not going to handle it unless u want ur program to crash. Typical php behavior – Charles D Pantoga Oct 04 '13 at 04:23
  • No offense dude but you just clearly don't know what your talking about at all. The variable caused a notice. The exception caused a fatal error. Exceptions cause the stack to unwind and therefor must be handled appropriately. Read the up voted answer. The problem was not using the restful API correctly. And also throwing exceptions for no reason except to make the program crash. Who throws and doesn't catch? Weak shit right there – Charles D Pantoga Oct 04 '13 at 04:27