1

“file_get_contents”和“cURL”有问题。当我做这个时:

$myFile = 'http://example.com/asset_compress/assets/get/bbb.js?file%5B0%5D=myfile.js';
$a=file_get_contents( $myFile );

我收到此错误:

Warning (2): file_get_contents
(http://example.com/asset_compress/assets/get/bbb.js?file%5B0%5D=myfile.js)
 [function.file-get-contents]: failed to open stream: 
    HTTP request failed! HTTP/1.0 404 Not Found
   [APP/Controller/MyController.php, line 1373]

然后我尝试了这样的 CURL:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myFile);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
curl_setopt($curl_handle, CURLOPT_AUTOREFERER, true);
$a = curl_exec($curl);
curl_close($curl);

我得到这个错误:

404 Not Found: The resource requested could not be found on this server!

但是当我写到http://example.com/asset_compress/assets/get/bbb.js?file%5B0%5D=myfile.js浏览器的地址栏时,我得到了完美的文件。浏览器的标题是这样的:

Request URL:http://example.com/asset_compress/assets/get/bbb.js?file%5B0%5D=myfile.js
Request Method:GET
Status Code:200 OK

Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:tr,en-US;q=0.8,en;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Host:example.com
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.28 Safari/537.31

Query String Parameters
file[0]:myfile.js

Response Headers
Connection:close
Content-Length:15911
Content-Type:application/javascript; charset=UTF-8
Date:Fri, 29 Mar 2013 20:33:43 GMT
Server:Apache
X-Powered-By:PleskLin

我怀疑 file_get_contents ,当我这样做时,我得到了完美的输出:

$d1 = file_get_contents("http://www.yahoo.com");
print_r($d1);

当我尝试 cURL 时,出现 404 错误。尽管我从浏览器请求中得到了 200,但我如何才能更多地诊断我得到 404 的原因。

4

1 回答 1

0

This being hosted on a local computer? Or else where? I was having an similar issue that my host determined to be an ISP down the line blocking my requests.

于 2013-03-29T21:59:13.790 回答