차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판이전 판다음 판 | 이전 판 | ||
| tech:curl [2015/02/14 15:51] – V_L | tech:curl [2016/09/29 06:30] (현재) – V_L | ||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| - | ======Curl====== | + | {{tag> |
| + | ======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><? | <code php><? | ||
| function fetch_page($url, | function fetch_page($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, | curl_setopt ($curlsession, | ||
| 줄 56: | 줄 64: | ||
| curl_setopt ($curlsession, | curl_setopt ($curlsession, | ||
| } | } | ||
| - | curl_setopt ($curlsession, | + | curl_setopt ($curlsession, |
| curl_setopt ($curlsession, | curl_setopt ($curlsession, | ||
| - | curl_setopt ($curlsession, | + | curl_setopt ($curlsession, |
| 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; | ||
| - | } | + | } |
| ?></ | ?></ | ||
| 줄 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; | ||
| ?></ | ?></ | ||
| - | |||
| ==== cURL을 이용한 웹페이지 가져오기==== | ==== cURL을 이용한 웹페이지 가져오기==== | ||
| - | |||
| <code php><? | <code php><? | ||
| 줄 158: | 줄 164: | ||
| $timeout = 60; | $timeout = 60; | ||
| - | $rawhtml = curl_init(); | + | $rawhtml = curl_init(); |
| - | curl_setopt ($rawhtml, CURLOPT_URL, | + | curl_setopt ($rawhtml, CURLOPT_URL, |
| - | curl_setopt ($rawhtml, CURLOPT_RETURNTRANSFER, | + | curl_setopt ($rawhtml, CURLOPT_RETURNTRANSFER, |
| - | curl_setopt ($rawhtml, CURLOPT_REFERER, | + | curl_setopt ($rawhtml, CURLOPT_REFERER, |
| - | curl_setopt ($rawhtml, CURLOPT_COOKIE, | + | curl_setopt ($rawhtml, CURLOPT_COOKIE, |
| - | curl_setopt ($rawhtml, CURLOPT_CONNECTTIMEOUT, | + | curl_setopt ($rawhtml, CURLOPT_CONNECTTIMEOUT, |
| - | curl_setopt ($rawhtml, CURLOPT_USERAGENT, | + | curl_setopt ($rawhtml, CURLOPT_USERAGENT, |
| - | $output = curl_exec($rawhtml); | + | $output = curl_exec($rawhtml); |
| - | curl_close($rawhtml); | + | curl_close($rawhtml); |
| </ | </ | ||
| ===== 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) | + | |
| - | { // http:// | + | { // http:// |
| - | $ch = curl_init(); | + | $ch = curl_init(); |
| - | curl_setopt($ch, | + | curl_setopt($ch, |
| - | curl_setopt($ch, | + | curl_setopt($ch, |
| - | curl_setopt($ch, | + | curl_setopt($ch, |
| - | $g = curl_exec($ch); | + | $g = curl_exec($ch); |
| - | curl_close($ch); | + | curl_close($ch); |
| - | return $g; | + | return $g; |
| - | } | + | } |
| </ | </ | ||
| ====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><? | + | <code php><? |
| $ch = curl_init(); | $ch = curl_init(); | ||
| curl_setopt($ch, | curl_setopt($ch, | ||
| 줄 300: | 줄 305: | ||
| curl_close($ch); | curl_close($ch); | ||
| ?></ | ?></ | ||
| - | |||
| ==== curl_exec ==== | ==== curl_exec ==== | ||
| 줄 317: | 줄 321: | ||
| ==== curl_setopt ==== | ==== curl_setopt ==== | ||
| - | CURLOPT_FOLLOWLOCATION | + | ===CURLOPT_FOLLOWLOCATION=== |
| TRUE to follow any " | TRUE to follow any " | ||
| 줄 323: | 줄 327: | ||
| curl_setopt($ch, | curl_setopt($ch, | ||
| - | 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, | curl_setopt($ch, | ||
| - | 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/ | TRUE to do a regular HTTP POST. This POST is the normal application/ | ||
| 줄 345: | 줄 349: | ||
| curl_setopt($ch, | curl_setopt($ch, | ||
| - | 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, | curl_setopt($ch, | ||
| - | 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, | curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, | ||
| - | 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, | curl_setopt ($ch, CURLOPT_SSLVERSION, | ||
| - | CURLOPT_TIMEOUT | + | ===CURLOPT_TIMEOUT=== |
| The maximum number of | The maximum number of | ||
| 줄 377: | 줄 381: | ||
| curl_setopt($ch, | curl_setopt($ch, | ||
| - | CURLOPT_POSTFIELDS | + | ===CURLOPT_POSTFIELDS=== |
| The full data to post in a HTTP " | The full data to post in a HTTP " | ||
| POST 메소드라면 파라미터 값들을 이 옵션에 정의하면 된다. | POST 메소드라면 파라미터 값들을 이 옵션에 정의하면 된다. | ||
| - | curl_setopt($cu, | + | curl_setopt($cu, |
| | | ||
| 줄 388: | 줄 392: | ||
| ex) $vars = " | ex) $vars = " | ||
| - | CURLOPT_REFERER | + | ===CURLOPT_REFERER=== |
| The contents of the " | The contents of the " | ||
| 리퍼러 정보를 설정 | 리퍼러 정보를 설정 | ||
| - | 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: | ||
| </ | </ | ||
| - | CURLOPT_USERAGENT | + | ===CURLOPT_USERAGENT=== |
| The contents of the " | The contents of the " | ||
| 줄 409: | 줄 413: | ||
| 에이전트 정보를 설정 | 에이전트 정보를 설정 | ||
| - | curl_setopt($ch, | + | curl_setopt($ch, |
| + | ^ 누구나 수정할 수 있다. [[http:// | ||
| - | ^ 누구나 수정하실 수 있습니다. [[http:// | ||
| - | {{tag> | ||