search
Homephp教程php手册failed to open stream: HTTP request failed

failed to open stream: HTTP request failed

Jun 13, 2016 am 09:55 AM
fopenhttpopenrequeststreamkeepcontentserverofRemotelycollection

fopen他以采集远程服务器的内容保存到本地同时也可以打开本地的文件,是一个非常不错的函数,下面我们来看看关于在使用fopen函数时出现failed to open stream: HTTP request failed问题解决方法。

$handle = fopen ("http://www.zhutiai.com/c5-03/", "rb");
$contents = "";
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
echo $contents; //输出获取到得内容。
?>

// 对 PHP 5 及更高版本可以使用下面的代码

$handle = fopen("http://mb.bKjia.c0m", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
echo $contents;
?>


有人说在php.ini中,有这样两个选项:allow_url_fopen =on(表示可以通过url打开远程文件),user_agent="PHP"(表示通过哪种脚本访问网络,默认前面有个 " ; " 去掉即可。)重启服务器。
但是有些还是会有这个警告信息,想用完美的解决还差一步,还得设置php.ini里面的user_agent,php默认的user_agent是PHP,我们把它改成Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)来模拟浏览器就可以了

使用PHP的CURL模块取回PHP主页,并保存到文件中

$ch = curl_init("http://www.bKjia.c0m/");
$fp = fopen("php_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

curl相关函数列表:

  curl_init — 初始化一个CURL会话

  curl_setopt — 为CURL调用设置一个选项

  curl_exec — 执行一个CURL会话

  curl_close — 关闭一个CURL会话

  curl_version — 返回当前CURL版本

  1>curl_init — 初始化一个CURL会话

  描述

  int curl_init ([string url])

  curl_init()函数将初始化一个新的会话,返回一个CURL句柄供 curl_setopt(), curl_exec(),和 curl_close() 函数使用。如果可选参数被提供,那么CURLOPT_URL选项将被设置成这个参数的值。你可以使用curl_setopt()函数人工设置。

例 1. 初始化一个新的CURL会话,且取回一个网页

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, “http://www.zhutiai.com/”);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);
curl_close ($ch);
?>

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

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools