This article mainly introduces the cURL library of PHP and its examples. For more information, see the cURL library of PHP to easily and effectively capture webpages. You only need to run a script, analyze the web page you crawled, and then you can get the data you want as a program. Whether you want to retrieve part of the data from a link or an XML file and import it to the database, you may simply get the webpage content, cURL is a powerful PHP library.
CURL function library in PHP(Client URL Library Function)
The code is as follows:
Curl_close-close a curl session
Curl_copy_handle-copy all content and parameters of a curl connection resource
Curl_errno-a number containing the current session error message is returned.
Curl_error-returns a string containing the current session error message.
Curl_exec-execute a curl session
Curl_getinfo-obtains the information of a curl connection resource handle.
Curl_init-initialize a curl session
Curl_multi_add_handle-add a separate curl handle resource to the curl batch processing session
Curl_multi_close-close a batch processing handle resource
Curl_multi_exec-parse a curl batch handle
Curl_multi_getcontent-return the obtained output text stream
Curl_multi_info_read-obtains the transmission information of the currently resolved curl.
Curl_multi_init-initialize a curl batch processing handle resource
Curl_multi_remove_handle-remove a handle resource from the curl batch processing handle.
Curl_multi_select-Get all the sockets associated with the cURL extension, which can then be "selected"
Curl_setopt_array-set session parameters for a curl in the form of an array
Curl_setopt-set session parameters for a curl
Curl_version-obtain curl-related version information
The function curl_init () initializes a curl session. the unique parameter of the curl_init () function is optional, indicating a url address.
The role of the curl_exec () function is to execute a curl session. the unique parameter is the handle returned by the curl_init () function.
The function curl_close () is used to close a curl session. the only parameter is the handle returned by the curl_init () function.
Example 1: Basic example
The code is as follows:
<? Php
// Initialize a cURL object
$ Curl = curl_init ();
// Set the URL you want to capture
Curl_setopt ($ curl, CURLOPT_URL, 'http: // www.cmx8.cn ');
// Set the header
Curl_setopt ($ curl, CURLOPT_HEADER, 1 );
// Set the cURL parameter to save the result to the string or output to the screen.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
// Run cURL to request the webpage
$ Data = curl_exec ($ curl );
// Close the URL request
Curl_close ($ curl );
// Display the obtained data
Var_dump ($ data );
?>
Example 2: POST data
SendSMS. php can accept two form fields: phone number and text message content.
The code is as follows:
<? Php
$ PhoneNumber = '000000 ';
$ Message = 'This message was generated by curl and php ';
$ CurlPost = 'pnumber = '. urlencode ($ phoneNumber).' & MESSAGE = '. urlencode ($ message).' & SUBMIT = send ';
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.lxvoip.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 );
? >
Example 3: use a proxy server
The code is as follows:
<? Php
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.cmx8.cn ');
Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_HTTPPROXYTUNNEL, 1 );
Curl_setopt ($ ch, CURLOPT_PROXY, 'proxy .lxvoip.com: 1080 ');
Curl_setopt ($ ch, CURLOPT_PROXYUSERPWD, 'User: password ');
$ Data = curl_exec ();
Curl_close ($ ch );
? >
Example 4: simulate logon
Curl simulates login to the discuz program. it is suitable for DZ7.0. change username to your username and change userpass to your password.
The code is as follows:
<? Php
/**
* Curl simulates login to the discuz program
* You have not enabled the forum login function to enable the verification code.
*/
! Extension_loaded ('curl') & die ('The curl extension is not loaded .');
$ Discuz_url = 'http: // www.lxvoip.com '; // forum address
$ Login_url = $ discuz_url. '/logging. php? Action = login '; // logon page address
$ Get_url = $ discuz_url. '/my. php? Item = threads'; // my post
$ Post_fields = array ();
// The following two items do not need to be modified
$ Post_fields ['loginfield'] = 'username ';
$ Post_fields ['loginsubmit '] = 'true ';
// Username and password, required
$ Post_fields ['username'] = 'lxvoip ';
$ Post_fields ['password'] = '000000 ';
// Security question
$ Post_fields ['questionid'] = 0;
$ Post_fields ['answer'] = '';
// @ Todo verification code
$ Post_fields ['seccodeverify '] = '';
// Obtain the FORMHASH form
$ Ch = curl_init ($ login_url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ Contents = curl_exec ($ ch );
Curl_close ($ ch );
Preg_match ('/ /I ', $ contents, $ matches );
If (! Empty ($ matches )){
$ Formhash = $ matches [1];
} Else {
Die ('not found the forumhash .');
}
// POST the data to obtain the COOKIE
$ Cookie_file = dirname (_ FILE _). '/cookie.txt ';
// $ Cookie_file = tempnam ('/tmp ');
$ Ch = curl_init ($ login_url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_fields );
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file );
Curl_exec ($ ch );
Curl_close ($ ch );
// Obtain the page content that requires logon with the COOKIE obtained above.
$ Ch = curl_init ($ get_url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0 );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file );
$ Contents = curl_exec ($ ch );
Curl_close ($ ch );
Var_dump ($ contents );
?>
The above is all the content of this article. I hope you will like it.