【问题标题】:PHP Get Contents of a URL or PagePHP 获取 URL 或页面的内容
【发布时间】:2011-08-23 16:55:38
【问题描述】:

我正在尝试创建一个 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,但之后我不确定。有人可以发布一个通用示例来说明我如何做到这一点吗?

【问题讨论】:

标签: php html curl


【解决方案1】:

如果你只需要 GET 并且在你的服务器上启用了allow_url_fopen,你可以简单地使用

$data = file_get_contents('http://api.somesite.com');

【讨论】:

  • 是的,我只需要一个简单的 GET 请求。我知道 allow_url_fopen 在某些网络主机上默认未启用,尤其是在预算主机上。为了实现最大的兼容性,您推荐file_get_contents() 还是 curl 库?
  • Curl 也可能丢失。所以你可能想使用任何可用的东西。
  • 介于两者之间,如果您可以选择,file_get_contents 实际上是更好的方法
  • @Ascherer 实际上,我认为 cURL 更好。它似乎获得了更好的性能。 http://stackoverflow.com/questions/555523/file-get-contents-vs-curl-what-has-better-performance.
  • 为了速度是的,但不是简单
【解决方案2】:

这就是您将如何使用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;
}

【讨论】:

    【解决方案3】:
    $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

    【讨论】:

      【解决方案4】:

      简单方法

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

      【讨论】:

      • 请不要进行此类编辑,@solutioner。最好将此帖子标记为重复。
      • 对于Stack Overflow,仅包含链接的答案不被视为一个好的答案。
      • 根据php.net/manual/en/function.include.php,从包含的文件中回显返回似乎不会像您暗示的那样。
      【解决方案5】:

      简单地说:

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

      【讨论】:

      • 你需要curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );或者返回的数据发送给客户端。
      • 当然可以,还有很多其他的 curl 选项可以设置。我确实说过“简单地说”。
      【解决方案6】:

      如果您的 PHP 安装不支持 curl 且不支持 allow_url_fopen,如果您有 PECL,则可以选择以下选项:

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

      【讨论】:

      • (PECL pecl_http &gt;= 0.1.0) - 甚至比 curl 或 allow_url_fopen 更不可能
      • 我的房东有 PECL,但我同意,这可能更长远。
      • 我同意,我希望获得最大的兼容性。不过谢谢,哈佛!
      猜你喜欢
      • 2011-05-12
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      相关资源
      最近更新 更多