Example #1
0
            $ad->id = $id;
            $ad->title = $title;
            $ad->description = $description;
            $ad->price = $price;
            $ad->link = $link;
            $ad->save();
            echo "Added " . $id . " {$price}<br/>";
        } else {
            echo "Item already on database " . $id . "<br/>";
        }
    }
    #return false;
});
Route::get('htmlparser', function () {
    $parser = new HtmlDomParser();
    $html = $parser->file_get_html('http://www.kijiji.ca/v-mountain-bike/kitchener-waterloo/eranger-electric-mid-drive-fat-bike-48v-750w/1101241419');
    echo $html->plaintext;
    foreach ($html->find('span[itemprop=price]') as $span) {
        $price = $span->plaintext;
    }
    echo $price;
    foreach ($html->find('div[id=ImageThumbnails] img') as $img) {
        $src = str_replace('$_14', '$_27', $img->src);
        echo "<img src='{$src}'>";
    }
});
Route::get('mailtest', function () {
    $ads = App\Ad::whereEmailed(false)->get();
    foreach ($ads as $ad) {
        $data['ad'] = $ad;
        $ret = Mail::send(['html' => 'emails.ad'], $data, function ($message) use($data) {
 /**
  * 查询关键词排名
  * @param  string $keyword 关键词
  * @param  string $url     要查询的网站
  * @return string|int          查询结果
  */
 public function getRank($keyword = '阿瑞吡坦', $url = 'www.c-aring.com', $limit = 1)
 {
     Vendor('HtmlDomParser.HtmlDomParser');
     $retry = 0;
     $index = 0;
     for ($p = 0; $p < $limit; $p++) {
         $html = \HtmlDomParser::file_get_html('http://www.baidu.com/s?wd=' . urlencode($keyword) . '&pn=' . $p * 10);
         if (!$html->root) {
             if ($retry < 3) {
                 // 查询失败的话,重试两次
                 $p--;
                 $retry++;
             } else {
                 $retry = 0;
                 // 超出查询次数,重试次数归零
             }
             continue;
         }
         $retry = 0;
         // 查询成功,重试次数归零
         $ret = $html->find('div[class=c-container]');
         foreach ($ret as $i => $e) {
             $index++;
             $url_span = $e->find('span[class=g]');
             if (count($url_span) == 0) {
                 $url_span = $e->find('span[class=c-showurl]');
             }
             if (strpos($url_span[0]->plaintext, $url) !== false) {
                 return $index;
             }
         }
     }
     return '不在前' . $limit . '页';
 }