차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판이전 판
다음 판
이전 판
tech:curl [2015/02/14 15:51] V_Ltech:curl [2016/09/29 06:30] (현재) V_L
줄 1: 줄 1:
-======Curl======+{{tag>curl php}} 
 +======cURL (Client URL Library Functions)=====
  
 cURL (Client URL Library Functions) cURL (Client URL Library Functions)
  
 제작자 Daniel Stenberg 의 설명을 그대로 변역하면 제작자 Daniel Stenberg 의 설명을 그대로 변역하면
-curl is a comand line tool for transferring files with URL syntax 
  
-커맨드라인에서 URL 문법을 사용하여 파일을 전송 프로그램+> curl is a comand line tool for transferring files with URL syntax 
 +커맨드라인에서 URL 문법을 사용하여 파일을 전송하는 프로그램
  
 내가 원하는 주소의 페이지에서 내가 임의의 값을 넣고 그 넣은 값으로 페이지에서 리턴되는 값을 받아오는 역할을 한다. 내가 원하는 주소의 페이지에서 내가 임의의 값을 넣고 그 넣은 값으로 페이지에서 리턴되는 값을 받아오는 역할을 한다.
 PHP에서 cURL을 사용하려는 사람들 대부분이 아마도 HTTPS 접속 때문일 것이다. PHP에서 cURL을 사용하려는 사람들 대부분이 아마도 HTTPS 접속 때문일 것이다.
 소켓 또는 그 외 여러가지 접속방법이 있는데 굳이 cURL을 사용하는 건 속도면에서도 빠르고 HTTPS도 쉽게 접속할 수 있기 때문이다. 소켓 또는 그 외 여러가지 접속방법이 있는데 굳이 cURL을 사용하는 건 속도면에서도 빠르고 HTTPS도 쉽게 접속할 수 있기 때문이다.
-cURL 모듈을 서버에 설치해야 합니다.(리눅스 - curl.so, 윈도우 - php_curl.dll 확장모듈 필요) 
  
 +cURL 모듈을 서버에 설치해야 한다.(리눅스 - curl.so, 윈도우 - php_curl.dll 확장모듈 필요)
  
- ===== cURL로 가능한 일 ===== 
  
 +=====설치=====
 +curl을 php 모듈과 연결해줘야 한다.
 +
 +  sudo apt-get install php5-curl
 +혹은
 +  sudo apt-get install php7.0-curl
 +
 + ===== cURL로 가능한 일 =====
  
   * HTTPS certificates   * HTTPS certificates
   * HTTP POST   * HTTP POST
   * HTTP PUT   * HTTP PUT
-  * FTP upload+  * FTP  upload
   * HTTP Form   * HTTP Form
   * cookie   * cookie
   * authentication   * authentication
  
 +[[snoopy]]도 한번 볼 것.
  
 =====활용 예제===== =====활용 예제=====
