我正在尝试创建一个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;
我想我应该在这里使用curl,但之后我不确定。有人可以发布一个如何做到这一点的广义例子吗?
答案 0 :(得分:24)
如果您只需要GET并且在服务器上启用了allow_url_fopen
,则只需使用
$data = file_get_contents('http://api.somesite.com');
答案 1 :(得分:6)
这是您使用cURL从远程网址获取内容的方法。您可以定义函数并进行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;
}
答案 2 :(得分:5)
简单方法
<?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
?>
最佳方式(使用cURL):
echo get_remote_data('http://example.com'); //SIMPLE REQUEST;
//OR
echo get_remote_data('http://example.com', "var2=something&var3=blabla" ); //POST REQUEST;
(代码:GitHub)
答案 3 :(得分:4)
$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);
答案 4 :(得分:1)
简单地说:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.somesite.com/');
$dataFromExternalServer = curl_exec($ch);
答案 5 :(得分:0)
如果您的PHP安装不支持curl且不支持allow_url_fopen
,如果您有PECL,这里有一个选项:
$body = http_parse_message(http_get($url))->body;