Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.