public function gtrendsdisplay($query) { $output = $query; $casper = new Casper(); $casper->setOptions(array('ignore-ssl-errors' => 'yes')); $casper->setUserAgent('Mozilla/4.0 (comptible; MSIE 6.0; Windows NT 5.1)'); $url = 'https://www.google.com/trends/explore#q=' . $query; $casper->start($url); $casper->wait(5000); $casper->captureSelector('#reportMain', $output); $casper->capturePage('pagesample.png'); $casper->run(); $regex = '#\\<div id="reportMain"\\>(.+?)\\<\\/div\\>#s'; preg_match($regex, $output, $matches); return $output; }
/** * Get Img throught CasperJs * @param string $url * @param array $params - [ * 'proxy' => ['host' => '', 'port' => '', 'login' => '', 'password' => ''], * 'waitForSelector' => ['#idOrOtherSel' => 1000], * 'wait' => 3 * ] * @return array */ public static function casperQueryPicture(array $params = []) { if (!is_dir(Yii::getAlias(self::IMG_STORAGE))) { return; } if (!isset($params['url']) || !isset($params['proxy']) || !isset($params['search_engine']) || !isset($params['code'])) { return false; } $imgName = date('Y-m-d') . "_{$params['search_engine']}_{$params['proxy']['host']}_{$params['code']}.png"; $filePath = Yii::getAlias(self::IMG_STORAGE . "{$imgName}"); $isFilePattern = Yii::getAlias(self::IMG_STORAGE . date('Y-m-d') . "_{$params['search_engine']}_{$params['proxy']['host']}*.png"); if (!empty(glob($isFilePattern))) { echo PHP_EOL . "CASPER: ALREADY"; return; } $options = []; if (!empty($params['proxy'])) { $options['proxy'] = "{$params['proxy']['host']}:{$params['proxy']['port']}"; $options['proxy-auth'] = "{$params['proxy']['login']}:{$params['proxy']['password']}"; } elseif (!empty(Proxy::$proxy)) { $proxy = current(Proxy::$proxy); $options['proxy'] = "{$proxy['host']}:{$proxy['port']}"; $options['proxy-auth'] = "{$proxy['login']}:{$proxy['password']}"; } $casper = new Casper(Yii::$app->params['casperJsPath']); $userAgent = Proxy::$userAgent != '' ? Proxy::$userAgent : self::WEB_BROWSER; $casper->setUserAgent($userAgent); $casper->setOptions($options); $casper->start($params['url']); if (!empty($params['waitForSelector'])) { $casper->waitForSelector(key($params['waitForSelector']), current($params['waitForSelector'])); } if (!empty($params['wait'])) { $casper->wait($params['wait']); } $casper->capture(array('top' => 0, 'left' => 0, 'width' => 800, 'height' => 600), $filePath); $casper->run(); $params['proxyLog']->addition = $filePath; $params['proxyLog']->save(); $clrPattern = Yii::getAlias(self::IMG_STORAGE . date('Y-m-d', strtotime('-4 days')) . "*"); foreach (glob($clrPattern) as $filename) { unlink($filename); } }
/** * Get Html throught CasperJs * @param string $url * @param array $params - [ * 'proxy' => ['host' => '', 'port' => '', 'login' => '', 'password' => ''], * 'waitForSelector' => ['#idOrOtherSel' => 1000] * ] * @return array */ public static function getHtmlByCasper($url, array $params = []) { $options = []; if (!empty($params['proxy'])) { $options['proxy'] = "{$params['proxy']['host']}:{$params['proxy']['port']}"; $options['proxy-auth'] = "{$params['proxy']['login']}:{$params['proxy']['password']}"; } elseif (!empty(Proxy::$proxy)) { $proxy = current(Proxy::$proxy); $options['proxy'] = "{$proxy['host']}:{$proxy['port']}"; $options['proxy-auth'] = "{$proxy['login']}:{$proxy['password']}"; } $casper = new Casper(Yii::$app->params['casperJsPath']); $casper->setUserAgent(self::WEB_BROWSER); $casper->setOptions($options); $casper->start($url); if (!empty($params['waitForSelector'])) { $casper->waitForSelector(key($params['waitForSelector']), current($params['waitForSelector'])); } $casper->run(); return ['currentUrl' => $casper->getCurrentUrl(), 'html' => $casper->getHTML()]; }
public function testDoubleClick() { $evaluateHtml = <<<TEXT <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>test evaluate</title> </head> <body> <script type="text/javascript"> function increase() { document.getElementById('theField').value++; } </script> <a id="theLink" href='#' onclick='javascript:increase()'>test</a> <input type="text" value="0" id="theField" /> </body> </html> TEXT; $filename = '/tmp/test-click.html'; file_put_contents($filename, $evaluateHtml); $casper = new Casper(); $casper->start($filename)->click("#theLink")->run(); print_r($casper->getOutput()); @unlink($filename); }