file_get_contents和curl函数用法

  •   2009-07-31/22:46
  • file_get_contents ()应用很简单,但是有的服务器php.ini设置如果关闭allow_url_fopen,这个函数就失效了,一般个人服务器可以设置,但是如果是虚拟主机就不在自己掌控范围内了。但是curl 是另外一个打开远程页面的内容的函数用法如下:

    <?php
    // create a new curl resource
    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "");
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // grab URL and pass it to the browser
    curl_exec($ch);

    // close curl resource, and free up system resources
    curl_close($ch);
    ?>

    当然此功能也有被关闭的可能。

    使用以上2个方法可以使用function_exists()判断使用

    if(function_exists('file_get_contents')) {
    $file_contents = file_get_contents($url);
    } else {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);
    }
    return $file_contents;
    if(function_exists('file_get_contents')) {
    $file_contents = file_get_contents($url);
    } else {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);
    }
    return $file_contents;


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多