Several examples of curl in PHP

Source: Internet
Author: User
Tags urlencode

Using the PHP curl library, you can easily and effectively grab Pages. You just need to run a script, then analyze the page you crawled, 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 the database, the fear is simply to get the Web content, CURL is a powerful PHP library.

Curl Libraries in PHP (Client URL library Function)

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

The Curl_init () function initializes a curl session, and the only parameter to the Curl_init () function is optional, representing a URL address.
The function of the curl_exec () function is to perform a curl session, and the only argument is the handle returned by the Curl_init () Function.
The function of the Curl_close () function is to close a curl session, and the only argument is the handle returned by the Curl_init () Function.


Example One: Basic Example

Basic examples
﹤?php
Initialize a CURL object
$curl = Curl_init ();
Set the URL you need to crawl
curl_setopt ($curl, curlopt_url, ' http://www1.qixoo.com ');
Set Header
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);

?>


Example Two: Post data

sendsms.php, which can accept two form fields, one is the phone number, and the other is the text message Content.


﹤?php
$phoneNumber = ' 13812345678 ';
$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.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

﹤?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 Simulation Login Discuz program, suitable for DZ7.0, will username changed to your user name, Userpass changed to your password on it.


<?php
/**
* Curl Emulation Login Discuz Program
* The forum login function that has not been implemented to enable verification code
*/
!extension_loaded (' Curl ') && die (' The curl extension are 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 ';
Safety 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, Obtaining 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 to get the page content you need to log in to see
$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);
?>

Several examples of curl in PHP
Pay attention to the public number and learn common progress together

Several examples of curl in PHP

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.