php下通过curl抓取yahoo boss 搜索结果的实现代码
2015-01-24信息快讯网
php下通过curl抓取yahoo boss 搜索结果的实现代码,需要的朋友可以参考下。
1.编写curl类,进行网页内容抓取
class CurlUtil
{
private $curl;
private $timeout = 10;
/**
* 初始化curl对象
*/
public function __construct()
{
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
curl_setopt($this->curl, CURLOPT_HEADER, false); //设定是否显示头信息
curl_setopt($this->curl, CURLOPT_NOBODY, false); //设定是否输出页面内容
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
}
/**
* 注销函数 关闭curl对象
*/
public function __destruct()
{
curl_close($this->curl);
}
/**
* 获取网页的内容
*/
public function getWebPageContent($url)
{
curl_setopt($this->curl, CURLOPT_URL, $url);
return curl_exec($this->curl);
}
}
2.创建curl对象
$CurlUtil = new CurlUtil();
3.抓取yahoo搜索结果
function getYahooSearch(CurlUtil $curl, $key)
{
$key = urlencode($key);
$searchUrl = "http://boss.yahooapis.com/ysearch/web/v1/$key?appid=你的雅虎appid&lang=tzh®ion=hk&abstract=long&count=20&format=json&start=0&count=10";
$josnStr = $curl->getWebPageContent($searchUrl);
$searchDataInfo = json_decode($josnStr, true);
$searchData = $searchDataInfo['ysearchresponse']['resultset_web'];
$returnArray = array();
if (!empty($searchData)) {
foreach ($searchData as $data) {
$returnArray[] = array("url" => $data['url'], "date" => $data['date'], 'title' => strip_tags($data['title']), 'description' => strip_tags($data['abstract']));
}
}
return $returnArray;
}
4.测试结果
var_dump(getYahooSearch($CurlUtil, "百度"));
php curl 伪造IP来源的实例代码
解析百度搜索结果link?url=参数分析 (全)
PHP fopen 读取带中文URL地址的一点见解
PHP curl 并发最佳实践代码分享
PHP文章采集URL补全函数(FormatUrl)
header中Content-Disposition的作用与使用方法
php中通过curl smtp发送邮件
php DOS攻击实现代码(附如何防范)
php模拟post行为代码总结(POST方式不是绝对安全)
php中通过curl模拟登陆discuz论坛的实现代码
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
php中$_REQUEST、$_POST、$_GET的区别和联系小结
防止本地用户用fsockopen DDOS攻击对策
php获取后台Job管理的实现代码
PHP缩略图等比例无损压缩,可填充空白区域补充色
php获取post中的json数据的实现方法
PHP 获取远程网页内容的代码(fopen,curl已测)
关于php curl获取301或302转向的网址问题的解决方法
基于PHP的cURL快速入门教程 (小偷采集程序)
PHP curl_setopt()函数实例代码与参数分析
php file_put_contents()功能函数(集成了fopen、fwrite、fclose)
centos 5.6 升级php到5.3的方法
用PHP的超级变量$_POST获取HTML表单(HTML Form) 数据
PHP中使用CURL伪造来路抓取页面或文件
php中利用post传递字符串重定向的实现代码