graham23s Posted March 21, 2010 Share Posted March 21, 2010 Hi Guys, Using this code i am able to grab the search results from a website: <?php if (isset($_GET['searchDeep'])) { print $searchString; // Deep search code $searchString = str_replace( " ","+",$searchString); $search_url = "http://www.site.com/mkplSearchResult.htm?dores=true&includeKeywords=$searchString&firstResult=50"; // make the cURL request to $search_url $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, 'Firefox (WindowsXP) - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6'); curl_setopt($ch, CURLOPT_URL,$search_url); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $html = curl_exec($ch); if (!$html) { echo "<p class=\"fcs-message-error\">cURL error:" . curl_error($ch) . " (Number " . curl_errno($ch).")</p>"; } curl_close($ch); // parse the html into a DOMDocument $dom = new DOMDocument(); @$dom->loadHTML($html); //$affid = get_option('ma_cb_affkey');if ($affid == '') { $affid = 'lun4tic' ;} $xpath = new DOMXPath($dom); $paras = $xpath->query("//div[@id='results']//tr/td[@class='details']/h4/a"); $para = $paras->item(0); $urlt = $para->textContent; if($urlt == '' | $urlt == null) { echo '<p class="fcs-message-error">No Deep search products found!</p>'; } else { $url = $para->getAttribute('href'); $url = str_replace("zzzzz", "graham25s", $url); $xpath = new DOMXPath($dom); $paras = $xpath->query("//div[@id='results']//td[@class='details']//div[@class='description']"); $para = $paras->item(0); $description = $para->textContent; $xpath = new DOMXPath($dom); $paras = $xpath->query("//div[@id='results']//td[@class='details']//h4/a"); $para = $paras->item(0); $title = $para->textContent; $link = '<a rel="nofollow" href="'.$url.'">'.$title.'</a>'; //print "<br/><strong>".$link."</strong><br/>".$description; print $link; } } ?> this brings me back 1 search result, i need the document to loop and bring all results back but i'm not sure how to go about it. any help would be appreaciated. thanks guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/196034-looping-domdocument/ Share on other sites More sharing options...
salathe Posted March 22, 2010 Share Posted March 22, 2010 The idea would be to get each of the items by targeting the appropriate td elements then loop over those (with a foreach) to delve inside and get the required information out of them. Quote Link to comment https://forums.phpfreaks.com/topic/196034-looping-domdocument/#findComment-1029949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.