Using curl, you can quickly crawl Web pages, simulate post, get requests, and not just some of the HTTP protocol packages, but also support FTP, proxy, HTTPS, LDAP and other applications.
Curl is not unique to PHP, many languages have, a thing to use, nature will be promoted, like many years ago log4j the same truth, just beginning is Java version, then also appear Log4c,log4c++⋯⋯curl is the same truth. Curl is not a PHP default support, you need to open this item in the configuration of the load extension= Php_curl.dll under Linux remember to compile PHP./configure plus parameter-with-curl. To see if the curl extension is open, pass phpinfo (); should be visible.
1) Basic usage (General Order of invocation)
Curl_init: Initialize,
curl_setopt: Setting parameter options
Curl_exec: Executing the current Curl object
Curl_close: Close the current Curl object
Example: To access the PHP page saved by the code below, we saw a page with 126 mailboxes.
$curl = Curl_init ();
Set the URL you need to crawl
curl_setopt ($curl, Curlopt_url, ' http://126.com ');
Sets whether headers display header information
curl_setopt ($curl, Curlopt_header, 1);
Sets the curl parameter, which requires the result to be saved to a string or output to the screen.
curl_setopt ($curl, Curlopt_returntransfer, 1);
Run Curl, request a Web page
$data = curl_exec ($curl);
Close URL Request
Curl_close ($curl);
Show the data obtained
Var_dump ($data);
2) post mode to send data
﹤?php
$phoneNumber = ' 13912345678 ';
$message = ' This message is generated by curl and PHP ';
$curlPost = ' pnumber= '. UrlEncode ($phoneNumber). ' &message= '. UrlEncode ($message). ' &submit=send ';
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, ' http://www.example.com/sendSMS.php ');
curl_setopt ($ch, Curlopt_header, 1);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $curlPost);
$data = Curl_exec ();
Curl_close ($ch);
? ﹥
3) Proxy Server
﹤?php
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, ' http://www.example.com ');
curl_setopt ($ch, Curlopt_header, 1);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_httpproxytunnel, 1);
curl_setopt ($ch, Curlopt_proxy, ' fakeproxy.com:1080 ');
curl_setopt ($ch, curlopt_proxyuserpwd, ' User:password ');
$data = Curl_exec ();
Curl_close ($ch);
? ﹥
4) About SSL and cookies
About SSL is the HTTPS protocol, you just need to curlopt_url the connection of http://Into https://. Of course, there is also a parameter called Curlopt_ssl_verifyhost that can be set to verify the site.
For cookies, you need to understand the following three parameters:
Curlopt_cookie, set a COOKIE in the face of the conversation
Curlopt_cookiejar, save a cookie when the session ends
The Curlopt_cookiefile,cookie file.
5) HTTP Server Authentication
﹤?php
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, ' http://www.example.com ');
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_httpauth, Curlauth_basic);
curl_setopt (Curlopt_userpwd, ' [Username]:[password] ')
$data = Curl_exec ();
Curl_close ($ch);
? ﹥
Summary: Although there are several examples attached, in fact the order is the same as the first one, but depending on the situation, the curl_setopt function will be different. We also found that the parameters of this function are very numerous and very flexible.
The following is an argument about this function's use:
BOOL curl_setopt (int ch, string option, mixed value)
The curl_setopt () function sets options for a curl session. The option parameter is the setting that you want, and value is the one given by this choice.
The values of the following options are used as long shapes (specified in the option parameter):
* Curlopt_infilesize: When you upload a file to a remote site, this option tells PHP the size of your upload file.
* Curlopt_verbose: If you want curl to report every unexpected thing, set this option to a value other than 0.
* Curlopt_header: If you want to include a header in the output, set this option to a value other than 0.
* Curlopt_noprogress: If you do not have PHP display a process bar for curl transfer, set this option to a value other than 0. Note: PHP automatically sets this option to a value other than 0, you should only change this option for debugging purposes.
* Curlopt_nobody: If you do not want to include the body part in the output, set this option to a value other than 0.
* Curlopt_failonerror: If you want PHP to not display when an error occurs (HTTP code returns greater than or equal to 300), set this option to one person other than 0 value. The default behavior is to return a normal page, ignoring the code.
* Curlopt_upload: If you want PHP to prepare for uploading, set this option to a value other than 0.
* Curlopt_post: If you want PHP to do a regular HTTP POST, set this option to a value other than 0. This post is an ordinary application/x-www-from-urlencoded type, most of which is used by HTML forms.
* Curlopt_ftplistonly: Set this option to a value other than 0, PHP will list the list of directory names for FTP.
* Curlopt_ftpappend: Set this option to a value other than 0, PHP will apply the remote file instead of overwriting it.
* CURLOPT_NETRC: Set this option to a value other than 0, PHP will look in your ~./netrc file for the user name and password of the remote site you want to connect to.
* Curlopt_followlocation: Set this option to a non-0 value (like "Location:") header, the server will send it as part of the HTTP header (note that this is recursive, PHP will be sent as "location:" the head).
* Curlopt_put: Set this option to use HTTP to upload a file for a value other than 0. To upload this file you must set the Curlopt_infile and Curlopt_infilesize options.
* Curlopt_mute: Set this option to a value other than 0, PHP will be completely silent for the curl function.
* Curlopt_timeout: Sets a long shape number, as the maximum continuation of how many seconds.
* Curlopt_low_speed_limit: Sets a long shaping number, controlling how many bytes are transferred.
* Curlopt_low_speed_time: Set a long shape number, control how many seconds to transmit curlopt_low_speed_limit the specified number of bytes.
* Curlopt_resume_from: Pass a long shaping parameter that contains the byte offset address (the start form you want to transfer to).
* Curlopt_sslversion: Pass a long parameter containing the SSL version. The default PHP will be determined by its own efforts, and in more security you must set it up manually.
* Curlopt_timecondition: Pass a long parameter specifying how to handle the Curlopt_timevalue parameter. You can set this parameter to Timecond_ifmodsince or timecond_isunmodsince. This is used only for HTTP.
* Curlopt_timevalue: Pass a number of seconds starting from 1970-1-1 to now. This time will be used by the Curlopt_timevalue option as the specified value, or by default timecond_ifmodsince.
The values of the following options will be used as strings:
Www.2cto.com
* Curlopt_url: This is the URL address you would like to retrieve with PHP. You can also set this option when initializing with the Curl_init () function.
* Curlopt_userpwd: Pass a shape like [Username]:[password] style string, function PHP to connect.
* Curlopt_proxyuserpwd: Pass a string that is formatted as [Username]:[password] to connect to the HTTP proxy.
* Curlopt_range: Pass a range that you want to specify. It should be in the "X-y" format, except for X. or Y. HTTP delivery also supports several intervals, separated by a phrase (x-y,n-m).
* Curlopt_postfields: Pass a string of all data as an HTTP "POST" operation.
* Curlopt_referer: A string containing a "REFERER" header in the HTTP request.
* Curlopt_useragent: A string containing a "user-agent" header in the HTTP request.
* Curlopt_ftpport: Pass an IP address that contains the FTP "POST" instruction used. This post command tells the remote server to connect to the IP address we specified. This string can be an IP address, a host name, a network interface name (under Unix), or '-' (using the system default IP address).
* Curlopt_cookie: Pass a header connection that contains an HTTP COOKIE.
* Curlopt_sslcert: Pass a string containing a certificate in PEM format.
* CURLOPT_SSLCERTPASSWD: Pass a password that contains required to use the Curlopt_sslcert certificate.
* Curlopt_cookiefile: A string that passes the name of a file that contains cookie data. This cookie file can be a Netscape format, or an HTTP-style header that is stockpiled in a file.
* Curlopt_customrequest: When making an HTTP request, pass a character to be used by get or head. For a delete or other operation is beneficial, more pass a string to is used instead of GET or HEAD when doing an HTTP request. This was useful for doing or another, and more obscure, HTTP request. Note: Do not do this before confirming your server support commands. The following options require a file description (obtained by using the fopen () function):
* Curlopt_file: This file will be the output file you placed to send, the default is stdout.
* Curlopt_infile: This file is the input file you sent over.
* Curlopt_writeheader: This file is written with the head section of your output.
* Curlopt_stderr: This file is written with errors rather than STDERR. To get an example of a page that needs to be signed in, the current practice is to log in once every time, and the person who needs to do it is improved.
Common functions:
curl_close-closing a Curl session
curl_copy_handle-Copy all the contents and parameters of a Curl connection resource
curl_errno-returns a numeric number that contains the current session error information
curl_error-returns a string containing the current session error message
Curl_exec-performing a Curl session
Curl_getinfo-gets the information for a Curl connection resource handle
curl_init-initialization of a curl session
curl_multi_add_handle-Adding a separate curl handle resource to a curl batch session
curl_multi_close-closing a batch handle resource
curl_multi_exec-parsing a Curl batch handle
curl_multi_getcontent-returns the text stream of the obtained output
Curl_multi_info_read-Gets the related transfer information for the currently resolved curl
curl_multi_init-initializing a curl batch handle resource
curl_multi_remove_handle-removing a handle resource from the Curl batch handle resource
Curl_multi_select-get all the sockets associated with the CURL extension, which can and be "selected"
curl_setopt_array-set session parameters as an array for a curl
curl_setopt-setting session parameters for a curl
curl_version-getting the version information about Curl
Author -274°c
http://www.bkjia.com/PHPjc/478522.html www.bkjia.com true http://www.bkjia.com/PHPjc/478522.html techarticle using curl, you can quickly crawl Web pages, simulate post, get requests, and not just some of the HTTP protocol packages, but also support FTP, proxy, HTTPS, LDAP and other applications. Curl is not PHP-specific ...