검색결과 리스트
curl에 해당되는 글 1건
- 2013.02.17 PHP curl 정리
글
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;
}
'-- PHP' 카테고리의 다른 글
배열의 데이터를 오름차순, 내림차순으로 정렬 (0) | 2013.04.11 |
---|---|
file_get_contents to get JSON data (0) | 2013.04.09 |
Apache에 PHP설정 (0) | 2012.09.10 |
require, require_once, include, include_once (0) | 2012.07.18 |
PHP (hex2bin, bin2hex) convert ASP (0) | 2009.07.30 |
RECENT COMMENT