【问题标题】:How do I load arbitrary data from a url PHP?如何从 url PHP 加载任意数据?
【发布时间】:2010-09-25 06:13:15
【问题描述】:

这个问题很简单。我将在 PHP 脚本中使用什么函数将数据从 URL 加载到字符串中?

【问题讨论】:

    标签: php url scripting load


    【解决方案1】:

    使用文件包装器,您可以使用 file_get_contents 访问 http 资源(几乎只是 GET 请求,没有 POST)。对于更复杂的 http 请求,如果您安装了 curl 包装器,则可以使用它们。查看 php.net 了解更多信息。

    【讨论】:

      【解决方案2】:

      我想你正在寻找

      $url_data = file_get_contents("http://example.com/examplefile.txt");
      

      【讨论】:

      • 好的,它有效。但不适用于 wikipedia.org。例如:`file_get_contents("en.wikipedia.org/wiki/apple");` 失败这是一个问题,因为这是我试图从中加载数据的站点"
      【解决方案3】:

      CURL 通常是一个很好的解决方案:http://www.php.net/curl

      
      // create a new cURL resource
      $ch = curl_init();
      
      // set URL and other appropriate options
      curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      
      // grab URL and pass it to the browser
      $html = curl_exec($ch);
      
      // close cURL resource, and free up system resources
      curl_close($ch);
      

      【讨论】:

      • 如果我想用 html id="simple_id" 获取一个元素应该怎么做?
      【解决方案4】:

      查看Snoopy,这是一个模拟网络浏览器的 PHP 类:

      include "Snoopy.class.php";
      $snoopy = new Snoopy;
      $snoopy->fetchtext("http://www.example.com");
      $html = $snoopy->results;
      

      【讨论】:

        猜你喜欢
        • 2015-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-13
        • 1970-01-01
        相关资源
        最近更新 更多