I have researched this quite a bit over the last few days, and I have found all the answers online for the various functions, so thank you.
I now have 3 separate bits of code that all grab the contents of a webpage (the page would be an e-commerce product page, review page, something with a product on it) to get different information, but I am assuming this is very inefficient grabbing the contents 3 times!
The 3 bits of code do the 3 following things: 1) Get the webpage Title 2) Get all the images from a page 3) Find figures to get (what is hopefully) the price of the item on that page.
I would appreciate some help to group these together so it only has to get the file contents once. This is my current code: 1st Time:
function getDetails($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
//preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
//The above didnt work well enough (for getting Title when <title id=... > etc) so used the DOM below
preg_match("/(\£[0-9]+(\.[0-9]{2})?)/",$str,$price); //£ for GBP
$priceRes = preg_replace("/[^0-9,.]/", "", $price[0]);
//$pageDeatil[0]=$title;
$pageDeatil[1]=$priceRes;
return $pageDeatil;
}
}
$pageDeatil = getDetails("$newItem_URL");
//$itemTitle = $pageDeatil[0];
$itemPrice = $pageDeatil[1];
2nd Time:
$doc = new DOMDocument();
@$doc->loadHTMLFile("$newItem_URL");
$xpath = new DOMXPath($doc);
$itemTitle = $xpath->query('//title')->item(0)->nodeValue."
";
3rd Time:
include('../../code/simplehtmldom/simple_html_dom.php');
include('../../code/url_to_absolute/url_to_absolute.php');
$html = file_get_html($newItem_URL);
foreach($html->find('img') as $e){
$imgURL = url_to_absolute($url, $e->src);
//More code here
}
I cant seem to get the file once then use just that throughout the rest. Any help would be appreciated! Thanks in advance.