메뉴 건너뛰기

홈피디자인닷컴

(*.138.65.73) 조회 수 37182 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print 수정 삭제
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print 수정 삭제
http://cafe.naver.com/q69/9086


XII. CURL, Client URL Library Functions




소개


PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.


These functions have been added in PHP 4.0.2.



요구 사항


In order to use the CURL functions you need to install the CURL package. PHP requires that you use CURL 7.0.2-beta or higher. PHP will not work with any version of CURL below version 7.0.2-beta. In PHP 4.2.3, you will need CURL version 7.9.0 or higher. From PHP 4.3.0, you will need a CURL version that's 7.9.8 or higher. PHP 5.0.0 will most likely require a CURL version greater than 7.10.5



설치


To use PHP's CURL support you must also compile PHP --with-curl[=DIR] where DIR is the location of the directory containing the lib and include directories. In the "include" directory there should be a folder named "curl" which should contain the easy.h and curl.h files. There should be a file named libcurl.a located in the "lib" directory. Beginning with PHP 4.3.0 you can configure PHP to use CURL for URL streams --with-curlwrappers.




Note to Win32 Users: In order to enable this module on a Windows environment, you must copy libeay32.dll and ssleay32.dll from the DLL folder of the PHP/Win32 binary package to the SYSTEM folder of your Windows machine. (Ex: C:WINNTSYSTEM32 or C:WINDOWSSYSTEM)



상수 정의


이 확장 모듈은 다음의 상수를 정의합니다. 이는 확장 모듈을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.





CURLOPT_PORT (integer)


CURLOPT_FILE (integer)


CURLOPT_INFILE (integer)


CURLOPT_INFILESIZE (integer)


CURLOPT_URL (integer)


CURLOPT_PROXY (integer)


CURLOPT_VERBOSE (integer)


CURLOPT_HEADER (integer)


CURLOPT_HTTPHEADER (integer)


CURLOPT_NOPROGRESS (integer)


CURLOPT_NOBODY (integer)


CURLOPT_FAILONERROR (integer)


CURLOPT_UPLOAD (integer)


CURLOPT_POST (integer)


CURLOPT_FTPLISTONLY (integer)


CURLOPT_FTPAPPEND (integer)


CURLOPT_NETRC (integer)


CURLOPT_FOLLOWLOCATION (integer)


CURLOPT_FTPASCII (integer)


CURLOPT_PUT (integer)


CURLOPT_MUTE (integer)


CURLOPT_USERPWD (integer)


CURLOPT_PROXYUSERPWD (integer)


CURLOPT_RANGE (integer)


CURLOPT_TIMEOUT (integer)


CURLOPT_POSTFIELDS (integer)


CURLOPT_REFERER (integer)


CURLOPT_USERAGENT (integer)


CURLOPT_FTPPORT (integer)


CURLOPT_LOW_SPEED_LIMIT (integer)


CURLOPT_LOW_SPEED_TIME (integer)


CURLOPT_RESUME_FROM (integer)


CURLOPT_COOKIE (integer)


CURLOPT_SSLCERT (integer)


CURLOPT_SSLCERTPASSWD (integer)


CURLOPT_WRITEHEADER (integer)


CURLOPT_SSL_VERIFYHOST (integer)


CURLOPT_COOKIEFILE (integer)


CURLOPT_SSLVERSION (integer)


CURLOPT_TIMECONDITION (integer)


CURLOPT_TIMEVALUE (integer)


CURLOPT_CUSTOMREQUEST (integer)


CURLOPT_STDERR (integer)


CURLOPT_TRANSFERTEXT (integer)


CURLOPT_RETURNTRANSFER (integer)


CURLOPT_QUOTE (integer)


CURLOPT_POSTQUOTE (integer)


CURLOPT_INTERFACE (integer)


CURLOPT_KRB4LEVEL (integer)


CURLOPT_HTTPPROXYTUNNEL (integer)


CURLOPT_FILETIME (integer)


CURLOPT_WRITEFUNCTION (integer)


CURLOPT_READFUNCTION (integer)


CURLOPT_PASSWDFUNCTION (integer)


CURLOPT_HEADERFUNCTION (integer)


CURLOPT_MAXREDIRS (integer)


CURLOPT_MAXCONNECTS (integer)


CURLOPT_CLOSEPOLICY (integer)


CURLOPT_FRESH_CONNECT (integer)


CURLOPT_FORBID_REUSE (integer)


