CURL transmission and retrieval function _ PHP Tutorial

Source: Internet
Author: User
CURL transmission and retrieval. What is CURL? A file transfer tool that uses URL syntax like command line. It supports many protocols. It supports authentication. Php is commonly used to implement more complex transmission functions. what is CURL?
A file transfer tool that uses URL syntax like command line. It supports many protocols. It supports authentication. Php commonly implements more complex transmission functions.
Implemented functions:
1. remote acquisition and collection of content
2. implement FTP upload and download for PHP web edition
3. implement simulated login: go to an email system and use curl to simulate cookies.
4. interface interconnection (API) and data transmission: send text messages, capture and transmit the transmitted information through a platform.
5. implement simulated cookies. some attributes can be operated only when the logon status is enabled.


How to use the CURL function:
By default, PHP does not support CURL. you need to enable this function in php. ini.
; Extension = remove the semicolon before php_curl.dll
1. during the entire operation, the first step is to use the cur_init () function for initialization.
$ Curl = curl_init ('www .php100.com ')
2. use the curl_setopt () function to set options.
3. after setting, execute the transaction curl_exec ($ curl );
4. close curl_close ();


Use php curl for transmission and retrieval (post transmission): to obtain remote webpage data
$ User = "admin ";
$ Pass = "admin100 ";
$ CurlPost = "user = $ user & pass = $ pass ";
$ Ch = curl_init (); // initialize a CURL object
Curl_setopt ($ ch, CURLOPT_URL, "http: // localhost/edu/login. php ");
// Set the URL you want to capture
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0 );
// Set the curl parameter to determine whether the result is output to the screen. if it is true, it is not returned to the webpage.
Assume that the above 0 is changed to 1, then the next $ data needs to be echo.
Curl_setopt ($ ch, CURLOPT_POST, 1 );
// Post submission
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost );

$ Data = curl_exec ($ ch );
// Run curl to request the webpage.
Curl_close ($ ch );
The most basic part of remote simulated login.
Curl also requires the user name and password, but it is hidden by the browser.
========================================================== ==========================================
Curl simulated login

Simulated login: You can view the relevant information without logging on to the php100 Forum.
Analyze the login field ---> keep the cookie after login --> Read the cookie and jump to the relevant page --> capture count
1. create a file after simulated login to save the cookie content
2. simulate user login status by reading the generated cookie content
3. go to the relevant page to get the required content

Tempname: create a temporary file
The tempnam () function creates a temporary file with a unique file name. If yes, the function returns a new temporary file name. If it fails, false is returned.
Tempnam (dir, prefix)
Parameter description
Dir is required. Specifies the directory for creating temporary files.
Prefix is required. Specifies the start of a file name.
Equivalent to fopen fwirte fclose
It returns a boolean value. It is dangerous to use a third party to log on to your QQ or msn, because it can record your logon status and capture your username and password.

Use CURL to simulate login to the PHP100 Forum

1. analyze the field names and number of required fields in the input box required for login

2. Save the cookie to simulate the number of member Gold Coins obtained after login

Code:

// Initialize a cURL object
$ Curl = curl_init ();

// Set the URL you want to capture
Curl_setopt ($ curl, CURLOPT_URL, "http://www.baidu.com ");

// Set the cURL parameter to save the result to the string or output to the screen.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 0 );

// Run cURL to request the webpage
$ Data = curl_exec ($ curl );

// Close the URL request
Curl_close ($ curl );


$ User = "admin ";
$ Pass = "admin100 ";
$ CurlPost = "user = $ user & pass = $ pass ";
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, "http: // localhost/curl/login. php ");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0 );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost );
$ Data = curl_exec ($ ch );
Curl_close ($ ch );

?>

If ($ _ POST ['user'] = "admin "){
Echo "script" alert ('success') script ";
} Else {
Echo "script alert ('failed') script";
}
// Print_r ($ _ POST );
?>

=====

Simulated login code

Pw_php100.php
$ Cookie_file = tempnam ('./temp', 'cooker ');
$ Login_url = "http://bbs.php100.com/login.php ";
$ Post_fields = "cktime = 3600 & step = 2 & pwuser = php100 & pwpwd = 11111 ";
$ Ch = curl_init ($ login_url );
Curl_setopt ($ ch, CURLOPT_URL, "http://www.baidu.com ";
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0 );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_fields );
$ Data = curl_exec ($ ch );
Curl_close ($ ch );

$ Url = "http://bbs.php100.com/userpay.php ";
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file );
$ Contents = curl_exec ($ ch );
Preg_match ("/

  • Money: <\/li>/", $ contents, $ arr );
    Echo $ arr [1];
    Curl_close ($ ch );

    ?>

    ======

    Login. php

    Print_r ($ _ POST );

    ?>

    ================================

    Curl. php

    $ CurlPost = "user = $ user & pass = $ pass ";
    $ Ch = curl_init ();
    Curl_setopt ($ ch, CURLOPT_URL, "http: // localhost/edu/login. php ");
    Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0 );
    Curl_setopt ($ ch, CURLOPT_POST, 1 );
    Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost );
    $ Data = curl_exec ($ ch );
    Curl_close ($ ch );
    ?>

    Why? A file transfer tool that uses URL syntax like command line. It supports many protocols. It supports authentication. Php is commonly used to implement more complex transmission functions...

  • 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.