차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
다음 판 | 이전 판 | ||
tech:curl [2013/05/14 07:31] – 새로 만듦 V_L | tech:curl [2016/09/29 06:30] (현재) – V_L | ||
---|---|---|---|
줄 1: | 줄 1: | ||
+ | {{tag> | ||
+ | ======cURL (Client URL Library Functions)===== | ||
+ | |||
+ | cURL (Client URL Library Functions) | ||
+ | |||
+ | 제작자 Daniel Stenberg 의 설명을 그대로 변역하면 | ||
+ | |||
+ | > curl is a comand line tool for transferring files with URL syntax | ||
+ | 커맨드라인에서 URL 문법을 사용하여 파일을 전송하는 프로그램 | ||
+ | |||
+ | 내가 원하는 주소의 페이지에서 내가 임의의 값을 넣고 그 넣은 값으로 페이지에서 리턴되는 값을 받아오는 역할을 한다. | ||
+ | PHP에서 cURL을 사용하려는 사람들 대부분이 아마도 HTTPS 접속 때문일 것이다. | ||
+ | 소켓 또는 그 외 여러가지 접속방법이 있는데 굳이 cURL을 사용하는 건 속도면에서도 빠르고 HTTPS도 쉽게 접속할 수 있기 때문이다. | ||
+ | |||
+ | cURL 모듈을 서버에 설치해야 한다.(리눅스 - curl.so, 윈도우 - php_curl.dll 확장모듈 필요) | ||
+ | |||
+ | |||
+ | =====설치===== | ||
+ | curl을 php 모듈과 연결해줘야 한다. | ||
+ | |||
+ | sudo apt-get install php5-curl | ||
+ | 혹은 | ||
+ | sudo apt-get install php7.0-curl | ||
+ | |||
+ | ===== cURL로 가능한 일 ===== | ||
+ | |||
+ | * HTTPS certificates | ||
+ | * HTTP POST | ||
+ | * HTTP PUT | ||
+ | * FTP upload | ||
+ | * HTTP Form | ||
+ | * cookie | ||
+ | * authentication | ||
+ | |||
+ | [[snoopy]]도 한번 볼 것. | ||
+ | |||
+ | =====활용 예제===== | ||
+ | ====POST방식으로 데이터 전송(simple)==== | ||
+ | |||
+ | <code php><? | ||
+ | $post_data = array( | ||
+ | " | ||
+ | " | ||
+ | ); | ||
+ | $ch = curl_init(); | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | curl_exec($ch); | ||
+ | ?></ | ||
+ | |||
+ | ==== POST방식으로 데이터 전송(function)==== | ||
+ | |||
+ | <code php><? | ||
+ | function fetch_page($url, | ||
+ | if(strlen(trim($referer_url)) ===== 0) $referer_url= $url; | ||
+ | $curlsession = curl_init (); | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | if($cookies && $cookies!=\" | ||
+ | curl_setopt ($curlsession, | ||
+ | } | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | ob_start(); | ||
+ | $res = curl_exec ($curlsession); | ||
+ | $buffer = ob_get_contents(); | ||
+ | ob_end_clean(); | ||
+ | if (!$buffer) { | ||
+ | $returnVal = \"Curl Fetch Error : \" | ||
+ | }else{ | ||
+ | $returnVal = $buffer; | ||
+ | } | ||
+ | curl_close($curlsession); | ||
+ | return $returnVal; | ||
+ | } | ||
+ | ?></ | ||
+ | |||
+ | ====파일 전송==== | ||
+ | |||
+ | <code php><? | ||
+ | $post_data[' | ||
+ | $post_data[' | ||
+ | $ch = curl_init(); | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | $postResult = curl_exec($ch); | ||
+ | ?></ | ||
+ | |||
+ | ==== https 접속==== | ||
+ | |||
+ | <code php><? | ||
+ | $ch = curl_init(); | ||
+ | curl_setopt ($ch, CURLOPT_URL," | ||
+ | curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, | ||
+ | // default 값이 true 이기때문에 이부분을 조심 (https 접속시에 필요) | ||
+ | curl_setopt ($ch, CURLOPT_SSLVERSION, | ||
+ | curl_setopt ($ch, CURLOPT_HEADER, | ||
+ | curl_setopt ($ch, CURLOPT_POST, | ||
+ | curl_setopt ($ch, CURLOPT_POSTFIELDS, | ||
+ | curl_setopt ($ch, CURLOPT_TIMEOUT, | ||
+ | curl_setopt ($ch, CURLOPT_RETURNTRANSFER, | ||
+ | $result = curl_exec ($ch); | ||
+ | curl_close ($ch); | ||
+ | echo $result; | ||
+ | ?></ | ||
+ | |||
+ | ====curl을 이용한 Gmail 로그인==== | ||
+ | |||
+ | <code php><? | ||
+ | $src = " | ||
+ | $ch = curl_init(); | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt ($ch, CURLOPT_HEADER, | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | $res = curl_exec($ch); | ||
+ | curl_close($ch); | ||
+ | /** 결과는 Atom xml 형식이다. DOM 또는 xml 파싱 function을 이용해서 파싱하면 된다. **/ | ||
+ | echo $res; | ||
+ | ?></ | ||
+ | |||
+ | ==== cURL을 이용한 웹페이지 가져오기==== | ||
+ | |||
+ | <code php><? | ||
+ | function get_content($url) { | ||
+ | $agent = ' | ||
+ | $curlsession = curl_init (); | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | curl_setopt ($curlsession, | ||
+ | $buffer = curl_exec ($curlsession); | ||
+ | $cinfo = curl_getinfo($curlsession); | ||
+ | curl_close($curlsession); | ||
+ | if ($cinfo[' | ||
+ | { | ||
+ | return ""; | ||
+ | } | ||
+ | return $buffer; | ||
+ | } | ||
+ | ?></ | ||
+ | |||
+ | ====레퍼러 속이기==== | ||
+ | |||
+ | <file php> | ||
+ | $url = ' | ||
+ | $cookie_string = ' | ||
+ | $useragent = ' | ||
+ | $timeout = 60; | ||
+ | |||
+ | $rawhtml = curl_init(); | ||
+ | curl_setopt ($rawhtml, CURLOPT_URL, | ||
+ | curl_setopt ($rawhtml, CURLOPT_RETURNTRANSFER, | ||
+ | curl_setopt ($rawhtml, CURLOPT_REFERER, | ||
+ | curl_setopt ($rawhtml, CURLOPT_COOKIE, | ||
+ | curl_setopt ($rawhtml, CURLOPT_CONNECTTIMEOUT, | ||
+ | curl_setopt ($rawhtml, CURLOPT_USERAGENT, | ||
+ | $output = curl_exec($rawhtml); | ||
+ | curl_close($rawhtml); | ||
+ | </ | ||
+ | |||
+ | ===== cURL, Client URL Library Functions ===== | ||
+ | |||
+ | * curl_init : 세션 초기화, 핸들값 리턴 | ||
+ | * curl_setopt : 옵션 세팅 | ||
+ | * curl_exec : curl을 실행 | ||
+ | * curl_errno : 에러번호를 가져온다. | ||
+ | * curl_error : 에러 메시지를 가져온다. | ||
+ | * curl_getinfo : 상태 정보를 리턴한다. | ||
+ | * curl_close : curl 세션을 닫는다 | ||
+ | |||
+ | |||
+ | <file php> | ||
+ | function curl($url) | ||
+ | { // http:// | ||
+ | $ch = curl_init(); | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | $g = curl_exec($ch); | ||
+ | curl_close($ch); | ||
+ | return $g; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====curl_close==== | ||
+ | |||
+ | * 기능 : cURL 세션을 닫는다. | ||
+ | * 구문 : curl_close(resource ch) | ||
+ | |||
+ | cURL 세션을 닫고, 리소스를 비워준다. 또한 cURL 핸들은 삭제된다. | ||
+ | |||
+ | ==== curl_errno==== | ||
+ | |||
+ | * 기능 : 마지막 에러 번호를 리턴해 준다. | ||
+ | * 구문 : curl_errno(resource ch) | ||
+ | 만일 연산 후 어떠한 에러도 발행하지 않았다면 0(zero)을 리턴한다. | ||
+ | |||
+ | ===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: | ||
+ | |||
+ | * 1 - Unsupported protocol. This build of curl has no support for this protocol. | ||
+ | * 2 - Failed to initialize. | ||
+ | * 3 - URL malformat. The syntax was not correct. 잘못된 형식의 도메인 | ||
+ | * 5 - Couldn' | ||
+ | * 6 - Couldn' | ||
+ | * 7 - Failed to connect to host. | ||
+ | * 8 - FTP weird server reply. The server sent data curl couldn' | ||
+ | * 9 - FTP access denied. The server denied login or denied access to the particular resource or directory you wanted to reach. Most often you tried to change to a directory that doesn' | ||
+ | * 11 - FTP weird PASS reply. Curl couldn' | ||
+ | * 13 - FTP weird PASV reply, Curl couldn' | ||
+ | * 14 - FTP weird 227 format. Curl couldn' | ||
+ | * 15 - FTP can't get host. Couldn' | ||
+ | * 17 - FTP couldn' | ||
+ | * 18 - Partial file. Only a part of the file was transferred. | ||
+ | * 19 - FTP couldn' | ||
+ | * 21 - FTP quote error. A quote command returned error from the server. | ||
+ | * 22 - HTTP page not retrieved. The requested url was not found or returned another error with the HTTP error code being 400 or above. This return code only appears if -f/--fail is used. | ||
+ | * 23 - Write error. Curl couldn' | ||
+ | * 25 - FTP couldn' | ||
+ | * 26 - Read error. Various reading problems. | ||
+ | * 27 - Out of memory. A memory allocation request failed. | ||
+ | * 28 - Operation timeout. The specified time-out period was reached according to the conditions. | ||
+ | * 30 - FTP PORT failed. The PORT command failed. Not all FTP servers support the PORT command, try doing a transfer using PASV instead! | ||
+ | * 31 - FTP couldn' | ||
+ | * 33 - HTTP range error. The range " | ||
+ | * 34 - HTTP post error. Internal post-request generation error. | ||
+ | * 35 - SSL connect error. The SSL handshaking failed. | ||
+ | * 36 - FTP bad download resume. Couldn' | ||
+ | * 37 - FILE couldn' | ||
+ | * 38 - LDAP cannot bind. LDAP bind operation failed. | ||
+ | * 39 - LDAP search failed. | ||
+ | * 41 - Function not found. A required LDAP function was not found. | ||
+ | * 42 - Aborted by callback. An application told curl to abort the operation. | ||
+ | * 43 - Internal error. A function was called with a bad parameter. | ||
+ | * 45 - Interface error. A specified outgoing interface could not be used. | ||
+ | * 47 - Too many redirects. When following redirects, curl hit the maximum amount. | ||
+ | * 48 - Unknown TELNET option specified. | ||
+ | * 49 - Malformed telnet option. | ||
+ | * 51 - The peer's SSL certificate or SSH MD5 fingerprint was not ok \\ 에러 메시지) SSL: certificate subject name ' | ||
+ | * 52 - The server didn't reply anything, which here is considered an error. | ||
+ | * 53 - SSL crypto engine not found | ||
+ | * 54 - Cannot set SSL crypto engine as default | ||
+ | * 55 - Failed sending network data | ||
+ | * 56 - Failure in receiving network data | ||
+ | * 58 - Problem with the local certificate | ||
+ | * 59 - Couldn' | ||
+ | * 60 - Peer certificate cannot be authenticated with known CA certificates 인증서 신뢰 검증에 실패한 경우다. (통합인증서, | ||
+ | * 61 - Unrecognized transfer encoding | ||
+ | * 62 - Invalid LDAP URL | ||
+ | * 63 - Maximum file size exceeded | ||
+ | * 64 - Requested FTP SSL level failed | ||
+ | * 65 - Sending the data requires a rewind that failed | ||
+ | * 66 - Failed to initialise SSL Engine | ||
+ | * 67 - User, password or similar was not accepted and curl failed to login | ||
+ | * 68 - File not found on TFTP server | ||
+ | * 69 - Permission problem on TFTP server | ||
+ | * 70 - Out of disk space on TFTP server | ||
+ | * 71 - Illegal TFTP operation | ||
+ | * 72 - Unknown TFTP transfer ID | ||
+ | * 73 - File already exists (TFTP) | ||
+ | * 74 - No such user (TFTP) | ||
+ | * 75 - Character conversion failed | ||
+ | * 76 - Character conversion functions required | ||
+ | * 77 - Problem with reading the SSL CA cert (path? access rights?) | ||
+ | * 78 - The resource referenced in the URL does not exist | ||
+ | * 79 - An unspecified error occurred during the SSH session | ||
+ | * 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 | ||
+ | |||
+ | ====curl_error==== | ||
+ | * 기능 : 그 현재의 세션에 대한 마지막 에러 메시지를 되돌려 준다. | ||
+ | * 구문 : curl_error(resource ch) | ||
+ | * 만일 cURL 연산 후 에러를 발생하면 에러메시지를 되돌려 주며, 에러가 발생하지 않으면 빈문자열을 되돌려 준다. | ||
+ | |||
+ | ====curl_init==== | ||
+ | |||
+ | * 기능 : 세션을 초기화하고 curl_setopt(), | ||
+ | * 구문 : resource curl_init([string $url]) | ||
+ | * 옵션 Url 매개변수가 포함되어 있다면 CURLOPT_URL 옵션은 매개변수의 값을 셋팅할 것이다. curl_setopt()를 이용하여 셋팅할 수 있다. | ||
+ | * 리턴 : 핸들값 or False | ||
+ | * 예제 : 새로운 curl세션을 초기화하고 웹페이지를 페칭한다. | ||
+ | |||
+ | <code php><? | ||
+ | $ch = curl_init(); | ||
+ | curl_setopt($ch, | ||
+ | curl_setopt($ch, | ||
+ | curl_exec($ch); | ||
+ | curl_close($ch); | ||
+ | ?></ | ||
+ | |||
+ | ==== curl_exec ==== | ||
+ | |||
+ | * 기능 : 세션을 싱행한다. | ||
+ | * 구문 : curl_exec(resource ch) | ||
+ | |||
+ | 이 함수는 cURL 세션을 초기화하고 세션의 옵션들을 셋팅한 후에 불려져야만 한다. | ||
+ | 본 함수의 목적은 단순히 미리 정의된 cURL 세션을 실행하기 위한 것이기 때문이다. | ||
+ | |||
+ | ==== curl_getinfo ==== | ||
+ | |||
+ | * 기능 : 특정 전송에 대한 정보를 받는다. | ||
+ | * 구문 : crul_getinfo(resource ch [, int opt]) | ||
+ | |||
+ | ==== curl_setopt ==== | ||
+ | |||
+ | ===CURLOPT_FOLLOWLOCATION=== | ||
+ | |||
+ | TRUE to follow any " | ||
+ | FOLLOWLOCATION 옵션 설정. 위치 헤더가 존재하면 따라간다. | ||
+ | curl_setopt($ch, | ||
+ | |||
+ | ===CURLOPT_HEADER=== | ||
+ | |||
+ | TRUE to include the header in the output. | ||
+ | 헤더 정보를 받기 원한다면 이 옵션을 추가한다. | ||
+ | |||
+ | VALUE : 1 OR true | ||
+ | |||
+ | curl_setopt($ch, | ||
+ | |||
+ | ===CURLOPT_NOBODY=== | ||
+ | |||
+ | TRUE to exclude the body from the output. | ||
+ | 본문의 정보를 받기 원하지 않는다면 이 옵션을 추가한다. | ||
+ | |||
+ | ===CURLOPT_POST=== | ||
+ | |||
+ | TRUE to do a regular HTTP POST. This POST is the normal application/ | ||
+ | 전송 메소드를 설정한다. | ||
+ | |||
+ | VALUE : 1-POST, 0-GET | ||
+ | curl_setopt($ch, | ||
+ | |||
+ | ===CURLOPT_RETURNTRANSFER=== | ||
+ | |||
+ | TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. | ||
+ | REQUEST 에 대한 결과값을 받을 건지 체크 | ||
+ | |||
+ | (WRITERUNCTION 콜백을 사용하는 대신, curl_exec 함수을 위한 반환 값으로 원격지 내용을 받는다.) | ||
+ | #Resource ID 형태로 넘어옴 :: 내장 함수 curl_errno 로 체크 | ||
+ | curl_setopt($ch, | ||
+ | |||
+ | ===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). | ||
+ | 인증서 체크같은데 true 시 안되는 경우가 많다. | ||
+ | default 값이 true 이기때문에 이 부분을 조심 (https 접속시에 필요) | ||
+ | curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, | ||
+ | |||
+ | ===CURLOPT_PORT=== | ||
+ | |||
+ | An alternative port number to connect to. | ||
+ | |||
+ | CURLOPT_SSLVERSION | ||
+ | The SSL version (2 or 3) to use. By default PHP will try to determine this itself, although in some cases this must be set manually. | ||
+ | SSL 버젼 (https 접속시에 필요) | ||
+ | curl_setopt ($ch, CURLOPT_SSLVERSION, | ||
+ | |||
+ | ===CURLOPT_TIMEOUT=== | ||
+ | |||
+ | The maximum number of | ||
+ | | ||
+ | REQUEST 에 대한 결과값을 받는 시간타임 설정한다. | ||
+ | curl_setopt($ch, | ||
+ | |||
+ | ===CURLOPT_POSTFIELDS=== | ||
+ | |||
+ | The full data to post in a HTTP " | ||
+ | POST 메소드라면 파라미터 값들을 이 옵션에 정의하면 된다. | ||
+ | |||
+ | curl_setopt($cu, | ||
+ | |||
+ | | ||
+ | |||
+ | ex) $vars = " | ||
+ | |||
+ | ===CURLOPT_REFERER=== | ||
+ | |||
+ | The contents of the " | ||
+ | 리퍼러 정보를 설정 | ||
+ | |||
+ | ===CURLOPT_URL=== | ||
+ | |||
+ | The URL to fetch. This can also be set when initializing a session with curl_init(). | ||
+ | 접속할 url정보를 설정 | ||
+ | curl_init()에서 url를 설정하면 별도 설정이 필요없다. | ||
+ | |||
+ | < | ||
+ | curl_setopt($ch, | ||
+ | </ | ||
+ | |||
+ | ===CURLOPT_USERAGENT=== | ||
+ | |||
+ | The contents of the " | ||
+ | |||
+ | 에이전트 정보를 설정 | ||
+ | |||
+ | curl_setopt($ch, | ||
+ | |||
+ | |||
+ | ^ 누구나 수정할 수 있다. [[http:// | ||
+ | |||