CURLOPT_RANDOM_FILE (integer)


CURLOPT_EGDSOCKET (integer)


CURLOPT_CONNECTTIMEOUT (integer)


CURLOPT_SSL_VERIFYPEER (integer)


CURLOPT_CAINFO (integer)


CURLOPT_COOKIEJAR (integer)


CURLOPT_SSL_CIPHER_LIST (integer)


CURLOPT_BINARYTRANSFER (integer)


CURLCLOSEPOLICY_LEAST_RECENTLY_USED (integer)


CURLCLOSEPOLICY_LEAST_TRAFFIC (integer)


CURLCLOSEPOLICY_SLOWEST (integer)


CURLCLOSEPOLICY_CALLBACK (integer)


CURLCLOSEPOLICY_OLDEST (integer)


CURLINFO_EFFECTIVE_URL (integer)


CURLINFO_HTTP_CODE (integer)


CURLINFO_HEADER_SIZE (integer)


CURLINFO_REQUEST_SIZE (integer)


CURLINFO_TOTAL_TIME (integer)


CURLINFO_NAMELOOKUP_TIME (integer)


CURLINFO_CONNECT_TIME (integer)


CURLINFO_PRETRANSFER_TIME (integer)


CURLINFO_SIZE_UPLOAD (integer)


CURLINFO_SIZE_DOWNLOAD (integer)


CURLINFO_SPEED_DOWNLOAD (integer)


CURLINFO_SPEED_UPLOAD (integer)


CURLINFO_FILETIME (integer)


CURLINFO_SSL_VERIFYRESULT (integer)


CURLINFO_CONTENT_LENGTH_DOWNLOAD (integer)


CURLINFO_CONTENT_LENGTH_UPLOAD (integer)


CURLE_OK (integer)


CURLE_UNSUPPORTED_PROTOCOL (integer)


CURLE_FAILED_INIT (integer)


CURLE_URL_MALFORMAT (integer)


CURLE_URL_MALFORMAT_USER (integer)


CURLE_COULDNT_RESOLVE_PROXY (integer)


CURLE_COULDNT_RESOLVE_HOST (integer)


CURLE_COULDNT_CONNECT (integer)


CURLE_FTP_WEIRD_SERVER_REPLY (integer)


CURLE_FTP_ACCESS_DENIED (integer)


CURLE_FTP_USER_PASSWORD_INCORRECT (integer)


CURLE_FTP_WEIRD_PASS_REPLY (integer)


CURLE_FTP_WEIRD_USER_REPLY (integer)


CURLE_FTP_WEIRD_PASV_REPLY (integer)


CURLE_FTP_WEIRD_227_FORMAT (integer)


CURLE_FTP_CANT_GET_HOST (integer)


CURLE_FTP_CANT_RECONNECT (integer)


CURLE_FTP_COULDNT_SET_BINARY (integer)


CURLE_PARTIAL_FILE (integer)


CURLE_FTP_COULDNT_RETR_FILE (integer)


CURLE_FTP_WRITE_ERROR (integer)


CURLE_FTP_QUOTE_ERROR (integer)


CURLE_HTTP_NOT_FOUND (integer)


CURLE_WRITE_ERROR (integer)


CURLE_MALFORMAT_USER (integer)


CURLE_FTP_COULDNT_STOR_FILE (integer)


CURLE_READ_ERROR (integer)


CURLE_OUT_OF_MEMORY (integer)


CURLE_OPERATION_TIMEOUTED (integer)


CURLE_FTP_COULDNT_SET_ASCII (integer)


CURLE_FTP_PORT_FAILED (integer)


CURLE_FTP_COULDNT_USE_REST (integer)


CURLE_FTP_COULDNT_GET_SIZE (integer)


CURLE_HTTP_RANGE_ERROR (integer)


CURLE_HTTP_POST_ERROR (integer)


CURLE_SSL_CONNECT_ERROR (integer)


CURLE_FTP_BAD_DOWNLOAD_RESUME (integer)


CURLE_FILE_COULDNT_READ_FILE (integer)


CURLE_LDAP_CANNOT_BIND (integer)


CURLE_LDAP_SEARCH_FAILED (integer)


CURLE_LIBRARY_NOT_FOUND (integer)


CURLE_FUNCTION_NOT_FOUND (integer)


CURLE_ABORTED_BY_CALLBACK (integer)


CURLE_BAD_FUNCTION_ARGUMENT (integer)


