PROGRAMING/PHP

[PHP] cURL을 통한 웹페이지 읽어들이기

donghunl 2011. 5. 25. 05:41
반응형

cURL(Client URL Library Frunctions) : comand line에서 URL문법을 이용하여 파일을 전송하는 프로그램
대부분 PHP에서 HTTS접속을 위해 사용


<?php
echo get_content("http://someurl.com");

function get_content($url) {
    $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)';
    $curlsession = curl_init ();
    curl_setopt ($curlsession, CURLOPT_URL,             $url);
    curl_setopt ($curlsession, CURLOPT_HEADER,          0);
    curl_setopt ($curlsession, CURLOPT_RETURNTRANSFER,  1);
    curl_setopt ($curlsession, CURLOPT_POST,            0);
    curl_setopt ($curlsession, CURLOPT_USERAGENT,       $agent);
    curl_setopt ($curlsession, CURLOPT_REFERER,         "");
    curl_setopt ($curlsession, CURLOPT_TIMEOUT,         3);
    $buffer = curl_exec ($curlsession);
    $cinfo = curl_getinfo($curlsession);
    curl_close($curlsession);
    if ($cinfo['http_code'] != 200)
    {
        return "Error : " . $cinfo['http_code'];
    }
    return $buffer;
}
?>

반응형

'PROGRAMING > PHP' 카테고리의 다른 글

[PHP] str_replace  (0) 2011.05.05
[PHP] Server and execution environment information  (0) 2011.04.20
[PHP] Http -> Https redirect  (0) 2011.04.20