PHP Extension Curl Usage detailed _php example

Source: Internet
Author: User
Tags curl

Features to implement:

1, the realization of remote access and collection of content

2, the implementation of PHP Web version of FTP upload download

3, the realization of analog landing: to a mail system, curl can simulate cookies

4, the implementation of interface docking (API), data transmission, etc.: through a platform to send text messages ah, crawl and transmit the information transmitted.

5, the implementation of analog cookies, such as: landing in the state can operate some properties.

How to use the Curl feature :

By default, PHP is not supported curl, you need to open the feature in php.ini

; Extension=php_curl.dll before the semicolon removed

1 The first step in the entire operation is to initialize with the Cur_init () function.

$curl = Curl_init (' www.jb51.net ')

2. Use the curl_setopt () function to set options.

3. After setting up, carry out the execution affairs curl_exec ($curl);

4 finally close curl_close ();

Use PHP curl for transfer and capture (post transfer mode): Get Remote Web page data

$user = "admin";
$pass = "admin";
$curlPost = "user= $user &pass= $pass";
$ch = Curl_init (); Initializes a Curl object
curl_setopt ($ch, Curlopt_url, "http://localhost/edu/login.php");
Set the URL you need to crawl
curl_setopt ($ch, Curlopt_returntransfer, 0);
Set the curl parameter to require that the result be output to the screen, when True is not returned to the Web page
assuming that the above 0 to 1, then the next $data need echo.
curl_setopt ($ch, Curlopt_post, 1);
Post Submit
curl_setopt ($ch, Curlopt_postfields, $curlPost);
$data = curl_exec ($ch);
Run Curl and request a Web page.
curl_close ($ch);
[/code]

Realize remote simulation landing the most basic part.

Curl also needs to configure the username and password, but is hidden by the browser.

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

Curl Analog Landing

Analog landing: is not landing to the PHP100 forum, you can see the corresponding information.

Analyze login Fields---> Keep cookies--> read cookies and jump to related pages--> crawl number

1, simulate the landing after the creation of a file to save cookie content

2. Simulate user login status by reading generated cookie content

3, to the relevant page to obtain the required content

Tempname Create a temporary file

The Tempnam () function creates a temporary file with a unique file name. If successful, the function returns a new temporary filename. Returns False if the failure occurs.

Tempnam (Dir,prefix)

Parameter description

Dir required. Specify the directory where temporary files are created.

Prefix required. Specify the beginning of the file name.

Equivalent to, 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, grab your username and password.

Use Curl simulation to login to PHP100 forum

1, analysis Landing required input box field name and the number of fields required

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

Code:

Initializes 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 to require that the results be saved to the string or to the screen.
curl_setopt ($curl, Curlopt_returntransfer, 0);
Run Curl, request Web page
$data = curl_exec ($curl);
Closes 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 "";
} else{
 echo "";
}
Print_r ($_post);
? >

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.