줄 46: 줄 54:
 <code php><?php <code php><?php
 function fetch_page($url,$param,$cookies,$referer_url){ function fetch_page($url,$param,$cookies,$referer_url){
-if(strlen(trim($referer_url)) ===== 0) $referer_url= $url; +if(strlen(trim($referer_url)) ===== 0) $referer_url= $url;
 $curlsession = curl_init (); $curlsession = curl_init ();
 curl_setopt ($curlsession, CURLOPT_URL, \"$url\"); curl_setopt ($curlsession, CURLOPT_URL, \"$url\");
줄 56: 줄 64:
 curl_setopt ($curlsession, CURLOPT_COOKIE, \"$cookies\"); curl_setopt ($curlsession, CURLOPT_COOKIE, \"$cookies\");
 } }
-curl_setopt ($curlsession, CURLOPT_HEADER, 1); //헤더값을 가져오기위해 사용합니다. 쿠키를 가져오려고요.+curl_setopt ($curlsession, CURLOPT_HEADER, 1); //헤더값을 가져오기위해 사용다. 쿠키를 가져오려고요.
 curl_setopt ($curlsession, CURLOPT_USERAGENT, \"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\"); curl_setopt ($curlsession, CURLOPT_USERAGENT, \"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\");
-curl_setopt ($curlsession, CURLOPT_REFERER, \"$referer_url\"); +curl_setopt ($curlsession, CURLOPT_REFERER, \"$referer_url\");
 ob_start(); ob_start();
 $res = curl_exec ($curlsession); $res = curl_exec ($curlsession);
줄 67: 줄 75:
 }else{ }else{
 $returnVal = $buffer; $returnVal = $buffer;
-}  +
-curl_close($curlsession); +curl_close($curlsession);
 return $returnVal; return $returnVal;
-+}
 ?></code> ?></code>
  
줄 120: 줄 128:
 $res = curl_exec($ch); $res = curl_exec($ch);
 curl_close($ch); curl_close($ch);
-/** 결과는 Atom xml 형식이다. DOM 또는 xml 파싱 function을 이용해서 파싱하면 됩니다. **/+/** 결과는 Atom xml 형식이다. DOM 또는 xml 파싱 function을 이용해서 파싱하면 다. **/
 echo $res; echo $res;
 ?></code> ?></code>
- 
  
 ==== cURL을 이용한 웹페이지 가져오기==== ==== cURL을 이용한 웹페이지 가져오기====
- 
  
 <code php><?php <code php><?php
줄 158: 줄 164:
 $timeout = 60; $timeout = 60;
  
-$rawhtml = curl_init();  +$rawhtml = curl_init(); 
-curl_setopt ($rawhtml, CURLOPT_URL,$url);   +curl_setopt ($rawhtml, CURLOPT_URL,$url); 
-curl_setopt ($rawhtml, CURLOPT_RETURNTRANSFER, 1);       +curl_setopt ($rawhtml, CURLOPT_RETURNTRANSFER, 1); 
-curl_setopt ($rawhtml, CURLOPT_REFERER, '');             +curl_setopt ($rawhtml, CURLOPT_REFERER, ''); 
-curl_setopt ($rawhtml, CURLOPT_COOKIE, $cookie_string);      +curl_setopt ($rawhtml, CURLOPT_COOKIE, $cookie_string); 
-curl_setopt ($rawhtml, CURLOPT_CONNECTTIMEOUT, $timeout);   +curl_setopt ($rawhtml, CURLOPT_CONNECTTIMEOUT, $timeout); 
-curl_setopt ($rawhtml, CURLOPT_USERAGENT, $userAgent);   +curl_setopt ($rawhtml, CURLOPT_USERAGENT, $userAgent); 
-$output = curl_exec($rawhtml);   +$output = curl_exec($rawhtml); 
-curl_close($rawhtml); +curl_close($rawhtml);
 </file> </file>
  
 ===== cURL, Client URL Library Functions ===== ===== cURL, Client URL Library Functions =====
- 
  
   * curl_init : 세션 초기화, 핸들값 리턴   * curl_init : 세션 초기화, 핸들값 리턴
줄 179: 줄 184:
   * curl_getinfo : 상태 정보를 리턴한다.   * curl_getinfo : 상태 정보를 리턴한다.
   * curl_close : curl 세션을 닫는다   * curl_close : curl 세션을 닫는다
- 
- 
  
  
 <file php> <file php>
- function curl($url)  +    function curl($url) 
- { // http://www.partner114.com/bbs/board.php?bo_table=B07&wr_id=126 +    { // http://www.partner114.com/bbs/board.php?bo_table=B07&wr_id=126 
- $ch = curl_init(); +        $ch = curl_init(); 
- curl_setopt($ch, CURLOPT_URL, $url); +        curl_setopt($ch, CURLOPT_URL, $url); 
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); +        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); +        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
- $g = curl_exec($ch); +        $g = curl_exec($ch); 
- curl_close($ch); +        curl_close($ch); 
- return $g; +        return $g; 
- }+    }
 </file> </file>
  
 ====curl_close==== ====curl_close====
  
-기능 : cURL 세션을 닫는다.+  * 기능 : cURL 세션을 닫는다. 
 +  * 구문 : curl_close(resource ch)
  
-구문 : curl_close(resource ch) 
 cURL 세션을 닫고, 리소스를 비워준다. 또한 cURL 핸들은 삭제된다. cURL 세션을 닫고, 리소스를 비워준다. 또한 cURL 핸들은 삭제된다.
-기능 : 마지막 에러 번호를 리턴해 준다. 
  
 ==== curl_errno==== ==== curl_errno====
-구문 : curl_errno(resource ch)+ 
 +  * 기능 : 마지막 에러 번호를 리턴해 준다. 
 +  * 구문 : curl_errno(resource ch)
 만일 연산 후 어떠한 에러도 발행하지 않았다면 0(zero)을 리턴한다. 만일 연산 후 어떠한 에러도 발행하지 않았다면 0(zero)을 리턴한다.
-Error Code+ 
 +===Error Code=== 
 There exists a bunch of different error codes and their corresponding error messages that may appear during bad conditions. At the time of this writing, the exit codes are: There exists a bunch of different error codes and their corresponding error messages that may appear during bad conditions. At the time of this writing, the exit codes are:
  
