Jump to content

Looping domDocument


graham23s

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/196034-looping-domdocument/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.