PHP 获取 URL 或页面的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5971398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Get Contents of a URL or Page
提问by Oliver Spryn
I am trying to create a PHP script which can request data, such as HTML content, from an external server, then do something with the received content. Here is a generalized example of what I am trying to accomplish:
我正在尝试创建一个 PHP 脚本,该脚本可以从外部服务器请求数据(例如 HTML 内容),然后对接收到的内容执行某些操作。这是我试图完成的一个通用示例:
//Get the HTML generated by http://api.somesite.com/
//Now tack on the Unix timestamp of when the data was received
$myFetchedData = $dataFromExternalServer . "\n Data received at: ". time();
echo $myFetchedData;
I'm thinking I should use curl in here somewhere, but I am not sure after that. Could someone post a generalized example of how I could do this?
我想我应该在这里的某个地方使用 curl,但在那之后我不确定。有人可以发布一个关于我如何做到这一点的通用示例吗?
回答by ThiefMaster
If you only need GET and allow_url_fopen
is enabled on your server, you can simply use
如果您只需要 GET 并allow_url_fopen
在您的服务器上启用,您可以简单地使用
$data = file_get_contents('http://api.somesite.com');
回答by tyronegcarter
This is how you would use cURLto get contents from a remote url. You would define the function and make calls like url_get_contents("http://example.com/");
这就是使用cURL从远程 url 获取内容的方式。您将定义函数并进行调用url_get_contents("http://example.com/");
function url_get_contents($url, $useragent='cURL', $headers=false, $follow_redirects=true, $debug=false) {
// initialise the CURL library
$ch = curl_init();
// specify the URL to be retrieved
curl_setopt($ch, CURLOPT_URL,$url);
// we want to get the contents of the URL and store it in a variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
// specify the useragent: this is a required courtesy to site owners
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
// ignore SSL errors
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// return headers as requested
if ($headers==true){
curl_setopt($ch, CURLOPT_HEADER,1);
}
// only return headers
if ($headers=='headers only') {
curl_setopt($ch, CURLOPT_NOBODY ,1);
}
// follow redirects - note this is disabled by default in most PHP installs from 4.4.4 up
if ($follow_redirects==true) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
}
// if debugging, return an array with CURL's debug info and the URL contents
if ($debug==true) {
$result['contents']=curl_exec($ch);
$result['info']=curl_getinfo($ch);
}
// otherwise just return the contents as a variable
else $result=curl_exec($ch);
// free resources
curl_close($ch);
// send back the data
return $result;
}
回答by T.Todua
simple methods
简单的方法
<?php
echo readfile("http://example.com/"); //needs "Allow_url_include" enabled
//OR
echo include("http://example.com/"); //needs "Allow_url_include" enabled
//OR
echo file_get_contents("http://example.com/");
//OR
echo stream_get_contents(fopen('http://example.com/', "rb")); //you may use "r" instead of "rb" //needs "Allow_url_fopen" enabled
?>
The best Way (using cURL):
最好的方法(使用 cURL):
echo get_remote_data('http://example.com'); //SIMPLE REQUEST;
//OR
echo get_remote_data('http://example.com', "var2=something&var3=blabla" ); //POST REQUEST;
(CODE: at GitHub)
(代码:在GitHub 上)
回答by Eli
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.url.com/cakephp/controller/action/param:1" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
$dataFromExternalServer=curl_exec($ch);
回答by Datajam
Put simply:
简单地说:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.somesite.com/');
$dataFromExternalServer = curl_exec($ch);
回答by H?vard S
If your PHP installation doesn't support curl and does not allow_url_fopen
, here's an option if you have PECL:
如果您的 PHP 安装不支持 curl 并且不支持allow_url_fopen
,那么如果您有 PECL,这里有一个选项:
$body = http_parse_message(http_get($url))->body;