<?php
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use DB, phpQuery, Log;
log类库主要是用来做调试 打印输出
phpquery类库主要是用来做 网页上面的信息采集
db类库主要是用来做 数据库的处理
代码里面没有使用中间件!怕大家晕……
类里面的
public function __construct($row)
{
//
$this->row = $row;
}
不能省略这是队列工作任务类最重要的、传值方式!变量$row我传入的是一个对象值!然后把它赋给ArtCaiji类里面的私有对象$row;
*/
class ArtCaiji extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $row;
* Create a new job instance.
*
* @return void
*/
public function __construct($row)
{
$this->row = $row;
}
* 下面是我们要执行的代码
*
* @return void
*/
public function handle()
{
$metas = array();
$res = array();
if($this->row){
$html = $this->get_url_content($this->row['href']);
if($html){
phpQuery::newDoclamb($html);
foreach(pq('meta') as $meta){
$key = pq($meta)->attr('name');
$value= pq($meta)->attr('content');
$metas[strtolower($key)] = $value;
}
$title = trim(pq('.head-wrap .title')->text());
$articles = DB::table('articles')->where('art_title', $title)->first();
if($articles){
phpQuery::$documents = array();
return true;
}else{
$insertedId = DB::table('articles')->insertGetId([
'user_id' => '10000',
'cate_id' => $this->row['cate_id'],
'art_title' => $title,
'art_tags' => $metas['keywords'],
'copy_from' => '本站原创',
'copy_url' => 'http://www.webshowu.com',
'art_intro' => $metas['description'],
'art_content' => $this->ImgFindShift_5118(pq('.content')->html()),
'art_views' => '10',
'art_status' => '3',
'created_at' => date('Y-m-d H:i:s',time()),
'updated_at' => date('Y-m-d H:i:s',time()),
]);
if($insertedId){
$result = $this->zhanzhang_push_baidu("http://www.webshowu.com/artinfo-".$insertedId.".html");
}
}
phpQuery::$documents = array();
return true;
}else{
Log::info(var_export($this->row,true));
return true;
}
}else{
Log::info('对象是空值');
return true;
}
}
function ImgFindShift_5118($html){
if($html){
$array = array();
$dochtml = phpQuery::newDoclamb($html);
foreach(pq('img') as $img){
$val['src1'] = pq($img)->attr('data-original');
$val['src2'] = pq($img)->attr('src');
$array[] = $val;
}
foreach($array as $key => $str){
pq("img:eq($key)")->attr('src',$str['src1']);
}
return $dochtml;
}else{
return false;
}
}
function get_url_content($url, $proxy = true) {
$data = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER ,0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if($proxy){
$appKey = 'xxx';
$secret = 'xxxx';
$paramMap = array(
'app_key' => $appKey,
'timestamp' => date('Y-m-d H:i:s'),
'enable-simulate' => 'false',
);
ksort($paramMap);
$codes = $secret;
$auth = 'MYH-AUTH-MD5 ';
foreach ($paramMap as $key => $val) {
$codes .= $key . $val;
$auth .= $key . '=' . $val . '&';
}
$codes .= $secret;
$auth .= 'sign=' . strtoupper(md5($codes));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Proxy-Authorization: {$auth}"));
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXY, 'xxxx');
curl_setopt($ch, CURLOPT_PROXYPORT, 'xxx');
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
}
$data = curl_exec($ch);
if ($data === FALSE) {
return false;
}
curl_close($ch);
if (!$data) {
return false;
} else {
$encode = mb_detect_encoding($data, array('ascii', 'gb2312', 'utf-8', 'gbk'));
if($encode != 'utf-8'){
if($encode == 'EUC-CN' || $encode == 'CP936'){
$data = @mb_convert_encoding($data, 'utf-8', 'gb2312');
}else{
$data = @mb_convert_encoding($data, 'utf-8', $encode);
}
}
return $data;
}
}
function zhanzhang_push_baidu($url){
$urls = array($url);
$api = 'http://data.zz.baidu.com/urls?site=www.webshowu.com&token=6ujhg0alnRLbwZr7';
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
return $result;
}
}