CURLE_BAD_CALLING_ORDER (integer)


CURLE_HTTP_PORT_FAILED (integer)


CURLE_BAD_PASSWORD_ENTERED (integer)


CURLE_TOO_MANY_REDIRECTS (integer)


CURLE_UNKNOWN_TELNET_OPTION (integer)


CURLE_TELNET_OPTION_SYNTAX (integer)


CURLE_OBSOLETE (integer)


CURLE_SSL_PEER_CERTIFICATE (integer)



예제


Once you've compiled PHP with CURL support, you can begin using the CURL functions. The basic idea behind the CURL functions is that you initialize a CURL session using the curl_init(), then you can set all your options for the transfer via the curl_setopt(), then you can execute the session with the curl_exec() and then you finish off your session using the curl_close(). Here is an example that uses the CURL functions to fetch the example.com homepage into a file:






예 1. Using PHP's CURL module to fetch the example.com homepage






<?php

$ch
= curl_init("http://www.example.com/"
);
$fp = fopen("example_homepage.txt", "w"
);

curl_setopt($ch, CURLOPT_FILE, $fp
);
curl_setopt($ch, CURLOPT_HEADER, 0
);

curl_exec($ch
);
curl_close($ch
);
fclose($fp
);
?>




차례
curl_close -- Close a CURL session
curl_copy_handle --  Copy a cURL handle along with all of it's preferences
curl_errno -- Return the last error number
curl_error --  Return a string containing the last error for the current session
curl_exec -- Perform a CURL session
curl_getinfo --  Get information regarding a specific transfer
curl_init -- Initialize a CURL session
curl_multi_add_handle --  Add a normal cURL handle to a cURL multi handle
curl_multi_close --  Close a set of cURL handles
curl_multi_exec --  Run the sub-connections of the current cURL handle
curl_multi_getcontent --  Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
curl_multi_info_read --  Get information about the current transfers
curl_multi_init --  Returns a new cURL multi handle
curl_multi_remove_handle --  Remove a multi handle from a set of cURL handles
curl_multi_select --  Get all the sockets associated with the cURL extension, which can then be "selected"
curl_setopt -- Set an option for a CURL transfer
curl_version -- Return the current CURL version




add a note add a note User Contributed Notes
CURL, Client URL Library Functions

administrator at gesoft dot org
08-Dec-2005 02:08

Hello everybody,
The solution how to use proxy in correct way with curl was founded. Here is the part of code which was successfully used for checking proxy server and can be used by anybody as an example. During the research one very useful parameter of curl was discovered: CURLOPT_TIMEOUT. If script is using curl object with proxy parameter it is very important to set this parameter. There is possibility that proxy will not replay (die or big latency) until PHP script?s timeout exceed.

   $cUrl = curl_init();
   curl_setopt($cUrl, CURLOPT_URL, $requestUrl);
   curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($cUrl, CURLOPT_TIMEOUT, 'timeout_in_seconds');
   curl_setopt($cUrl, CURLOPT_PROXY, 'proxy_ip:proxy_port');
   $pageContent = trim(curl_exec($cUrl));
   curl_close($cUrl);

Hope this post will be useful
Sincerely yours
Vitali Simsive
P.S. The example of working script can be seen here
http://proxy-list.org/admin/checkproxy.php?proxy=?ip:port?&timeout=sec
If somebody is interesting to obtain personal copy of checkproxy.php please mail me.

abbyz at online dot ie
03-Dec-2005 09:27

To setup http proxy for connection use CURLPROXY_HTTP.

I found it is something problematic to configure connection based on CURLOPT_PROXY.

