Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php脚本从其他网站读取文件_Php - Fatal编程技术网

使用php脚本从其他网站读取文件

使用php脚本从其他网站读取文件,php,Php,如何从完全不同的服务器读取文件内容,然后显示内容。稍后我将更改代码,以正确的方式使用返回的信息。您可以使用或 下面的示例下载google.com主页的HTML并将其显示在屏幕上 文件\u获取内容的方式: $data = file_get_contents("http://www.google.com/"); echo "<pre>" . $data . "</pre>"; $ch = curl_init("http://www.google.com"); curl_se

如何从完全不同的服务器读取文件内容,然后显示内容。稍后我将更改代码,以正确的方式使用返回的信息。

您可以使用或

下面的示例下载google.com主页的HTML并将其显示在屏幕上

文件\u获取内容的方式:

$data = file_get_contents("http://www.google.com/");
echo "<pre>" . $data . "</pre>";
$ch = curl_init("http://www.google.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content_of_page = curl_exec($ch); curl_close($ch);
$data=文件获取内容(“http://www.google.com/");
“回声”$数据。"";
卷曲方式:

$data = file_get_contents("http://www.google.com/");
echo "<pre>" . $data . "</pre>";
$ch = curl_init("http://www.google.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content_of_page = curl_exec($ch); curl_close($ch);
函数获取网页($url)
{
$options=array(
CURLOPT_RETURNTRANSFER=>true,//返回网页
CURLOPT_HEADER=>false,//不返回头
CURLOPT_FOLLOWLOCATION=>true,//跟随重定向
CURLOPT_ENCODING=>“”,//处理所有编码
CURLOPT_AUTOREFERER=>true,//在重定向时设置referer
CURLOPT_CONNECTTIMEOUT=>120,//连接超时
CURLOPT_TIMEOUT=>120,//响应超时
CURLOPT_MAXREDIRS=>10,//在10次重定向后停止
);
$ch=curl\u init($url);
curl_setopt_数组($ch$options);
$content=curl\u exec($ch);
$err=curl\u errno($ch);
$errmsg=curl\u error($ch);
$header=curl\u getinfo($ch);
卷曲关闭($ch);
$header['errno']=$err;
$header['errmsg']=$errmsg;
$header['content']=$content;
返回$header;
}
//现在获取网页
$data=获取网页(“https://www.google.com/" );
//显示数据(可选)
“回声”$数据['content']。"";
您可以使用curl

<?php
// Use the SFTP stream wrapper to download files through SFTP:
$file = file_get_contents('ssh2.sftp://user:password@ssh.example.com/path/to/file');
$ch=curl_init(“http://www.google.com"); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $content\u of\u page=curl\u exec($ch); 卷曲关闭($ch);
我建议有几种方法:

HTTP:
如果可能的话,可以使用PHP的内置文件流函数(如)或通过普通方式从服务器下载文件。但是,如果您想下载PHP文件的源代码,这将不起作用(您将获得它的输出)。例如:

<?php
// Use the FTP stream wrapper to download files through FTP or SFTP

// Anonymous FTP:
$file = file_get_contents('ftp://ftp.example.com/path/to/file');

// FTP with username and password:
$file = file_get_contents('ftp://user:password@ftp.example.com/path/to/file');

// FTPS with username and password:
$file = file_get_contents('ftps://user:password@ftp.example.com/path/to/file');

此文件是否受到某种方式的保护,或者您是否可以向其发出http请求?