PHP curl 정리

-- PHP 2013. 2. 17. 15:58
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1. curl 사용법과 옵션 (get)


A simple example on how to use the cURL module to download a website or any other file.

You can find more cURL options on the related PHP manual page.


function curl_download($Url) 

{

    // is cURL installed yet?

    if (!function_exists('curl_init')){

        die('Sorry cURL is not installed!');

    }

 

   // OK cool - then let's create a new cURL resource handle

    $ch = curl_init();

 

    // Now set some options (most are optional)

 

    // Set URL to download

    curl_setopt($ch, CURLOPT_URL, $Url);

 

    // Set a referer

    curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");

 

    // User agent

    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");

 

    // Include header in result? (0 = no, 1 = yes)

    curl_setopt($ch, CURLOPT_HEADER, 0);

 

    // Should cURL return or print out the data? (true = return, false = print)

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 

    // Timeout in seconds

    curl_setopt($ch, CURLOPT_TIMEOUT, 10);


    // Connection Timeout in seconds

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

 

    // Download the given URL, and return output

    $output = curl_exec($ch);

 

    // Close the cURL resource, and free system resources

    curl_close($ch);

 

    return $output;

}


출처 : http://www.jonasjohn.de/snippets/php/curl-example.htm
관련사이트 : http://www.yilmazhuseyin.com/blog/dev/curl-tutorial-examples-usage/



2. curl 를 이용하여 파일 업로드 (post)


$upload = $_FILES['upload'];

$arr_tmp_name = explode( '\\', $upload['tmp_name'] );


$tmp_name = $arr_tmp_name[ count( $arr_tmp_name ) - 1 ];

$orig_name = $upload['name'];

$full_path = str_replace( $tmp_name, $orig_name, $upload['tmp_name'] );


@rename( $upload['tmp_name'], $full_path );

$post = array(

'param1' => 'param1',

'upload' => '@' . $full_path . ';type=' . $upload['type']

);


// $url : 업로드를 받아야할 URL

curl_upload( $url, $post );


// 필자의 경우 HTTP Header에도 응답값이 있어 CURLOPT_HEADER = 1로 지정함

function curl_upload($url, $post) {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

    curl_setopt($ch, CURLOPT_HEADER, 1);

    $buffer = curl_exec($ch);

    curl_close($ch);

    

    return $buffer;

}

posted by 어린왕자악꿍