Curl transfer and get features _php tutorial

Source: Internet
Author: User
What is curl?
A file transfer tool that works by using URL syntax to love command-line mode. It supports many protocols. It supports authentication functions. More complex transfer functions are commonly implemented in PHP.
The functions implemented:
1. Realize remote acquisition and acquisition of content
2, the implementation of PHP Web version of the FTP upload download
3, realize the simulation landing: Go to a mail system, curl can simulate cookies
4, to achieve interface docking (API), data transmission, such as: through a platform to send text messages, crawling and transmitting the information transmitted.
5, the realization of analog cookies, etc.: the state of landing can be manipulated some properties.


How to use the Curl feature:
By default PHP does not support curl, you need to turn on this feature in php.ini
The semicolon in front of the Extension=php_curl.dll is removed
1 The first step during the entire operation is to initialize with the Cur_init () function.
$curl = Curl_init (' www.php100.com ')
2. Set the option with the curl_setopt () function.
3. After setting, perform the transaction curl_exec ($curl);
4 finally close curl_close ();


Use PHP curl for transfer and capture (post transfer): Get Remote Web page 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 that you want to crawl
curl_setopt ($ch, Curlopt_returntransfer, 0);
Set the curl parameter to ask if the result is output to the screen, true when it is not returned to the Web page
Assuming that the above 0 is replaced with 1, then the next $data needs echo.
curl_setopt ($ch, Curlopt_post, 1);
Post Submission
curl_setopt ($ch, Curlopt_postfields, $curlPost);

$data = curl_exec ($ch);
Run Curl and request the Web page.
Curl_close ($ch);
Realize the most basic part of remote simulation landing.
Curl also needs to configure the user name and password, but is hidden by the browser.
============================================================================
Curl Simulation Login

Analog landing: is not landing to the PHP100 forum, you can also see the corresponding information.
Analyze login Field---> Leave cookie after Login--read cookie and jump to related page--crawl count
1. Create a file after the login to save the cookie content
2. Simulate user login status by reading the generated cookie content
3, to the relevant page to obtain the required content

Tempname Creating a temporary file
The Tempnam () function creates a temporary file with a unique file name. If successful, the function returns a new temporary file name. If it fails, it returns false.
Tempnam (Dir,prefix)
Parameter description
Dir required. Specifies the directory where temporary files are created.
Prefix required. Specifies the beginning of the file name.
Equivalent, fopen fwirte fclose
It can return a Boolean value. Using a third party to login to your QQ, MSN is very dangerous, because it can record your login status, crawl your user name and password.

Login to the PHP100 forum using Curl emulation

1, analyze the login required input box field name and the number of fields required

2, save the cookie simulation login to obtain the number of member coins

Code

Initialize a CURL object
$curl = Curl_init ();

Set the URL you need to crawl
curl_setopt ($curl, Curlopt_url, "http://www.baidu.com");

Sets the curl parameter, which requires the result to be saved to a string or output to the screen.
curl_setopt ($curl, Curlopt_returntransfer, 0);

Run Curl, request a Web page
$data = curl_exec ($curl);

Close 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 "";
}else{
echo "";
}
Print_r ($_post);
?>

=====

Analog Login Code

pw_php100.php
$cookie _file=tempnam ('./temp ', ' Cookie ');
$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);
    ?>

    http://www.bkjia.com/PHPjc/815971.html www.bkjia.com true http://www.bkjia.com/PHPjc/815971.html techarticle What is curl? A file transfer tool that works by using URL syntax to love command-line mode. It supports many protocols. It supports authentication functions. PHP is commonly used to achieve 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.