I have a module that uses file_get_contents to read a file. Normally this function will create the $http_response_header var in the local scope. But if I enable the remote stream wrapper module I get an error saying Notice: Undefined variable: http_response_header

It seems to me that for some reason this variable is not created with this module enabled.

Comments

dave reid’s picture

Status: Active » Postponed (maintainer needs more info)

There's no reference to any kind of 'http_response_header' variable in the module? Where is the PHP notice coming from?

pgrond’s picture

The notice is coming from code in a custom module that connects to an Adlib database. This code is assumes there is a $http_repsonse_header in the local scope. Without the remote stream wrapper there is. I use file_get_contents, and that will create one. With the remote stream wrapper enabled the $http_response_header is not available anymore. This is the code that triggers the error:

$rawdata = @file_get_contents($fullurl, false, $context);
    switch ($type) {
      case 'image':
        // create AdlibImageResponse object, any request errors are handled in the constructor
        $response = new AdlibImageResponse($rawdata, $http_response_header);
        break;
      default:
        // create AdlibSearchResponse object, any request errors are handled in the constructor
        $response = new AdlibSearchResponse($rawdata, $http_response_header);
        break;
    }
    return $response;
dave reid’s picture

Status: Postponed (maintainer needs more info) » Postponed

Hrm. Since we're overriding the HTTP and HTTPS stream wrappers, this global variable is no longer available. I'm not really sure what we can do. I cannot duplicate what PHP is doing because I will not be able to emulate creating a local-scope variable that persists once file_get_contents() is executed.

pgrond’s picture

Hmm, I was guessing some override was the root of the problem ;).

I will try to figure out another way of doing this. Maybe remote stream wrapper is not the solution in this case.

dave reid’s picture

The alternative here is maybe to advise you to use drupal_http_request() rather than file_get_contents() to fetch your data since it provides you with a proper $request object back with all the data.

pgrond’s picture

That could be an alternative. The only thing is that the rest of the code for the connection with Adlib is Drupal agnostic. It's a library that could be used in any other PHP project. I have to think about it if we want to make it Drupal dependent for this.

dave reid’s picture

Yeah if you want to make it Drupal-agnostic, I'd advise requiring the cURL library and use those functions instead. You could make a Drupal "wrapper" layer on top of this that uses drupal_http_request() instead.

jrbeeman’s picture

Issue summary: View changes

Noting here for others that may run into the issue: I ran into exceptions with use of Composer Manager on a project that are related to this issue. Because remote stream wrappers is overriding the stream wrapper, Composer Manager was throwing exceptions and warnings when installing or updating packages. Fortunately, my project actually didn't need remote stream wrappers, so disabling the module solved the issue for me. I believe a workaround would be to not use `drush composer install` and instead install composer dependencies with just `composer install`.

becw’s picture

I ran into this issue recently with Acquia Search and the Solarium library: #2891977: Acquia Search uses a Solarium adapter that conflicts with the remote_stream_wrapper module

PHP's usage of the $http_response_header variable with file_get_contents() is very... quirky... and ideally new code shouldn't rely on such anachronistic behavior :)