curl_setopt ( resource ch, CURLPROXY_HTTP, '
http://host:port');

administrator at gesoft dot org
12-Nov-2005 08:49

To use proxy in cUrl, not only CURLOPT_PROXY parameter should be set but CURLOPT_HTTPPROXYTUNNEL (to 1 or true) and CURLOPT_PROXYUSERPWD (if proxy requires authorization) as it was written in the cUrl?s official documentation.
Working code should be something like this:

$cUrl = curl_init();
curl_setopt($cUrl, CURLOPT_URL, ?google.com?);
curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cUrl, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($this->cUrl, CURLOPT_PROXY, 'proxy_name_or_ip:proxy_prot');
curl_setopt($this->cUrl, CURLOPT_PROXYUSERPWD, 'username:password');
$PageContent = curl_exec($cUrl);
curl_close($cUrl);

To be honest this code does not work on my server. Parameters? different variation had tried but in vain, the code still did not work. Finally personal proxy server was bought and checking started again, the result was negative again.

Before bringing up this question here, it was asked on many different PHP forums but in vain, nobody had the answer. There is only one suspicion: there is possibility that there is something wrong with hosting where this code was tested.

To find the right answer the code should be tested in different hosting.

admin at sellchain dot com
17-Oct-2005 11:43

TIP: If you are using fopen and fread to read HTTP or FTP or Remote Files, and experiencing some performance issues such as stalling, slowing down and otherwise, then it's time you learned a thing called cURL.

Performance Comparison:

10 per minute for fopen/fread for 100 HTTP files
2000 per minute for cURL for 2000 HTTP files

cURL should be used for opening HTTP and FTP files, it is EXTREMELY reliable, even when it comes to performance.

I noticed when using too many scripts at the same time to download the data from the site I was harvesting from, fopen and fread would go into deadlock. When using cURL i can open 50 windows, running 10 URL's from each window, and getting the best performance possible.

Just a Tip :)

Also here is some quick code that I use in my scripts now, of course credits go to other PHP'ers who have posted on this page, so I take zero credit.

TO POST TO A PAGE USING values and fields:
<?
$url
="http://anything"
;
$ch = curl_init
();
curl_setopt($ch, CURLOPT_URL, $url
);
curl_setopt ($ch, CURLOPT_POST, 1
);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "fieldname=fieldvalue&fieldname=fieldvalue&"
);
#curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1
);
$store = curl_exec ($ch
);
$content = curl_exec ($ch);
# This returns HTML
curl_close ($ch
);
?>

TO GET A PAGE USING A URL ONLY:
<?
$url
="http://anything"
;
$ch = curl_init
();
curl_setopt($ch, CURLOPT_URL, $url
);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1
);
$store = curl_exec ($ch
);
$xml = curl_exec ($ch
);
curl_close ($ch
);

?>

expert at yahoo dot com
08-Oct-2005 08:26

For more features using JavaScript XML HTTP Request Object components (AJAX), visit this page : http://expert.no-ip.org/?ajax

example.php
===========

<?php

require_once 'Ajax.php'
;
 
// add headers
$addheader[] = array("TEST1", "test one"
);
$addheader[] = array("TEST2", "test two"
);
 
// javascript code by http status
$cmd[] = array("alert({ajax})", 200
);
$cmd[] = array("alert('Error 404 - File not found !')", 404
);
$cmd[] = array("alert('HTTP Status '+{stat}+' !')", "DEFSTAT"
);
 
$obj = new Ajax
;
// get test.php body source using method GET
$obj->request("test.php"
);
// set additional header information
$obj->headers($addheader
);
 
?>
<html>
<head>
<?=$obj->head();
?>
</head>
<body>
<?=$obj->get($cmd);
?>
Example.php
</body>
</html>

test.php
========

<?php

// print header information HTTP_TEST1 & HTTP_TEST2
echo "HTTP_TEST1: ".$_SERVER['HTTP_TEST1']."n"
.
    
"HTTP_TEST2: ".$_SERVER['HTTP_TEST2'
];
 
?>

ROXORT at TGNOOB dot FR
21-Sep-2005 11:09

/*
  Here is a script that is usefull to :
  - login to a POST form,
  - store a session cookie,
  - download a file once logged in.
*/

// INIT CURL
$ch = curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, '
http://www.external-site.com/Members/Login.php');

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'fieldname1=fieldvalue1&fieldname2=fieldvalue2');

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);

