-2

I have been looking around on the internet for the ways to get dates of an event and then store that date into the database, but I was not able to find much.

I was able to get the dates from the website, but I don't know how to store it.

I want to get dates only from the website and then store it in the format of Y-m-d. Please if you know any way to do this, tell me.

Link: https://www.brent.gov.uk/events-and-whats-on-calendar/?eventCat=Wembley+Stadium+events

<?php

$curl = curl_init(); 
$all_data = array();

$url = "https://www.brent.gov.uk/events-and-whats-on-calendar/?eventCat=Wembley+Stadium+events";

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl);

$event = array();

preg_match_all('/<h3 style="margin:0px!important;">(.*?)<\/h3>/si',$result,$match);
$event['title'] = $match[1];

print_r($event['title']);

echo $all_data;
?>
Faraz Irfan
  • 1,306
  • 3
  • 10
  • 17
Pruthvi Diu
  • 51
  • 1
  • 10
  • there's no db stuff here. – Funk Forty Niner Mar 24 '18 at 20:54
  • Would recommend using a python script instead of PHP. [BeautifulSoup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/) can be easily used to write a quick and dirty scraper. Scrapers are supposed to be used not that frequently (once a day perhaps) so writing it in php is just too much effort. – udiboy1209 Mar 24 '18 at 21:01
  • @udiboy1209 What about people that know PHP and don't know Python? – rollstuhlfahrer Mar 24 '18 at 21:19
  • @FunkFortyNiner thats the problem i just don't know how to store that array data into a format that i can then save it into database – Pruthvi Diu Mar 24 '18 at 21:57
  • @udiboy1209 I really don't know python, but i am using the scraper to get dates of event which the original website adds everyday, and I don't know to scrape once daily – Pruthvi Diu Mar 24 '18 at 21:58
  • ignore @udiboy1209 , PHP is fully capable, and due to its ubiquitous pdo_mysql, DOMDocument, and libcurl extensions, is very easy to do in PHP as well. – hanshenrik Mar 25 '18 at 00:46

2 Answers2

2

don't use regex to parse html, use a proper HTML parser, for example DOMDocument.

a quick inspection of the site reveals that all the dates are in h3 children of the only article element on the page, you can use that to identify them. after extracting their dates, you can use strtotime() to convert it to an unix timestamp, then you can use date() to convert it to to the Y-m-d format, eg

$result = curl_exec($curl);
$domd=@DOMDocument::loadHTML($result);
$dateElements=$domd->getElementsByTagName("article")->item(0)->getElementsByTagName("h3");
foreach($dateElements as $ele){
    var_dump(date("Y-m-d",strtotime($ele->textContent)));
}

as for how to store the results in a mysql database, try writing php mysql tutorial -w3schools in google, or read the PDO section here: http://www.phptherightway.com/#pdo_extension

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • the code work for getting the dates but i get some error – Pruthvi Diu Mar 25 '18 at 13:58
  • E_DEPRECATED : type 8192 -- Non-static method DOMDocument::loadHTML() should not be called statically -- at line 14 string(10) "1969-12-31" string(10) "2018-03-27" string(10) "2018-04-08" string(10) "2018-04-14" string(10) "2018-04-21" string(10) "2018-04-22" string(10) "2018-04-30" string(10) "2018-05-05" string(10) "2018-05-12" string(10) "2018-05-13" string(10) "2018-05-19" string(10) "2018-05-20" – Pruthvi Diu Mar 25 '18 at 13:58
  • @PruthviDiu either you forgot the `@` when writing the code, or you have `xdebug.scream=1` in php.ini, most likely the former. – hanshenrik Mar 25 '18 at 14:28
  • getElementsByTagName("article")->item(0)->getElementsByTagName("h3"); foreach($dateElements as $ele){ $data= (date("Y-m-d" , strtotime($ele->textContent ))); echo $data; } ?> – Pruthvi Diu Mar 25 '18 at 15:04
  • this is the code i am using and i am trying this on phpfiddle.org – Pruthvi Diu Mar 25 '18 at 15:04
  • When i uploaded on the hosting server there is no error – Pruthvi Diu Mar 25 '18 at 16:07
  • @PruthviDiu that's a bug with phpfiddle.org, you should send them a bugreport. – hanshenrik Mar 25 '18 at 16:38
  • thanks you, and thanks for the help now my website is live after a long time. eventsatwembley.co.uk – Pruthvi Diu Mar 27 '18 at 10:31
0
        <?php

        $db_host = "localhost"; 
        $db_username = "username"; 
        $db_pass = "password"; 
        $db_name = "name"; 

        // Run the actual connection here 
        $con = mysqli_connect($db_host, $db_username, $db_pass, $db_name);
        if ($con->connect_errno) {
            die("Failed to connect to MySQL: (" . $con->connect_errno . ") " . $con->connect_error);
        }

        $curl = curl_init();

    //The Website you want to get data from
        $url = "https://www.brent.gov.uk/events-and-whats-on-calendar/?eventCat=Wembley+Stadium+events";

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($curl);
        libxml_use_internal_errors(true);

        $domd=@DOMDocument::loadHTML($result);

        //Getting the date from the site

        $dateElements=$domd->getElementsByTagName("article")->item(0)->getElementsByTagName("h3");
        foreach($dateElements as $ele){
            $data = (date("Y-m-d",strtotime($ele->textContent)));

        // echo "<br>".$data;

//checking if the date match with database date
           $sql = "SELECT * FROM event_table WHERE date = '$data'";
            $result = $con->query($sql);

        if ($result->num_rows > 0) {

            // output data of each row, if date match echo "Data is there";
            while($row = $result->fetch_assoc()) {
                  echo  "Data is there";
            }
        } 
//if date is not there then inster it into the database
        else {
           $results = mysqli_query($con, "INSERT INTO event_table (id, date) VALUES ('',' $data')");
            echo "data uploaded";
        }

        }
        ?>
Pruthvi Diu
  • 51
  • 1
  • 10