当前位置:编程学习 > php >>

如何用PHP CURL功能 查询

问题补充: 我想查询 例如:GOOLE搜索www.qq.com 一共有多少条搜索结果 怎么利用PHP CURL 实现 写个简单易懂的例子给我就可以了 谢谢
补充: 快点啊 。。。都没人会啊 。。
追问:php 怎么会收费 curl_setopt();这个功能还很多
答案:用curl或fsockopen都可以或着你用file_get_contents更省劲直接把一个远程文件读到一个变量,然后你再分析这些数据,用正则preg_match把你需要用的数据取出来,采集就是这思路。

这里三个curl或fsockopen的例子,你可以参考一下

php抓取远程网页内容简单示例:www.phperz.com/php/php-article/061313K20081375.html

php中使用curl或fsockopen下载远程文件: www.phperz.com/php-promotion/Skills/030QT020091840.html

php的curl实现get和post

www.phperz.com/php/introduction/030QSR0091838.html

以下是采集的实例,要想做好采集,需要先熟悉curl系列函数参数等,筛选你想要的东西,初级点的就看字符替换,高级点的就是正则了,请详看实例:

<?php
set_time_limit(0);

//cookie保存目录
$cookie_jar = '/tmp/cookie.tmp';

/*函数------------------------------------------------------------------------------------------------------------*/

//模拟请求数据
function request($url,$postfields,$cookie_jar,$referer){
$ch = curl_init();
$options = array(CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_NOBODY => 0,
CURLOPT_PORT => 80,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_COOKIEFILE => $cookie_jar,
CURLOPT_REFERER => $referer
);
curl_setopt_array($ch, $options);
$code = curl_exec($ch);
curl_close($ch);
return $code;
}

//获取帖子列表
function getThreadsList($code){
preg_match_all('/ <!--[.|\r|\n]*? <a href=>return $threads[1];
}

//判断该帖子是否存在
function isExits($code){
preg_match('/ <p>指定的主题不存在或已被删除或正在被审核,请返回。 <\/p>/',$code,$error);
return isset($error[0])?false:true;
}

//获取帖子标题
function getTitle($code){
preg_match('/ <h1>[^ <\/h1>]*/',$code,$title_tmp);
$title = $title_tmp[0];
return $title;
}

//获取帖子作者:
function getAuthor($code){
preg_match('/ <a href=>$author = strip_tags($author_tmp[0]);
return $author;
}

//获取楼主发表的内容
function getContents($code){
preg_match('/ <div id=\"postmessage_\d+\" class=\"t_msgfont\">(.|\r|\n)*? <\/div>/',$code,$contents_tmp);
$contents = preg_replace('/images\//',' http://bbs.war3.cn/images/',$contents_tmp[0]);
return $contents;
}

//打印帖子标题
function printTitle($title){
echo " <strong> <h2>帖子标题: </h2> </strong>",strip_tags($title)," <br/> <br/>";
}

//输出帖子作者
function printAuthor($author){
echo " <strong> <h2>帖子作者: </h2> </strong>",strip_tags($author)," <br/> <br/>";
}

//打印帖子内容
function printContents($contents){
echo " <strong> <h2>作者发表的内容: </h2>",$contents," </strong> <br/>";
}

//错误
function printError(){
echo " <i>该帖子不存在! </i>";
}

/*函数列表end---------------------------------------------------------------------------------------------------*/


/*登录论坛 begin*/
$url = ' http://bbs.war3.cn/logging.php?action=login';
$postfields='loginfield=username&username=1nject10n&password=xxxxxx&questionid=0&cookietime=315360000&referer= http://bbs.war3.cn/&loginsubmit=提交';
request($url,$postfields,$cookie_jar,'');
unset($postfields,$url);
/*登录论坛 end*/


/*获取帖子列表(位于第一页的帖子) begin*/
$url = ' http://bbs.war3.cn/forumdisplay.php?fid=57';
$code = request($url,'',$cookie_jar,'');
$threadsList = getThreadsList($code);
/*获取帖子列表 end*/

//帖子序列
$rows = 0;

/*循环抓取所有帖子源代码 begin*/
foreach($threadsList as $list){
$url = " http://bbs.war3.cn/viewthread.php?tid=$list";

if(isExits($code)){
$code = request($url,'',$cookie_jar,'');
$color = $rows%2==0?'#00CCFF':'#FFFF33';
echo " <div style='background-color:$color'>";
echo " <h1>第",($rows+1),"贴: </h1> <br/>";
$author = getAuthor($code);
printAuthor($author);

$title = getTitle($code);
printTitle($title);

$contents = getContents($code);
printContents($contents);
echo " </div>";
$rows++;
}
else
printError();

echo "----------------------------------------------------------------------------------------- <br/> <br/>";
}
/*抓取源代码 end*/
?>

php curl是收费的!!!!!!

加参数curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);


如果帮助到您,请记得采纳为满意答案哈,谢谢!祝您生活愉快!

参考资料: http://vae.la

上一个:php sql查询报错
下一个:php截取某段英文

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,