search
HomeBackend DevelopmentPHP Tutorialnginx+php5.3.20下,file_get_contents无法使用,求助

file_get_contents外网的url完全没问题,但是如果要获取本机的url(比如http://localhost/index.php)就会报下面的错:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 124.2xx.xxx.xxx, server: www.xxx.com, request: "GET /index.php HTTP/1.0", upstream: "fastcgi://127.0.0.1:9000", host: "www.xxx.com"
在网上找了这么一篇文章, http://www.codesky.net/article/201106/174247.html   但不知在Windows下面如何操作,或者有其他办法么,求助了


回复讨论(解决方案)

试着给那函数加了时间,也无效,比如:

    <?php    $ctx = stream_context_create(array(       'http' => array(           'timeout' => 1 //设置一个超时时间,单位为秒           )       )    );    file_get_contents("http://example.com/", 0, $ctx);    ?>

也试着换成curl,依旧无效,如:

		$ch = curl_init();		$timeout = 5;		curl_setopt ($ch, CURLOPT_URL, 'http://localhost/index.php');		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);		$con = curl_exec($ch);		curl_close($ch);

注:url是外网的完全没问题,只要是系统服务器和url所在服务器是同一个,就出不来,崩溃中

修改hosts 弄个域名解析到本地
使用域名方式访问就可以解决了吧
或者直接ip地址试看看

修改hosts 弄个域名解析到本地
使用域名方式访问就可以解决了吧
或者直接ip地址试看看
是的,我也正是这样啊

检查一下php.ini里,是不是 allow_url_fopen = 1?
另外尝试用include替换file_get_contents,获取localhost的页面。

检查一下php.ini里,是不是 allow_url_fopen = 1?
另外尝试用include替换file_get_contents,获取localhost的页面。
allow_url_fopen确定是开启的
试过了用include,居然很卡很卡,然后在超时的那一刻达到了我想要的结果,但终究不是我想要的:按理说我要获取的内容应该在一秒钟以内就可以搞定。
虽然目前已找到其他办法解决,但仍然表示疑惑,想不通为什么,难道是nginx+php5.3在Windows下的bug?

bug?没这个说法吧。检查你的代码。
首先 http://localhost/index.php 里用简单的代码,比如 echo "hello world"; 是不是简单的代码也是这种情况吗?

其次,用chrome或者firebug,看一下request header的情况,以及浏览器的cookies。或者不用浏览器测试,用windows自带的命令提示符测试。 运行 -> cmd 回车 -> php c:\www\index.php 回车。看看dos界面下是否返回结果?

也遇到同样问题
和其他人对比 感觉 是
这个的问题,
mark 坐等高手

也遇到同样问题
和其他人对比 感觉 是
这个的问题,
mark 坐等高手
兄台也是想获取本地某个url的内容?我其实是用该功能用来生成静态的,不知道兄台用来做什么,呵呵,有点好奇。
但是我用其他办法解决了,就是用ajax请求本地的url,效果也是一样的。

遇到相同的问题。 请教楼主现在有解吗

相同问题,请教

function __curl_get($url){
/*print_r(phpinfo());exit;*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);

curl_close($ch);
return $ret;
}

抓取网页用这个函数就好了。要把curl_init()这个扩展添加了才能使用

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft