Use PHP curl to simulate browser crawl site information

Source: Internet
Author: User
Tags http post

Curl is a file transfer tool that works with URL syntax in the command line mode. Curl is a file transfer tool that works with URL syntax in the command line mode.
It supports many protocols: FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP. Curl also supports HTTPS authentication, HTTP POST method, HTTP put method, FTP upload, Kerberos authentication, HTTP upload, proxy server, cookies, username/password Authentication, download file breakpoint continuation,
Upload file breakpoint Continuation, HTTP proxy server pipeline (proxy tunneling), even it also supports IPV6, SOCKS5 proxy server, upload files via HTTP proxy server to FTP server and so on, the function is very powerful.

Curl explains a

Application of Curl function in PHP
In simple terms, there are four steps.
Curl_init ();
curl_setopt ();
Curl_exec ();
Curl_close ();

The most important command is curl_setopt ();

A simple example of a POST request
index.php

Copy CodeThe code is as follows:
<?php
$url = "http://www.mytest.com/curl/login.php"; The requested URL address
$user = "zkg111"; User name
$pass = "123456";
$postdata = "User_name=". $user. " &password= ". $pass; Requested data, split with & symbol
$curl = Curl_init (); Turn on Curl
curl_setopt ($curl, Curlopt_url, $url); Set Request Address
curl_setopt ($curl, Curlopt_returntransfer, 1); Whether output 1 or true is not output 0 or false output
curl_setopt ($curl, Curlopt_post, 1); Whether to use the Post method to request
curl_setopt ($curl, Curlopt_postfields, $postdata); Post data


echo $data = curl_exec ($curl); Perform a curl operation
Curl_close ($curl);
?>


Below a simple example, I casually opened the Brotherhood of the Forum, and then simulated a brother even forum landing, if you need to post the principle is the same, transfer the page, submit data
It is important to note that the cookie is saved in the directory Windows7 below must be in the./temp directory, I started to create a new folder myself, found to be correct, but the cookie is not read the wrong time, it is still in a lot of places
Asked questions, but did not answer the right, tossing for a few days to save the file as./temp directory, remind other friends not to go blind like me.

Copy CodeThe code is as follows:
<?php
$url = "http://bbs.lampbrother.net/login.php";
$urls = "Http://bbs.lampbrother.net";
$LGT = 0;
$user = "XXXX";
$pass = "XXXX";
$question = 0;
$hideid = 1;
$cookie _file = Tempnam ('./temp ', ' Cookie ');
$postdata = "Forward=&jumpurl=". $urls. " &step=2&lgt= ". $lgt." &pwuser= ". $user." &pwpwd= ". $pass." &question= ". $question." &answer=&hideid= ". $hideid;
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $postdata);
curl_setopt ($ch, Curlopt_cookiejar, $cookie _file);
curl_setopt ($ch, curlopt_followlocation, true);
$data = curl_exec ($ch);
Curl_close ($ch);
Echo $data;
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, ' http://bbs.lampbrother.net/');
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, 0);
curl_setopt ($ch, Curlopt_cookiefile, $cookie _file);
Curl_exec ($ch);
Curl_close ($ch);
?>

Use PHP curl to simulate browser crawl site information

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.