Php curl_init function usage description

Source: Internet
Author: User
Tags curl urlencode

First, we need to know that the installation of curl_init in php is not usable, and we must open an account before it can be used.

In php. ini in C/windows, I enabled the extension = php_curl.dll function, and then restarted apapche.

Check whether CURL extension support is available in PHP phpinfo!

Copy php_curl.dll to c:/windows/and c:/windows/system32 and restart apache.

CURL Library Function in PHP)

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
Basic example

The code is as follows: Copy code

?? 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.
POST data

The code is as follows: Copy code
?? Php
$ PhoneNumber = '2017 & prime ;;
$ 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.111cn.net/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
Use proxy server

The code is as follows: Copy code
?? 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 .111cn.net: 1080 & prime ;);
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.
Curl simulates login to the discuz program
<? Php
 

The code is as follows: Copy code

 
! Extension_loaded ('curl') & die ('The curl extension is not loaded .');
 
$ Discuz_url = 'http: // www.111cn.net '; // 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'] = '2017 & prime ;;
// 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 ('/<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 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 );

 

I am now using ie6 simulation:

The code is as follows: Copy code

Function crawl_dou_request ($ url, $ timeout = 1) {// php simulate get
Ob_start ();
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_USERAGENT, "MISE 6.0 & Prime ;);
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ File_msg = curl_exec ($ ch );
Curl_close ($ ch );
Return $ file_msg;
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.