1

I am working on a fast caching system to work with a PHP framework. Basicly, all static pages don't need to be loaded with framework, so I wanted to use CURL. For the command line it works very fast:

$ curl http://www.example.com/en/terms-of-use.html > web/cache/en/terms-of-use.html

My current solution is getting the file data with curl, open/create a file and put all the data in that. I'm not very familiar with curl, but there should be a faster way I think if the CLI version is very short.

  • are you building a reverse proxy ? – Arnaud Le Blanc Sep 09 '11 at 08:04
  • It doesn't make sense that curl is faster than visiting the webpage in any other browser .. I assume you are talking about loading the html statically to a file and just displaying that file. Using curl on the fly should not be any faster. – Explosion Pills Sep 09 '11 at 08:19
  • Maybe I formulated it wrong. And it is kind of like reverse proxy that @arnaud576875 said. I save the result once to a static HTML page and give that to the user. No framework to load for the user should really make a difference. – Rolf Babijn Sep 09 '11 at 08:28
  • possible duplicate of [Convert command line cURL to PHP cURL](http://stackoverflow.com/questions/1939609/convert-command-line-curl-to-php-curl) – Gajus Aug 10 '14 at 14:17

2 Answers2

1

You can do this in one of 2 ways:

Use PHP's system / process function calls....

    $page = system("curl http://www.example.com/en/terms-of-use.html");
    print "<pre>";
    print_r($page);
    print "</pre>";

or you can use the native curl PHP libraries

    <?php
      $url = "http://www.example.com/en/terms-of-use.html";
      print $url;
      $ch = curl_init($url);
      if(!$ch)
      {
        $errstr = "Could not connect to server.";
      }
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
      $page = curl_exec($ch);
      print "<pre>";
      print_r($page);
      print "</pre>";
    ?>
shawty
  • 5,729
  • 2
  • 37
  • 71
0

I'm not sure if it's faster, I still need to do some benchmarks, but it is a lot shorter to write.

<?php
    if(!copy('http://www.website.com/en/homepage.html', 'web/cache/en/homepage.html'))
    {
        // Notify someone
    }
?>