views:

120

answers:

3

I have the strangest issue ever. I'm trying to get results of CGI script running on the same server with get_file_contents and it works everywhere except my local machine under Ubuntu.

It works when I ask it to get url from different server (same script running on production), it works deployed on different server, I'm absolutely sure I have allow_url_fopen set. But every time I'm trying to get that page from a local server I get failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request on PHP side and [error] [client 127.0.0.1] request failed: error reading the headers in Apache error log.

So what can I do with it, what headers should I pass so Apache won't turn me down or, alternatively, what configuration options should I tweak for the same results?

A: 

What hostname are you using to refer to your local webserver? Maybe you're calling it "localhost" and it expects a real domain name.

Licky Lindsay
I am calling it 'localhost' but it resolves correctly, I can see it reaches apache in access.log and it works from the browser.
vava
A: 

Using file_get_contents try to manually forcing the HTTP headers, like so.

<?php
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
// gave it a mime-type, also forced text plain.
// of course,  add any other headers you need.
    'header'=>"Accept: text/plain\r\n" .
              "Content-Type: application/x-www-form-urlencoded\r\n"
  )
);

// create the stream.
$context = stream_context_create($opts);

// open the file using the HTTP headers set above
$file = file_get_contents("your_url_here", false, $context);
?>

If that doesn't work, have you thought about using cURL to process your request?

Anthony Forloney
A: 

[error] [client 127.0.0.1] request failed: error reading the headers

This means you are not setting the headers correctly. Keep the carriage return in mind.

PradeepKr
No, I don't think that was an issue. Absolutely the same code was working fine on different Linux server *and* on the same machine under Windows/Apache.
vava