PHP中file_get_contents高級用法实例,filegetcontents
PHP中file_get_contents高級用法实例,filegetcontents
本文实例讲述了PHP中file_get_contents高级用法,分享给大家供大家参考。具体分析如下:
首先解决file_get_contents的超时问题,在超时返回错误后就象js中的settimeout那样进行一次尝试,错误超过3次或者5次后就确认为无法连线伺服器而彻底放弃。
这裡就简单介绍两种解决方法:
一、增加超时的时间限制
注意:set_time_limit只是设定你的PHP程式的超时时间,而不是file_get_contents函数读取URL的超时时间。
我一开始以为set_time_limit也能影响到file_get_contents,后来经测试是无效的。真正的修改file_get_contents延时可以用resource $context的timeout参数:
PHP程序代码如下:
$opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>60, ) ); $context = stream_context_create($opts); $html =file_get_contents('http://www.bkjia.com', false, $context); fpassthru($fp);
二、多次尝试
PHP程序代码如下:
$cnt=0; while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE){ $cnt++; }
以上方法对付超时已经OK了。接下来演示一下用file_get_contents实现Post,如下:
PHP程序代码
function Post($url, $post = null){ $context = array(); if (is_array($post)) { ksort($post); $context['http'] = array ( 'timeout'=>60, 'method' => 'POST', 'content' => http_build_query($post, '', '&'), ); } return file_get_contents($url, false, stream_context_create($context)); } $data = array ( 'name' => 'test', 'email' => 'test@gmail.com', 'submit' => 'submit', ); echo Post('http://www.bkjia.com', $data);
注意文档头的Set_time_out否则整个文档都得超时了。
希望本文所述对大家php程序设计的学习有所帮助。
file_get_contents() 读取一个文件中的内容,包括远程文件!!
file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法!!
echo file_get_contents('www.baidu.com');
?>
这个context是指“流中的内容”。
我们知道OS操作都由输入输出流控制。
在开启一些特殊文件(unix下任何东西都是文件),比如设备,键盘、屏幕、网络文件、串口
需要输入流进行一些操作。这时候就用stream_context_create制作一份“输入内容”。
实际主要用途,看到大多数实例也是用来进行HTTP操作(实际用途远不局限于这一种功能)
把HTTP的报头Header的内容作为输入流的内容,这种方式开启文件后获得服务返回的内容。
原理上还是对系统文件的输入输出流操作。
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
//提交语言编码和cookie,获得一个网址的响应内容
$file = file_get_contents('www.example.com/', false, $context);
?>
希望不会悲剧,说了半天被看不懂

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
