PHP web crawler

Source: Internet
Author: User

<?php

/* Web crawler */

#最简单的使用, the properties are in default values

/*

$curl =curl_init (' http://www.baidu.com ');

$output =curl_exec ($curl);

Curl_close ($curl);

Echo $output;

*/


#稍微复杂一点的, working with the page

/*

$curl =curl_init ();

curl_setopt ($curl, Curlopt_url, ' http://www.baidu.com ');//Can dynamically change the URL

curl_setopt ($curl, Curlopt_returntransfer, true);//Do not print directly on the browser

$output =curl_exec ($curl);//execution

Curl_close ($curl);//Close

Echo str_replace (' Baidu ', ' tiger Brother ', $output);

*/


#更复杂一点的, simulate a POST request

/*

$data = ' thecityname= Beijing ';

$curl =curl_init ();

curl_setopt ($curl, Curlopt_url, ' http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName ');

curl_setopt ($curl, Curlopt_header, 0);

curl_setopt ($curl, Curlopt_returntransfer, 1);

curl_setopt ($curl, Curlopt_post, 1);

curl_setopt ($curl, Curlopt_postfields, $data);

curl_setopt ($curl, Curlopt_httpheader,array (' Application/x-www-form-rulencode;charset=utf-8 ', ' Content-length '). Strlen ($data)));

$output =curl_exec ($curl);

if (!curl_errno ($curl)) {

$info =curl_getinfo ($curl);

Print_r ($info);

Header (' content-type:text/html;charset=utf-8; ');

Echo $output;

}else{

Echo ' Curl Error: '. Curl_error ($curl);

}

Curl_close ($curl);

*/


#抓取需要登录的页面

/*

$data = ' [email protected]&password=19900417&remember=1 ';

$curl =curl_init ();

curl_setopt ($curl, Curlopt_url, ' http://www.imooc.com/user/login ');

curl_setopt ($curl, Curlopt_returntransfer, true);//do not print directly after execution

Cookie-related settings, which need to be set before all calls are started

Date_default_timezone_set (' PRC ');//The time zone must be set when using cookies

curl_setopt ($curl, curlopt_cookiesession, true);

curl_setopt ($curl, Curlopt_cookiefile, ' cookiefile ');

curl_setopt ($curl, Curlopt_cookiejar, ' cookiefile ');

curl_setopt ($curl, Curlopt_cookie, Session_name (). ' = '. session_id ());

curl_setopt ($curl, curlopt_header,0);//Prohibit Output header information

curl_setopt ($curl, curlopt_followlocation, 1);

curl_setopt ($curl, Curlopt_post, 1);

curl_setopt ($curl, Curlopt_postfields, $data);

curl_setopt ($curl, Curlopt_httpheader,array (' Application/x-www-form-rulencode;charset=utf-8 ', ' Content-length: '). Strlen ($data)));

Curl_exec ($curl);//execution

curl_setopt ($curl, Curlopt_url, ' http://www.imooc.com/space/index ');

curl_setopt ($curl, curlopt_post,0);//download page is not a POST request, so 0

curl_setopt ($curl, Curlopt_httpheader,array (' content-type:text/xml '));

$output =curl_exec ($curl);

Echo $output;

*/


#从ftp服务器上下载一个文件

/*

$curl =curl_init ();

curl_setopt ($curl, Curlopt_url, ' ftp://10.14.12.22:21/test.txt ');

curl_setopt ($curl, curlopt_header,0);//Prohibit output header information, debug, can output

curl_setopt ($curl, Curlopt_returntransfer, 1);

curl_setopt ($curl, curlopt_timeout,300);//Set time-out, units per second

curl_setopt ($curl, Curlopt_userpwd, "admin:123");//ftp user name, password

$outfile =fopen (' Mytest.txt ', ' WB ');//Save to local file name

curl_setopt ($curl, Curlopt_file, $outfile);

$output =curl_exec ($curl);

Fclose ($outfile);

if (!curl_errno ($curl)) {

$info =curl_getinfo ($curl);

Print_r ($info);

echo "RETURN:" $output;

}else{

Echo ' Curl error: '. Curl_error ($curl);

}

Curl_close ($curl);

*/


#ftp文件上传

/*

$curl =curl_init ();

$uploadfile = ' demo1.php ';

$FP =fopen ($uploadfile, ' R ');

curl_setopt ($curl, Curlopt_url, ' ftp://10.14.12.22:21/uploadfile.php ');

curl_setopt ($curl, curlopt_header,0);

curl_setopt ($curl, curlopt_returntransfer,1);

curl_setopt ($curl, curlopt_timeout,300);

curl_setopt ($curl, curlopt_userpwd, ' admin:123 ');

curl_setopt ($curl, curlopt_upload,1);

curl_setopt ($curl, Curlopt_infile, $fp);

curl_setopt ($curl, Curlopt_infilesize,filesize ($uploadfile));

$output =curl_exec ($curl);

Fclose ($FP);

if (!curl_errno ($curl)) {

Echo ' UploadFile successfully ';

}else{

Echo ' UploadFile error: '. Curl_error ($curl);

}

Curl_close ($curl);

*/


#curl访问https上的资源

/*

$curl =curl_init ();

curl_setopt ($curl, Curlopt_url, ' https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js ');

curl_setopt ($curl, curlopt_returntransfer,true);

Date_default_timezone_set (' PRC ');

curl_setopt ($curl, curlopt_ssl_verifypeer,0);//terminating authentication from the service side

$output =curl_exec ($curl);

Curl_close ($curl);

Echo $output;

*/



$data = ' {

"Button": [

{

"Name": "I want to scour the book",

"Sub_button": [

{

"Type": "Click",

"Name": "Registered Account",

"Key": "Zcyh"

},

{

"Type": "Click",

"Name": "Account Binding",

"Key": "BD"

},

{

"Type": "Click",

"Name": "Book Search",

"Key": "TSSs"

},

{

"Type": "Click",

"Name": "Sell book order",

"Key": "[email protected]"

},

{

"Type": "Click",

"Name": "Buy book Order",

"Key": "[email protected]"

}

]

},

{

"Name": "Study Hard",

"Sub_button": [

{

"Type": "Click",

"Name": "Score Query",

"Key": "Cjcx"

},

{

"Type": "Click",

"Name": "Examination Room Enquiry",

"Key": "KCCX"

},

{

"Type": "Click",

"Name": "Timetable Query",

"Key": "Kbcx"

}

]

},

{

"Name": "Colorful Life",

"Sub_button": [

{

"Type": "Click",

"Name": "Genius Tool",

"Key": "Xbtool"

},

{

"Type": "View",

"Name": "Campus Perimeter",

"url": "Http://weixuebar.duapp.com/View/surround/schoolSurround.html"

},

{

"Type": "Click",

"Name": "Online translator",

"Key": "Zxfy"

},

{

"Type": "Click",

"Name": "Genius Community",

"Key": "XBSQ"

},

{

"Type": "View",

"Name": "Contact Us",

"url": "Http://weixuebar.duapp.com/View/help.html"

}

]

}

]

}‘;

$url = ' https://api.weixin.qq.com/cgi-bin/menu/create?access_token=4__FZWKzO1gP2_PhvUE9F9_ 1rpwy2whbqufkiuvrnbqhnfdrfangrogi0rxyswxuw2cykrfglktb5i5wtooloa ';

$curl =curl_init ();

curl_setopt ($curl, Curlopt_url, $url);

curl_setopt ($curl, curlopt_returntransfer,true);

curl_setopt ($curl, Curlopt_post, 1);

curl_setopt ($curl, Curlopt_postfields, $data);

Date_default_timezone_set (' PRC ');

curl_setopt ($curl, curlopt_ssl_verifypeer,0);//terminating authentication from the service side

curl_setopt ($curl, Curlopt_httpheader,array (' Application/x-www-form-rulencode;charset=utf-8 ', ' Content-length '). Strlen ($data)));

$output =curl_exec ($curl);

Curl_close ($curl);

Echo $output;




/*curl_setopt ($curl, Curlopt_url, ' http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName ');

curl_setopt ($curl, Curlopt_header, 0);

curl_setopt ($curl, Curlopt_returntransfer, 1);

curl_setopt ($curl, Curlopt_post, 1);

curl_setopt ($curl, Curlopt_postfields, $data);

curl_setopt ($curl, Curlopt_httpheader,array (' Application/x-www-form-rulencode;charset=utf-8 ', ' Content-length '). Strlen ($data)));

*/


PHP Official document URL: http://php.net/manual/zh/book.curl.php

?>


This article is from "Tiger Brother's Blog" blog, please be sure to keep this source http://7613577.blog.51cto.com/7603577/1531692

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.