开发者

CLI cURL to PHP cURL

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 c开发者_开发问答url, 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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