php使用curl模拟登录后采集页面的例子_php实例

WBOY
Release: 2016-05-17 08:53:46
Original
856 people have browsed it

今天接到的功课是从一个网站获取商品库存,但是这个网站需要登录,我用fsockopen传递了整个header头都没用,只能求助于curl了。
附带说一下curl模块的开启办法:
(1)从php目录下拷贝:libeay32.dll,ssleay32.dll 到windows目录下。
(2)打开php.ini,查找“extension_dir = xxxxx”,确认后面的文件目录内有php_curl.dll文件。
(3)同样是php.ini,查找“extension=php_curl.dll”,确认它没有被注释(前面没有';')。
(4)重启apache,如果使用curl_init();语句出现错误提示,则说明没有安装成功。

复制代码 代码如下:

$curl = curl_init();
$cookie_jar = tempnam('./tmp','cookie');
curl_setopt($curl, CURLOPT_URL,'http://b2b.bookuu.com/b2b_club/checkUser.jsp');//这里写上处理登录的界面
curl_setopt($curl, CURLOPT_POST, 1);
$request = 'user=xxx&password=xxx';
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);//传 递数据
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);// 把返回来的cookie信息保存在$cookie_jar文件中
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设定返回 的数据是否自动显示
curl_setopt($curl, CURLOPT_HEADER, false);//设定是否显示头信 息
curl_setopt($curl, CURLOPT_NOBODY, false);//设定是否输出页面 内容
curl_exec($curl);//返回结果
curl_close($curl); //关闭

$curl2 = curl_init();
curl_setopt($curl2, CURLOPT_URL, 'http://b2b.bookuu.com/search/b2b_zxsm_new.jsp');//登陆后要从哪个页面获取信息
curl_setopt($curl2, CURLOPT_HEADER, false);
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl2, CURLOPT_COOKIEFILE, $cookie_jar);
$content = curl_exec($curl2);


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!