// SET FILE TO DOWNLOAD
curl_setopt($ch, CURLOPT_URL, '
http://www.external-site.com/Members/Downloads/AnnualReport.pdf');

// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
$content = curl_exec ($ch);

// CLOSE CURL
curl_close ($ch);

/*
  At this point you can do do whatever you want
  with the downloaded file stored in $content :
  display it, save it as file, and so on.
*/

webmaster at oscontract dot com
31-Aug-2005 02:26

Just an addition to ksavage's script. Iam using HSBC API method of integration.

We may not need those array of xml tags.
We may need to check and fetch values of few strings.

<?

print_r
($vals); 
// print all the arrays.

print_r($vals[29]);
// print only the selected array.

//prints something like below.

//Array ( [tag] => ProcReturnMsg [type] => complete [level] => 6 [attributes] => Array ( [DataType] => String ) [value] => Approved )

$val1 = $vals[29
];

print_r ($val1[tag]); 
// ProcReturnMsg

print_r ($val1[value]);
//Approved

?>

imranlink at hotmail dot com
25-Aug-2005 10:05

If you need Specific Help in CURL PHP Please visit the site http://curl.phptrack.com

CURL PHP Forum (http://curl.phptrack.com/forum/index.php) help a lot for beginners students and PHP professional to write Commercial Applications by using PHP with cURL support.

alex at javafaq dot nu
04-Aug-2005 09:00

Hello!

CURL function make security hole at your site....

Just read that PHP cURL functions bypass open_basedir

protection, so users can navigate through

filesystem.
For example, setting "open_basedir" in php.ini to

"/var/www/html" anybody can retrieve "/etc/parla"

using cURL functions.

== Proof of concept (curl.php)

<?php

$ch
= curl_init("file:///etc/parla"
);

$file=curl_exec($ch
);

echo
$file

?>

More details here:
http://www.securityfocus.com/archive/1/379657/2004-10-26/2004-11-01/0

eric at harmonylinemusic dot com
25-Jun-2005 06:19

After trying both ksavage and mmm's attempts to send HTTP POST's with full xml in them, I was finally successful after using an obscure content-type (multipart/mixed) with a boundary declaration. Incidentally, CURLOPT_CUSTOMREQUEST only accepts method names like 'POST' or 'PUT', not entire headers. You need to use CURLOPT_HTTPHEADER.

<?php
$request
= "<xml>...</xml>"
;

      
$header[] = "Host: $host"
;
      
$header[] = "MIME-Version: 1.0"
;
      
$header[] = "Content-type: multipart/mixed; boundary=----doc"
;
      
$header[] = "Accept: text/xml"
;
      
$header[] = "Content-length: ".strlen($request
);
      
$header[] = "Cache-Control: no-cache"
;
      
$header[] = "Connection: close rn"
;
      
$header[] = $request
;
    
      
$ch = curl_init
();
      
curl_setopt($ch, CURLOPT_URL,$url
);
      
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1
);
      
curl_setopt($ch, CURLOPT_T

List of Articles
번호 제목 글쓴이 날짜 조회 수
30 비동기 자바스크립트와 XML 손님 2006.03.21 18082
29 AJAX 구현을 위한 JavaScript 라이브러리들 손님 2006.03.07 18065
28 겁나게 쉬운 자바스크립트 사운드 API 플래셔 2006.11.30 17966
27 Ajax와PHP로 리치 클라이언트 손님 2005.10.14 17874
26 [AJAX] 마소기사 - 풍부한 웹 UI 구현을 위한 기술 집합체, AJAX(1) 손님 2005.10.13 17842
25 [Ajax] DOM을 사용한 XML 데이터 다루기 2 플래셔 2006.11.22 17721
24 ajax 사용하기 위한 javascript 손님 2006.03.30 17444
23 지식iN·미니홈피가 웹 2.0이라고? 사용자 행복 추구하는 철학이 없다. 손님 2006.02.09 17424
22 AJAX를 이용한 Naver 키워드 검색 효과 구현 손님 2005.12.13 17284
21 AJAX를 이용한 Naver 키워드 검색 효과 구현 손님 2006.03.21 17231
20 JavaScript와 Ajax를 이용한 비동기식 요청 사모님 2006.12.15 16760
19 AJAX에서 즐겨찾기와 뒤로가기를 다루는 방법 손님 2006.03.21 16406
18 브라우저 개척자 마크 앤드리슨,「PHP가 자바보다 한수 위」 손님 2006.03.07 16226
17 Ajax 낙서 어플리케이션 만들기 손님 2006.03.21 16119
16 플래시 (flash)에 대해... 플래셔 2006.11.28 16010
15 Exactly About Online Flash Casino Games LeroyApplerothawjrmi 2014.04.15 11
14 Basons Sur Fr Primes Est Achat Option Binaire JustinaMcLemore 2014.04.15 9
13 Barcelona Sofa For Condo LasonyaRascoeswwm 2014.04.15 7
12 Air Purifier Controversy new EstelaStingleyrq 2014.04.16 6
11 First Look Penny Hardaway S Nike Air Flight One Returns 2012 new EstelaStingleyrq 2014.04.15 6
Board Pagination ‹ Prev 1 2 3 4 Next ›
/ 4

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

BUNTGAMES.COM © 2013