如何从完全不同的服务器读取文件的内容,然后显示内容。我将稍后更改代码以适当方式使用返回的信息。使用php脚本从其他网站读取文件
2
A
回答
4
您可以使用file_get_contents
或cURL
。
下面的例子下载google.com的主页的HTML并在屏幕上显示它。
的file_get_contents方式:
$data = file_get_contents("http://www.google.com/");
echo "<pre>" . $data . "</pre>";
卷曲方式:
function get_web_page($url)
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch);
curl_close($ch);
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
//Now get the webpage
$data = get_web_page("https://www.google.com/");
//Display the data (optional)
echo "<pre>" . $data['content'] . "</pre>";
0
您可以使用卷曲
$ch = curl_init("http://www.google.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content_of_page = curl_exec($ch); curl_close($ch);
1
有几种方法我建议:
HTTP:
如果可能的话,使用PHP内置的文件流功能(如file_get_contents()
)或cURL通过正常HTTP requests到从服务器下载文件。但是,如果你想下载一个PHP文件的源代码,这将不起作用(你会得到它的输出)。举个例子:
<?php
// Most basic HTTP request
$file = file_get_contents('http://www.example.com/path/to/file');
// HTTP request with a username and password
$file = file_get_contents('http://user:[email protected]/path/to/file');
// HTTPS request
$file = file_get_contents('https://www.example.com/path/to/file');
SSH:
如果您已经安装了SSH2扩展名,您必须将服务器的SSH访问,你可能想通过SFTP(SSH文件传输协议)来下载文件:
<?php
// Use the SFTP stream wrapper to download files through SFTP:
$file = file_get_contents('ssh2.sftp://user:[email protected]/path/to/file');
FTP:
如果服务器有你要访问的FTP服务器,你可能想使用FTP或FTPS(安全FTP,如果支持的话)来下载文件:
<?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:[email protected]/path/to/file');
// FTPS with username and password:
$file = file_get_contents('ftps://user:[email protected]/path/to/file');
相关问题
- 1. 从其他文件读取PHP?
- 2. 从其他网站获取文本以便在html中使用
- 3. 从php其他网站提取数据
- 4. 从其他站点读取信息的脚本
- 5. 是否有可能从其他网站读取PHP代码?
- 6. 使用PHP和/或Javascript从网站读取JSON文件。
- 7. 使用Java从网站读取php文件?
- 8. shell脚本来读取文件和grep其他文件
- 9. 从angularjs编写的其他网站阅读文本
- 10. 使用php脚本从远程服务器读取大文件
- 11. PHP使用其他网站登录?
- 12. 在其他网站上使用php
- 13. 是否有可能从其他网站拉文本并将其放置在使用javascript的其他网站中?
- 14. 想从其他网站一个specic文本在PHP
- 15. 从其他网站
- 16. 从其他网站
- 17. 从其他网站
- 18. 我将如何使用JavaScript从其他网站检索文本?
- 19. 如何使用php脚本读取30MB文本文件?
- 20. 从Octave中的其他脚本文件运行脚本文件
- 21. 从其他网页使用PHP登录网站
- 22. 使用本地运行的HTML文件中的其他网站
- 23. 从网站阅读文本
- 24. PHP脚本下载一个网站及其文件
- 25. 无法使用jquery读取其他网站内容
- 26. 自动登录到其他网站php脚本
- 27. 如何使脚本从文件和/或其他脚本中获取STDIN
- 28. 从远程网站读取JSON文件
- 29. 从其他网站使用favicon?
- 30. 使用SharePoint 2010从其他网站
这个文件以某种方式保护或者你可以做一个HTTP请求呢? – Jage 2010-09-01 17:44:33