为什么file_get_contents()返回“应用程序错误”?

I have a simple code:

$t = file_get_contents("http://sub.example.com/list?u=test&passw=12345");

If I print that result I get "Application Error" text.

I really don't understand the reason, if I try to open that url on the browser i see it perfectly.

It returns "application/json" data.

It is possible that the web application at sub.example.com expects some additional input apart from what goes into the URL (e.g. a cookie), does not find this input, and errors out.

In any case, it's certain that the HTTP requests generated by (a) your program and (b) your browser are not identical, otherwise the application would exhibit the same behavior in both cases.

To debug this, you can do the following:

  • Rewrite the code to use cURL instead of file_get_contents, so that you can specify an HTTP proxy
  • Install Fiddler, a fantastic HTTP debugging proxy
  • Pass the HTTP request your application generates and one given manually through the browser through Fiddler
  • Find the differences (you can simply do a text compare on the requests), this will lead you to the answer