php根据关键字搜索百度网盘资源下载
2017-04-09
后端
<?php
//返回
/*
'title' => 资源名称
'downUrl' => 资源下载链接
'fileSize' => 资源大小
'format' => 资源类型
'shareUser' => 共享用户
*/
$url = 'http://blog.jaekj.com/MyZR/baiduPanDown/baiduPan.php';
$postFields = array(
's' => '教程', //关键字
'searcher' => 'baidu', //百度网盘
'page' => 1, //返回页数
'rows' => 100, //返回总条数
);
//模拟提交要搜索的内容
$data = curl_content($url, $postFields);
//截取json数据, 返回a到b区间的内容
$data = cut($data, '{"rows":', ',"total":');
//转换数组
$data = json_decode($data, true);
var_dump($data);
//截取指定区间内容
function cut($from, $start, $end, $lt = false, $gt = false)
{
$str = explode($start, $from);
if (isset($str['1']) && $str['1'] != '')
{
$str = explode($end, $str['1']);
$strs = $str['0'];
}
else
{
$strs = '';
}
if ($lt)
$strs = $start . $strs;
if ($gt)
$strs .= $end;
return $strs;
}
/*
* 访问网址并取得其内容
* @param $url String 网址
* @param $postFields Array 将该数组中的内容用POST方式传递给网址中
* @param $cookie_file string cookie文件
* @param $r_or_w string 写cookie还是读cookie或是两都都有,r读,w写,a两者,null没有cookie
* @return String 返回网址内容
*/
function curl_content($url, $postFields = null, $cookie_file = null, $r_or_w = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
if ($cookie_file && ($r_or_w == 'a' || $r_or_w == 'w')) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); // 存放Cookie信息的文件名称
}
if ($cookie_file && ($r_or_w == 'a' || $r_or_w == 'r')) {
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); // 读取上面所储存的Cookie信息
}
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (is_array($postFields) && 0 < count($postFields))
{
$postBodyString = "";
foreach ($postFields as $k => $v)
{
$postBodyString .= "$k=" . urlencode($v) . "&";
}
unset($k, $v);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
}
$reponse = curl_exec($ch);
if (curl_errno($ch)){
throw new Exception(curl_error($ch),0);
}
else{
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
}
curl_close($ch);
return $reponse;
}