簡體   English   中英

PHP獲取URL或頁面的內容

[英]PHP Get Contents of a URL or Page

我正在嘗試創建一個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,但之后我不確定。 有人可以發布一個如何做到這一點的廣義例子嗎?

如果您只需要GET並在服務器上啟用allow_url_fopen ,則只需使用即可

$data = file_get_contents('http://api.somesite.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;
}

簡單的方法

<?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

$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); 

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

簡單地說:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.somesite.com/');
$dataFromExternalServer = curl_exec($ch);

如果你的PHP安裝不支持curl而且不支持allow_url_fopen ,那么如果你有PECL,這里有一個選項:

$body = http_parse_message(http_get($url))->body;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM