PHP获取URL或页面的内容

Oli*_*ryn 10 html php curl

我正在尝试创建一个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;
Run Code Online (Sandbox Code Playgroud)

我想我应该在这里使用curl,但之后我不确定.有人可以发布一个如何做到这一点的广义例子吗?

Thi*_*ter 24

如果您只需要GET并allow_url_fopen在服务器上启用,则只需使用即可

$data = file_get_contents('http://api.somesite.com');
Run Code Online (Sandbox Code Playgroud)


tyr*_*ter 8

这是您使用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;
}
Run Code Online (Sandbox Code Playgroud)


Eli*_*Eli 5

$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); 
Run Code Online (Sandbox Code Playgroud)

另见:http : //php.net/manual/en/function.curl-exec.php


T.T*_*dua 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
?> 
Run Code Online (Sandbox Code Playgroud)

最好的方式(使用cURL):

echo get_remote_data('http://example.com');   //SIMPLE REQUEST;
//OR
echo get_remote_data('http://example.com', "var2=something&var3=blabla" ); //POST REQUEST;
Run Code Online (Sandbox Code Playgroud)

(代码:在GitHub)