#php
#php
Вопрос:
Я хотел бы создать PHP-скрипт, который перейдет на другой веб-сайт (с заданным URL) и проверит источник страницы этой страницы на наличие определенной строки данных.
На самом деле у меня есть способ сделать это прямо сейчас, но я ищу альтернативный способ.
Прямо сейчас я использую php-функцию file_get_contents для чтения URL-адреса из источника страницы в переменную.
$link = "www.example.com";
$linkcontents = file_get_contents($link);
Затем я использую php-функцию strpos для поиска на странице искомой строки:
$needle = "<div>find me</div>";
if (strpos($linkcontents, $needle) == false) {
echo "String not found";
} else {
echo "String found";
}
Я слышал, что команда cURL хороша для обработки вещей, связанных с URL-адресами, я просто не уверен, как бы я использовал ее для выполнения того, что я делаю с объединенными функциями file_get_contents и strpos, как я описал выше.
Или, если есть другой способ сделать это, я весь внимание 🙂
Комментарии:
1. Смотрите: Библиотека / фреймворк PHPCrawl webcrawler
Ответ №1:
Ну, мы создаем функцию CURL следующим образом
function Visit($irc_server){
// Open the connection
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$port = '80';
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $irc_server);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_PORT, $port);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if ($curl_errno > 0) {
$return = ("cURL Error ($curl_errno): $curl_errorn");
} else {
$return = $data;
}
curl_close($ch);
/*if($httpcode >= 200 amp;amp; $httpcode < 300){
$return = 'OK';
}else{
$return ='Nok';
}*/
return $return;
}
Еще одна функция для обработки нашего URL
function tenta($url){
// Now, create a instance of your class, define the behaviour
// of the crawler (see class-reference for more options and details)
// and start the crawling-process.
$crawler = new MyCrawler();
// URL to crawl
$crawler->setURL($url);
// Only receive content of files with content-type "text/html"
$crawler->addContentTypeReceiveRule("#text/html#");
// Ignore links to pictures, dont even request pictures
$crawler->addURLFilterRule("#.(jpg|jpeg|gif|png)$# i");
// Store and send cookie-data like a browser does
$crawler->enableCookieHandling(true);
// Set the traffic-limit to 1 MB (in bytes,
// for testing we dont want to "suck" the whole site)
$crawler->setTrafficLimit(1000 * 1024);
// Thats enough, now here we go
$crawler->go();
// At the end, after the process is finished, we print a short
// report (see method getProcessReport() for more information)
$report = $crawler->getProcessReport();
if (PHP_SAPI == "cli") $lb = "n";
else $lb = "<br />";
/*
echo "Summary:".$lb;
echo "Links followed: ".$report->links_followed.$lb;
echo "Documents received: ".$report->files_received.$lb;
echo "Bytes received: ".$report->bytes_received." bytes".$lb;
echo "Process runtime: ".$report->process_runtime." sec".$lb; */
}
Мы создаем наш класс
// It may take a whils to crawl a site ...
set_time_limit(110000);
// Inculde the phpcrawl-mainclass
include("libs/PHPCrawler.class.php");
// Extend the class and override the handleDocumentInfo()-method
class MyCrawler extends PHPCrawler
{
function handleDocumentInfo($DocInfo)
{
global $find;
// Just detect linebreak for output ("n" in CLI-mode, otherwise "<br>").
if (PHP_SAPI == "cli") $lb = "n";
else $lb = "<br />";
// Print the URL and the HTTP-status-Code
echo "Page requested: ".$DocInfo->url." (".$DocInfo->http_status_code.")".$lb;
//echo $img_url = '<img src="'.$DocInfo->url.'.jpg" width = "150" height = "150" />'.$lb;
//we looking for kenya on this domain
foreach ($find as $matche) {
$matchb = implode(',',$matche);
//$matchb = $matche['word'];
if(preg_match("/(".$matchb.")/i", Visit($DocInfo->url))) {
echo "<a href=".$DocInfo->url." target=_blank>".$DocInfo->url."</a><b style='color:red;'>".$matche['word']."</b>".$lb;
}
}
// Print the refering URL
echo "Referer-page: ".$DocInfo->referer_url.$lb;
// Print if the content of the document was be recieved or not
if ($DocInfo->received == true)
echo "Content received: ".$DocInfo->bytes_received." bytes".$lb;
else
echo "Content not received".$lb;
// Now you should do something with the content of the actual
// received page or file ($DocInfo->source), we skip it in this example
echo $lb;
flush();
}
}
Наши переменные в массиве
URL-адреса, которые мы будем сканировать.
$url = array(
array("id"=>7, "name"=>"soltechit","url" => "soltechit.co.uk"),
array("id"=>5, "name"=>"CNN","url" => "cnn.com", "description" => "A social utility that connects people, to keep up with friends, upload photos, share links")
);
strings we are looking for
$find = array(
array("word" => "routers"),
array("word" => "Moose"),
array("word" => "worm"),
array("word" => "kenya"),
array("word" => "alshabaab"),
array("word" => "ISIS"),
array("word" => "security"),
array("word" => "windows 10 release"),
array("word" => "hacked")
);
Которую мы вызываем следующим образом
foreach ($url as $urls) {
$url = $urls['url'];
echo '<h2>'.$urls['name'].'</h2>';
echo $urls['description'].'<br>';
echo tenta($url).'<br>';
}
Ответ №2:
Если file_get_contents
все отлично работает для текущей задачи, зачем что-то менять …? Я говорю, продолжайте использовать это.
Обратите внимание, что вам нужно будет передать ему URL, начинающийся с «http: //», в противном случае он попытается открыть локальный файл с именем «www.example.com «.
Также это хорошая практика делать === false
с strpos
, поскольку в противном случае совпадение в позиции 0 не будет распознано (поскольку 0 == false
но не 0 === false
)
Комментарии:
1. Хорошая мысль, Матти. Спасибо. Причина, по которой я хочу альтернативу, заключается в том, что я чувствую, что то, как я сейчас это делаю, может считаться навязчивым методом выполнения того, что я хочу сделать.
2. @Charles: Что вы подразумеваете под «навязчивым»?
3. @Matti Virkkunen Что ж, если мне придется перебирать кучу URL-адресов (скажем, 100), используя этот метод, веб-сервер или брандмауэр могут подумать, что я пытаюсь спамить / атаковать его, и я хочу предотвратить это.
4. @Charles: В таком случае, если вы собираетесь выполнять множество запросов к одному и тому же веб-серверу одновременно, может помочь использование cURL, поскольку он поддерживает постоянные соединения. Однако вы все равно захотите реализовать какой-либо ограничитель запросов, если рассматриваемому серверу не нравится получать запросы слишком часто.
5. @Matt Virkkunen Ya Я согласен, cURL кажется правильным решением, просто ищу пример того, как это работает так, как мне нужно, чтобы это работало.
Ответ №3:
Здесь есть кое-что получше, я думаю, это помогло бы, что выглядит так:
<?php
// It may take a whils to crawl a site ...
set_time_limit(10000);
// Inculde the phpcrawl-mainclass
include("libs/PHPCrawler.class.php");
// Extend the class and override the handleDocumentInfo()-method
class MyCrawler extends PHPCrawler
{
function handleDocumentInfo($DocInfo)
{
// Just detect linebreak for output ("n" in CLI-mode, otherwise "<br>").
if (PHP_SAPI == "cli") $lb = "n";
else $lb = "<br />";
// Print the URL and the HTTP-status-Code
echo "Page requested: ".$DocInfo->url." (".$DocInfo->http_status_code.")".$lb;
// Print the refering URL
echo "Referer-page: ".$DocInfo->referer_url.$lb;
// Print if the content of the document was be recieved or not
if ($DocInfo->received == true)
echo "Content received: ".$DocInfo->bytes_received." bytes".$lb;
else
echo "Content not received".$lb;
// Now you should do something with the content of the actual
// received page or file ($DocInfo->source), we skip it in this example
echo $lb;
flush();
}
}
// Now, create a instance of your class, define the behaviour
// of the crawler (see class-reference for more options and details)
// and start the crawling-process.
$crawler = new MyCrawler();
// URL to crawl
$crawler->setURL("www.php.net");
// Only receive content of files with content-type "text/html"
$crawler->addContentTypeReceiveRule("#text/html#");
// Ignore links to pictures, dont even request pictures
$crawler->addURLFilterRule("#.(jpg|jpeg|gif|png)$# i");
// Store and send cookie-data like a browser does
$crawler->enableCookieHandling(true);
// Set the traffic-limit to 1 MB (in bytes,
// for testing we dont want to "suck" the whole site)
$crawler->setTrafficLimit(1000 * 1024);
// Thats enough, now here we go
$crawler->go();
// At the end, after the process is finished, we print a short
// report (see method getProcessReport() for more information)
$report = $crawler->getProcessReport();
if (PHP_SAPI == "cli") $lb = "n";
else $lb = "<br />";
echo "Summary:".$lb;
echo "Links followed: ".$report->links_followed.$lb;
echo "Documents received: ".$report->files_received.$lb;
echo "Bytes received: ".$report->bytes_received." bytes".$lb;
echo "Process runtime: ".$report->process_runtime." sec".$lb;
?>
Комментарии:
1. Это квалифицируется как ответ «только для ссылок» — не могли бы вы включить фрагмент кода, решающий проблему, даже если вы просто копируете его со страницы, на которую вы ссылаетесь?