开发者

get_file_contents fails with 400 response code

I have the stra开发者_开发问答ngest 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?


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