줄 278: 줄 284:
   * 80 - Failed to shut down the SSL connection   * 80 - Failed to shut down the SSL connection
   * XX - There will appear more error codes here in future releases. The existing ones are meant to never change   * XX - There will appear more error codes here in future releases. The existing ones are meant to never change
- 
  
 ====curl_error==== ====curl_error====
줄 293: 줄 298:
   * 예제 : 새로운 curl세션을 초기화하고 웹페이지를 페칭한다.   * 예제 : 새로운 curl세션을 초기화하고 웹페이지를 페칭한다.
  
-<code php><?phpphp+<code php><?php
 $ch = curl_init(); $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
줄 300: 줄 305:
 curl_close($ch); curl_close($ch);
 ?></code> ?></code>
- 
  
 ==== curl_exec ==== ==== curl_exec ====
줄 317: 줄 321:
 ==== curl_setopt ==== ==== curl_setopt ====
  
-CURLOPT_FOLLOWLOCATION+===CURLOPT_FOLLOWLOCATION===
  
 TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set). TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).
줄 323: 줄 327:
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, tru curl_setopt($ch, CURLOPT_FOLLOWLOCATION, tru
  
-CURLOPT_HEADER+===CURLOPT_HEADER===
  
 TRUE to include the header in the output. TRUE to include the header in the output.
줄 332: 줄 336:
 curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_HEADER, false);
  
-CURLOPT_NOBODY+===CURLOPT_NOBODY===
  
 TRUE to exclude the body from the output. TRUE to exclude the body from the output.
 본문의 정보를 받기 원하지 않는다면 이 옵션을 추가한다. 본문의 정보를 받기 원하지 않는다면 이 옵션을 추가한다.
  
-CURLOPT_POST+===CURLOPT_POST===
  
 TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms. TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.
줄 345: 줄 349:
 curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POST,1);
  
-CURLOPT_RETURNTRANSFER+===CURLOPT_RETURNTRANSFER===
  
 TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
줄 354: 줄 358:
 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  
-CURLOPT_SSL_VERIFYPEER+===CURLOPT_SSL_VERIFYPEER===
  
 FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option. CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2). FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option. CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2).
줄 361: 줄 365:
 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  
-CURLOPT_PORT+===CURLOPT_PORT===
  
 An alternative port number to connect to. An alternative port number to connect to.
줄 370: 줄 374:
 curl_setopt ($ch, CURLOPT_SSLVERSION,3); curl_setopt ($ch, CURLOPT_SSLVERSION,3);
  
-CURLOPT_TIMEOUT+===CURLOPT_TIMEOUT===
  
 The maximum number of The maximum number of
줄 377: 줄 381:
 curl_setopt($ch, CURLOPT_TIMEOUT,100); curl_setopt($ch, CURLOPT_TIMEOUT,100);
  
-CURLOPT_POSTFIELDS+===CURLOPT_POSTFIELDS===
  
 The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path.
 POST 메소드라면 파라미터 값들을 이 옵션에 정의하면 된다. POST 메소드라면 파라미터 값들을 이 옵션에 정의하면 된다.
  
-curl_setopt($cu, CURLOPT_POSTFIELDS,$vars); +curl_setopt($cu, CURLOPT_POSTFIELDS,$vars);
  
  보낼 데이타 형식은 GET 방식으로 설정  보낼 데이타 형식은 GET 방식으로 설정
줄 388: 줄 392:
 ex) $vars = "arg=$arg1&arg2=$arg2&arg3=$arg3"; ex) $vars = "arg=$arg1&arg2=$arg2&arg3=$arg3";
  
-CURLOPT_REFERER+===CURLOPT_REFERER===
  
 The contents of the "Referer: " header to be used in a HTTP request. The contents of the "Referer: " header to be used in a HTTP request.
 리퍼러 정보를 설정 리퍼러 정보를 설정
  
-CURLOPT_URL+===CURLOPT_URL===
  
 The URL to fetch. This can also be set when initializing a session with curl_init(). The URL to fetch. This can also be set when initializing a session with curl_init().
줄 403: 줄 407:
 </file> </file>
  
-CURLOPT_USERAGENT+===CURLOPT_USERAGENT===
  
 The contents of the "User-Agent: " header to be used in a HTTP request. The contents of the "User-Agent: " header to be used in a HTTP request.
줄 409: 줄 413:
 에이전트 정보를 설정 에이전트 정보를 설정
  
-curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");  +curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
  
  
 +^  누구나 수정할 수 있다. [[http://openwiki.kr/syntax|위키 사용법]] 참고하라.  ^
  
-^  누구나 수정하실 수 있습니다. [[http://vaslor.net/syntax|위키 사용법]] 참고하세요.  ^ 
  
-{{tag>curl}}