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()这个扩展添加了才能使用

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

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.

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

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

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

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

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.

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

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
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
