脚本宝典收集整理的这篇文章主要介绍了file_get_contents的超时在PHP中不起作用,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。@H_304_0@
public function post ($content,$timeout=null) { $timeinIT = new DateTime(); $this->method = 'POST'; $header = array(); $header['header'] = null; $header['content'] = is_array($content) ? http_build_query($content) : $content; $header['method'] = $this->method; if ($timeout != NULL) { $header['header'] .= "timeout: $timeout" } $header['header'] .= "Content-length: ".strlen($header['content']); $headerContext = stream_context_create(array('http' => $header)); $contents = file_get_contents($this->url,false,$headerContext); $this->responseHeader = $http_response_header; $timeFinal = new DateTime(); $this->time = $timeInit->diff($timeFinal); return $contents; }
基本上,我创建了一个$header并使用file_get_contents将一些$content发布到URL中.
显然,除了$timeout之外,一切正常.它不被考虑.例如,即使我将其设置为1.
SO中的其他类似问题建议使用Curl(我正在使用它,但由于其他原因我正在更改file_get_contents)或fsockopen,但这不是我需要的.
存在一些使用file_get_contents设置超时的方法?
public function myPost($content,$timeout = null) { $timeInit = new DateTime(); $this->method = 'POST'; $header = array(); $header['header'] = null; $header['content'] = is_array($content) ? http_build_query($content) : $content; $header['method'] = $this->method; if ($timeout) { $header['header']['timeout'] = $timeout; } $header['header']['Content-length'] . strlen($header['content']); $headerContext = stream_context_create(array('http' => $header)); $contents = file_get_contents($this->url,$headerContext); $this->responseHeader = $http_response_header; $timeFinal = new DateTime(); $this->time = $timeInit->diff($timeFinal); return $contents; }
但更好的方法是使用它,如例子所示,例如,
$timeInit = new DateTime(); // all your defaults go here $opts = array( 'http'=>array( 'method'=>"POST",) ); //this way,inside conditions if you want $opts['http']['header'] = "Accept-language: en\r\n" . "Cookie: foo=bar\r\n"; $context = stream_context_create($opts);
以上是脚本宝典为你收集整理的file_get_contents的超时在PHP中不起作用全部内容,希望文章能够帮你解决file_get_contents的超时在PHP中不起作用所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。