PHP through the Curl function to obtain data, analog landing, post data

Source: Internet
Author: User
Tags curl

For example, use PHP curl to get web page data:

The code is as follows Copy Code

$ch =curl_init ();
Setting options, including URLs
curl_setopt ($ch, Curlopt_url, "http://www.111cn.net");
curl_setopt ($ch, curlopt_returntransfer,1);
curl_setopt ($ch, curlopt_header,0);
Execute and get the contents of the HTML document
$output =curl_exec ($ch);
Release Curl handle
Curl_close ($ch);


How to submit data using PHP Curl Post

The code is as follows Copy Code

$url = "http://www.111cn.net/curl_post.php";
$post _data=array (
"Nameuser" => "Syxrrrr",
"PW" => "123456"
);
$ch =curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer,1);
curl_setopt ($ch, curlopt_post,1);
curl_setopt ($ch, Curlopt_postfields, $post _data);
$output =curl_exec ($ch);
Curl_close ($ch);
Echo $output;

Obtain data, simulate landing, post data

The code is as follows Copy Code

/********************** Curl Series ***********************/
Get data directly through curl (including post, header, etc.)
/*
* $url: http if not an array;
* $header: Header file
* $post: Post mode submit Array form
* $cookies: 0 default no cookie,1 for setting, 2 for getting
*/
Public Function Curl_allinfo ($urls, $header = False, $post = false, $cookies = 0) {
$url = Is_array ($urls)? $urls [' 0 ']: $urls;
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);

Submit with Header
if ($header!= FALSE) {
curl_setopt ($ch, Curlopt_httpheader, $header);
}

Post Submission Method
if ($post!= FALSE) {
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $post);
}

if ($cookies = = 1) {
curl_setopt ($ch, Curlopt_cookiejar, "cookiefile");
}else if ($cookies = = 2) {
curl_setopt ($ch, Curlopt_cookiefile, "cookiefile");
}

if (Is_array ($urls)) {
curl_setopt ($ch, Curlopt_ssl_verifypeer, false);
curl_setopt ($ch, Curlopt_ssl_verifyhost, false);
}

$data = curl_exec ($ch);
Curl_close ($ch);
return $data;
}

Finally attached a mimic search engine spider to crawl the page

The code is as follows Copy Code

function Get_web_page ($url)
{
$options = Array (
Curlopt_returntransfer => True,//Return Web page back page
Curlopt_header => false,//No header information returned
Curlopt_followlocation => True,//follow redirects
Curlopt_encoding => "",//Handle all encodings
Curlopt_useragent => "Spider",//Set useragent
Curlopt_autoreferer => True,//set Referer on redirect
Curlopt_connecttimeout =>,//timeout on connect connection timeout
Curlopt_timeout =>,//TIMEOUT on response reply timeout
Curlopt_maxredirs =>//stop after redirects
);
$ch = Curl_init ($url);
Curl_setopt_array ($ch, $options);
$content = curl_exec ($ch);
$err = Curl_errno ($ch);
$errmsg = Curl_error ($ch);
$header = Curl_getinfo ($ch);
Curl_close ($ch);
$header [' errno '] = $err;
$header [' errmsg '] = $errmsg;
$header [' content '] = $content;
return $header;
}

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.