Php Bot Making

Inter robots that automatically access, evaluate, use, store or publish many addresses and content on the Internet for the purpose for which they are programmed are called bots. Making a bot in PHP is very simple and fun. Nowadays, bots are mostly used to get content from other sites and have many useful and wonderful functions. For example, you can extract weather information from a meteorology site or exchange rates from stock exchange sites and publish them on your own site in the most up-to-date way with the help of a PHP bot.

< p>Accessing site contents with PHP

To do this, we simply have the file_get_contents() function. It allows us to enter the address in the first parameter we will write into and load the source into a variable or print it to the screen.


$site = "https://www.mutluweb.net";$icerik = file_get_contents($site);echo $content;
When we ran this example, we entered the address //https://www.mutluweb.net, accessed the source of the site, and printed that source on the screen, thanks to the file_get_contents() function. Another way to access sites is curl functions. Thanks to Curl functions, we can not only enter an address, but also send form data to the entered address and log in as a user. In other words, thanks to curl in PHP, we can access sites as if we were a real user, browsing in a browser. You can send data to any address you want in the POST method with the curl function I prepared:

function curl($url, $post=< span class="constant language">false){
$user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; tr; rv:1.9.0.6) Gecko/2009011913 Firefox/ 3.0.6';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, $post ? true : false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post ? $post : false);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
You can use this function just like the file_get_contents function. If you are going to send post data, you will need to use the 2nd parameter as “user=musa&password=123456”.

echo curl("//www.mutluweb.net/login.php"< /span>, "user_login=user&user_pass=password");
In this example usage, we have posted our username and password to log in to the panel of our own site. When we type the correct information, we can access our panel via PHP thanks to the curl function.

Getting certain content with the PHP bot

After accessing the content of the sites directly, we first create a page to get the value and content in the section we want. We must determine the interval. We need to examine the source code of the site and find a distinctive range. I will provide a sample content, and we will take the part we want from this sample content. The source code of the section containing the results of the meteorology website for Istanbul is as follows:   

Sunday, June 20    21    30        59    93        18    11.4    32.2    16.7    25.5

It looks like a very complicated source code, but it is actually exactly the result we are looking for. If you notice, all the values ​​are among certain elements that can be distinguished. For example, date class=”backTrh”> We will use a ready-made function such as curl to get the range we want in a content. You can also use this function by copying and pasting.


function call($head, $end, $text){ @ preg_match_all('/' . preg_quote($bas, '/ ') . '(.*?)'. preg_quote($son, '/').'/i', $text, $m); return @$m[1];}
This intermediate function simply gives us the value within the range. There are 3 parameters for its use. The first parameter is the beginning of the range, the second parameter is the end of the range, and the third parameter is the content to be searched.$content = file_get_contents("//www.phpr.org");$title = search("", "", $content);echo $title [0];In this example, we accessed the source codes of our site, took the title of our site and printed it on the screen. The reason why we call the variable to which the value is loaded [0] is that we get the first captured range. If there are more similar ranges, it will add them as other elements of the array. For example, if there was more than one title range, the other results would be sorted into other series as [1], [2]. Let's apply this function on the meteorology site we mentioned:

$site = "http://www.meteor.gov.tr/tahmin/il-ve-ilceler.aspx?m=ISTANBUL ";
$content = file_get_contents($site);$alt_temperature = call('class="minS">', '', $content);
$upper_temperature = call('class="maxS">', '', $content);
echo 'Istanbul weather: ' . $upper_temperature[0] . '/' . $sub_temperature[0];
We found the range of values ​​for lower and upper temperatures on the site and took the range and printed it on the screen, thanks to our search function. As a result, we will get a printout on the screen as Istanbul weather: 21/30. In short, the logic of bot content retrieval is based on finding the distinctive ranges of the content on the sites and retrieving that part. Apart from these, you can produce many more logics by using your imagination and access all kinds of content and code the bots you want.