Introduction to PHP's Curl library and examples of using _php example

Source: Internet
Author: User
Tags urlencode

Using PHP's Curl library can be a simple and effective way to capture web pages. All you have to do is run a script and then analyze the pages you crawl, and then you can get the data you want in a program. Whether you want to take part of the data from a link, or take an XML file and import it into a database, it's a simple way to get the Web content, CURL is a powerful PHP library.

Curl function library in PHP (Client URL library function)

Copy Code code as follows:

Curl_close-closes a Curl session
curl_copy_handle-copies all the contents and parameters of a Curl connection resource
curl_errno-returns a numeric number containing the current session error message
curl_error-returns a string containing the current session error message
curl_exec-executes a Curl session
Curl_getinfo-get information on a Curl connection resource handle
curl_init-initialization of a curl session
curl_multi_add_handle-Add a separate curl handle resource to a curl batch session
Curl_multi_close-closes a batch handle resource
curl_multi_exec-resolves a curl batch handle
curl_multi_getcontent-returns the text stream of the obtained output
Curl_multi_info_read-gets the relevant transport information of the currently resolved curl
curl_multi_init-initialization of a curl batch handle resource
curl_multi_remove_handle-removes a handle resource from a curl batch handle resource
Curl_multi_select-get the sockets associated with the curl extension, which can then to be "selected"
curl_setopt_array-sets the session parameters as an array for a curl
Curl_setopt-set session parameters for a curl
curl_version-Get Curl-related version information
The Curl_init () function Initializes a curl session, and the only parameter to the Curl_init () function is optional, representing a URL address.
The Curl_exec () function is to execute a curl session, the only argument is the handle returned by the Curl_init () function.
The role of the Curl_close () function is to close a curl session, with the only argument being a handle returned by the Curl_init () function.

Example one: basic example

Copy Code code as follows:

﹤?php
Initializes a CURL object
$curl = Curl_init ();
Set the URL you want to crawl
curl_setopt ($curl, Curlopt_url, ' http://www.cmx8.cn ');
Set Header
curl_setopt ($curl, Curlopt_header, 1);
Sets the curl parameter to require that the results be saved to the string or to the screen.
curl_setopt ($curl, Curlopt_returntransfer, 1);
Run Curl, request Web page
$data = curl_exec ($curl);
Close URL Request
Curl_close ($curl);
Show the data you get
Var_dump ($data);
?>

Example two: Post data

sendsms.php, which can accept two form fields, one is the telephone number, one is the text message content.

Copy Code code as follows:

﹤?php
$phoneNumber = ' 13812345678 ';
$message = ' This 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.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 three: Using a proxy server

Copy Code code 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 FOUR: Analog login

Curl Analog Login Discuz program, suitable for DZ7.0, the username will be changed to your username, Userpass changed to your password on it.

Copy Code code as follows:

<?php
/**
* Curl Analog Login Discuz Program
* Not yet implemented to open the authentication Code of the forum login function
*/
!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 ';//Login page address
$get _url = $discuz _url. /my.php?item=threads '; My posts
$post _fields = Array ();
The following two items do not need to be modified
$post _fields[' loginfield '] = ' username ';
$post _fields[' loginsubmit '] = ' true ';
User name and password must be filled in
$post _fields[' username '] = ' lxvoip ';
$post _fields[' password '] = ' 88888888 ';
Security Questions
$post _fields[' QuestionID '] = 0;
$post _fields[' answer '] = ';
@todo Verification Code
$post _fields[' seccodeverify '] = ';
Get Form Formhash
$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 ('/<input\s*type= "hidden" \s*name= "Formhash" \s*value= "(. *?)" \s*\/>/i ', $contents, $matches);
if (!empty ($matches)) {
$formhash = $matches [1];
} else {
Die (' not found the Forumhash ');
}
Post data, getting cookies
$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);
Take the cookie above and get the content of the page that you need to log in to view
$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 the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.