Reposted from: www.csdn.netXI. Curl, client URL Library Function PHP supports libcurl (you can use different protocols to connect to and communicate with different servers )., Libcurl currently supports HTTP, https, FTP, Gopher, telnet, dict, file, and LDAP protocols. Libcurl also supports HTTPS certificate authorization, http post, http put, FTP upload (of course, you can also use php ftp extension), HTTP basic form upload, proxy, cookies, and user authentication. To use the curl function, you need to install the curl package. PHP requires you to use curl 7.0.2-Beta or later. If the curl version is earlier than 7.0.2-Beta, PHP will not work. To use PHP curl support, you must use-- With-curl [= dir]Re-compile PHP parameters (DIR is the directory containing the library and header files ). These functions are added in PHP 4.0.2. Once you compile PHP with cURL support, you can use the curl function. The basic idea is:Curl_init ()The function initializes the curl session, and then you can set all your options throughCurl_exec ()Function execution.Curl_close ()Function to end your session. The following is an example: Put the PHP homepage back into a file.
Example 1. Use the PHP curl module to retrieve the PHP Homepage
<?php$ch = curl_init ("http://www.php.net/");$fp = fopen ("php_homepage.txt", "w");curl_setopt ($ch, CURLOPT_FILE, $fp);curl_setopt ($ch, CURLOPT_HEADER, 0);curl_exec ($ch);curl_close ($ch);fclose ($fp);?> |
|
-
Directory List
-
Curl_init-initialize a curl session
-
Curl_setopt-set an option for Curl calls
-
Curl_exec-execute a curl session
-
Curl_close-close a curl session
-
Curl_version-returns the current curl version
Curl_init(PHP 4> = 4.0.2) Curl_init -- initialize a curl session Description int curl_init ([string url])
Curl_init ()The function initializes a new session and returns a curl handleCurl_setopt (),Curl_exec (), AndCurl_close ()Function usage. If the optional parameter is provided, the curlopt_url option is set to the value of this parameter. You can useCurl_setopt ()Manually set the function.
Example 1. initialize a new curl session and retrieve a webpage
<?php$ch = curl_init();curl_setopt ($ch, CURLOPT_URL, "http://www.zend.com/");curl_setopt ($ch, CURLOPT_HEADER, 0);curl_exec ($ch);curl_close ($ch);?> |
|
Curl_setopt(PHP 4> = 4.0.2) Curl_setopt -- set an option for Curl calls Description bool curl_setopt (int ch, string option, mixed value)
Curl_setopt ()The function sets options for a curl session.OptionParameters are the settings you want,ValueIs the value given by this option. The values of the following options will be used as long integer (inOption):
Curlopt_infilesize: When you upload a file to a remote site, this option tells PHP about the size of the file to be uploaded.
Curlopt_verbose: If you want curl to report every unexpected event, set this option to a non-zero value.
Curlopt_header: If you want to include a header in the output, set this option to a non-zero value.
Curlopt_noprogress: If you do not display a process entry for Curl transmission in PHP, set this option to a non-zero value.
Note:PHP automatically sets this option to a non-zero value. You should change this option only for debugging purposes.
Curlopt_nobody: If you do not want to include the body in the output, set this option to a non-zero value.
Curlopt_failonerror: If you want PHP to be invisible when an error occurs (HTTP code returns a value greater than or equal to 300), set this option to a non-zero value. By default, a normal page is returned, ignoring the code.
Curlopt_upload: If you want PHP to prepare for upload, set this option to a non-zero value.
Curlopt_post: If you want PHP to create a regular http post, set this option to a non-zero value. This post is a common application/X-WWW-from-urlencoded type, most of which are used by HTML forms.
Curlopt_ftplistonly: Set this option to a non-zero value. php will list the FTP directory names.
Curlopt_ftpappend: Set this option to a non-zero value. php overwrites the remote file of the application.
Curlopt_netrc: Set this option to a non-zero value. php will be in your ~. In the/netrc file, find the username and password of the remote site you want to establish a connection.
Curlopt_followlocation: Set this option to a non-zero value (like "Location:") header. The server will send it as part of the HTTP header (note that this is recursive, PHP will send the header like "Location ).
Curlopt_put: Sets this option to upload a file with HTTP as a non-zero value. To upload this file, you must set the curlopt_infile and curlopt_infilesize options.
Curlopt_mute: Set this option to a non-zero value. php will completely silence the curl function.
Curlopt_timeout: Sets the maximum number of seconds for a long integer.
Curlopt_low_speed_limit: Sets the number of long integers to control the number of bytes transmitted.
Curlopt_low_speed_time: Sets the number of long integers to control the number of seconds to transmit the number of bytes specified by curlopt_low_speed_limit.
Curlopt_resume_from: Pass a long integer parameter containing the byte offset address (the start form you want to transfer ).
Curlopt_sslversion: Pass a long parameter that contains the SSL version. By default, PHP will be determined by its own efforts. You must set it manually in more security scenarios.
Curlopt_timecondition: Pass a long parameter to specify how to process the curlopt_timevalue parameter. You can set this parameter to timecond_ifmodsince or timecond_isunmodsince. This is only used for HTTP.
Curlopt_timevalue: The number of seconds that have elapsed since January 1. This time will be used by the curlopt_timevalue option as the specified value, or by the default timecond_ifmodsince.
The values of the following options will be used as strings:
Curlopt_url: This is the URL you want to retrieve with PHP. You can also useCurl_init ()Set this option during function initialization.
Curlopt_userpwd: Transmits a string in the format of [username]: [Password] to act as a PHP connection.
Curlopt_proxyuserpwd: Transmits a string in the format of [username]: [Password] to connect to the HTTP proxy.
Curlopt_range: Pass a range you want to specify. It should be "X-Y" format, X or Y is excluded. HTTP shipping also supports several intervals separated by sentences (X-Y, N-M ).
Curlopt_postfields: Transmits a string of all data for the HTTP "Post" operation.
Curlopt_referer: Contains a "Referer" header string in an HTTP request.
Curlopt_useragent: Contains a "User-Agent" header string in the HTTP request.
Curlopt_ftpport: Pass an include
Curl_exec(PHP 4> = 4.0.2) Curl_exec -- execute a curl session Descriptionbool curl_exec (int ch)
After you initialize a curl session and set all the options for the session, this function will be called. It is only intended to execute a pre-determined curl SESSION (through the givenChParameters ). |