Table of Contents
PHP中file_get_contents高級用法实例,filegetcontents
怎运用php 的 file_get_contents
php里的file_get_contents 第3个参数的问题
Home php教程 php手册 PHP中file_get_contents高級用法实例,filegetcontents

PHP中file_get_contents高級用法实例,filegetcontents

Jun 13, 2016 am 09:24 AM
file get php

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);

Copy after login

二、多次尝试

PHP程序代码如下:

$cnt=0;
while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE){
   $cnt++;
}

Copy after login

以上方法对付超时已经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);

Copy after login

注意文档头的Set_time_out否则整个文档都得超时了。

希望本文所述对大家php程序设计的学习有所帮助。

怎运用php 的 file_get_contents

file_get_contents() 读取一个文件中的内容,包括远程文件!!
file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法!!
echo file_get_contents('www.baidu.com');
?>
 

php里的file_get_contents 第3个参数的问题

这个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);
?>

希望不会悲剧,说了半天被看不懂
 

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles