HolidayPyo > 정보게시판 > CURL이 먼지 알고 사용하자

CURL이 먼지 알고 사용하자
CURL이 먼지 알고 사용하자
글쓴이 : 별마음   날짜 : 16-05-25 17:42  
조회 : 359


Curl

cURL (Client URL Library Functions)

커맨드라인에서 URL 문법을 사용하여 파일을 전송하는 프로그램

내가 원하는 주소의 페이지에서 내가 임의의 값을 넣고 그 넣은 값으로 페이지에서 리턴되는 값을 받아오는 역할을 한다.

 PHP에서 cURL을 사용하려는 사람들 대부분이 아마도 HTTPS 접속 때문일 것이다.

소켓 또는 그 외 여러가지 접속방법이 있는데 굳이 cURL을 사용하는 건 속도면에서도 빠르고 HTTPS도 쉽게 접속할 수 있기 때문이다.

cURL 모듈을 서버에 설치해야 한다.(리눅스 - curl.so, 윈도우 - php_curl.dll 확장모듈 필요)

cURL로 가능한 일

POST방식으로 데이터 전송(simple)

POST방식으로 데이터 전송(function)

파일 전송

https 접속

curl을 이용한 Gmail 로그인

cURL을 이용한 웹페이지 가져오기

레퍼러 속이기

​CURL Library Functions

​curl_init : 세션 초기화, 핸들값 리턴

  • curl_setopt : 옵션 세팅
  • curl_exec : curl을 실행
  • curl_errno : 에러번호를 가져온다.
  • curl_error : 에러 메시지를 가져온다.
  • curl_getinfo : 상태 정보를 리턴한다.
  • curl_close : curl 세션을 닫는다
  • curl_close

    • 기능 : cURL 세션을 닫는다.
    • 구문 : curl_close(resource ch)

    cURL 세션을 닫고, 리소스를 비워준다. 또한 cURL 핸들은 삭제된다.

    <form class="button btn_secedit" action="/tech/curl" method="post">

    curl_errno
    </form>
    • 기능 : 마지막 에러 번호를 리턴해 준다.
    • 구문 : curl_errno(resource ch)

    만일 연산 후 어떠한 에러도 발행하지 않았다면 0(zero)을 리턴한다.

    <form class="button btn_secedit" action="/tech/curl" method="post">

    Error Code
    </form>

    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't resolve proxy. The given proxy host could not be resolved.
    • 6 - Couldn't resolve host. The given remote host was not resolved. 해당 도메인을 못찾는다는 뜻. 네임서버가 제대로 등록되어 있는지 확인하고 도메인도 정확한지 확인할 것.
    • 7 - Failed to connect to host.
    • 8 - FTP weird server reply. The server sent data curl couldn't parse.
    • 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't exist on the server.
    • 11 - FTP weird PASS reply. Curl couldn't parse the reply sent to the PASS request.
    • 13 - FTP weird PASV reply, Curl couldn't parse the reply sent to the PASV request.
    • 14 - FTP weird 227 format. Curl couldn't parse the 227-line the server sent.
    • 15 - FTP can't get host. Couldn't resolve the host IP we got in the 227-line.
    • 17 - FTP couldn't set binary. Couldn't change transfer method to binary.
    • 18 - Partial file. Only a part of the file was transferred.
    • 19 - FTP couldn't download/access the given file, the RETR (or similar) command failed.
    • 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't write data to a local filesystem or similar.
    • 25 - FTP couldn't STOR file. The server denied the STOR operation, used for FTP uploading.
    • 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't use REST. The REST command failed. This command is used for resumed FTP transfers.
    • 33 - HTTP range error. The range "command" didn't work.
    • 34 - HTTP post error. Internal post-request generation error.
    • 35 - SSL connect error. The SSL handshaking failed.
    • 36 - FTP bad download resume. Couldn't continue an earlier aborted download.
    • 37 - FILE couldn't read file. Failed to open the file. Permissions?
    • 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 'www.test.co.kr' does not match target host name 'test.co.kr'
      해결1) 인증서 발급받은 주소로 호출하거나
      해결2) 설정으로 제어
      $soapClient→setOpt('curl', CURLOPT_SSL_VERIFYHOST, 0);
    • 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't use specified SSL cipher
    • 60 - Peer certificate cannot be authenticated with known CA certificates 인증서 신뢰 검증에 실패한 경우다. (통합인증서, 번들과 관련 있는 듯) 에러 메시지) SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed 해결) 신뢰 검증을 하지 않도록 설정.상호 서버가 신뢰된다면 굳이 신뢰성을 검증하지 않아도 된다. $soapClient→setOpt('curl', CURLOPT_SSL_VERIFYPEER, 0);
    • 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
    <form class="button btn_secedit" action="/tech/curl" method="post">
    curl_error
    </form>
    • 기능 : 그 현재의 세션에 대한 마지막 에러 메시지를 되돌려 준다.
    • 구문 : curl_error(resource ch)
    • 만일 cURL 연산 후 에러를 발생하면 에러메시지를 되돌려 주며, 에러가 발생하지 않으면 빈문자열을 되돌려 준다.
    <form class="button btn_secedit" action="/tech/curl" method="post">

    curl_init
    </form>
    • 기능 : 세션을 초기화하고 curl_setopt(), curl_exec() 그리고 curl_close 함수들과 함께 사용하기 위해 CURL 핸들값을 리턴해준다.
    • 구문 : resource curl_init([string $url])
    • 옵션 Url 매개변수가 포함되어 있다면 CURLOPT_URL 옵션은 매개변수의 값을 셋팅할 것이다. curl_setopt()를 이용하여 셋팅할 수 있다.
    • 리턴 : 핸들값 or False
    • 예제 : 새로운 curl세션을 초기화하고 웹페이지를 페칭한다.