Laravel - Save HTML Dom-Parser response to database

Viewed 730

I have managed to get some HTML from a website using HTMLDomParser (http://simplehtmldom.sourceforge.net/):

$html = HtmlDomParser::file_get_html('https://example.com/id=1');

This results in the following HTML:

<p> ANDE 2, the Atmospheric Neutral Density Experiment 2, is a pair of microsatellites (Castor and Pollux) launched from Cape Canaveral on STS 127 on 15 July 2009 at 22:03 UT and deployed from the payload bay of the shuttle on 30 July 2009 at 17:22 UT.</p>
<p><strong>Launch Date:</strong> 2009-07-15<br/><strong>Launch Vehicle:</strong> Shuttle<br/><strong>Launch Site:</strong> Cape Canaveral, United States<br/></p>

What I want to attempt to do is to take this HTML and save it to a mySQL database. So, in the database I will have four columns:

  • Description
  • Launch Date
  • Launch Vehicle
  • Launch Site

The HTML layout will always stay the same. My other database contains the id's for the URL's. I am able to replace the id=1 with a variable, so I would be able to manage to save multiple webpage data.

The only thing I am worrying about if the id does not exist. Would it be possible just to skip that page if it cannot find the HTML it is looking for?

I have never done this before, so I am a bit overwhelmed by this. Would it even be possible to do this?

Basically, I just want to save data from a DomParser into my database.

EDIT: Since there has not been an answer, I think we need to tackle this systematically - first we should try creating the function that saves the data to the database.

EDIT 2: I was just thinking - maybe the function that needs to save the HTML can split the result from HTMLDomParser by tag ('strong', 'p', etc.) and create a variable for each column and then save it. I guess the question here first is how do you split the HTML?

EDIT 3: Since I did not receive any answers / suggestions, I am guessing it is very complex to implement this. I still have not managed to come up with anything that saves the HTML successfully to my database.

1 Answers

To check if the ID is valid to the site, I suggest a curl function, so if $url exists, try and get the data (try and catch), if there is an error with the data, catch it and return appropriate response or log it and move to next valid url.

You will have to format the data with a separate function inside the try.

Hope that helps as methodology goes, good luck. Peace

Related