Hello Experts I am a beginner in PHP. Need a lil practical help. Say I have a form in a page with a text field. What I need is to get the number of results of that the term entered in the form from Google. How do I send that "tem" entered in the text field to search in GOogle and how do I pull back the number of results from the SERP page. A little demonstration and example will be vey helpful. Please try to give one if you can make time for that. Thanks BUBAIPAL
I guess it would be better if you can show us how far have you gone. Do you know how to get input from an HTML form at least? On the other hand, you have to think about Google API for querying instead of creating a robot who parses the standard user output. Google will probably ban your site's IP if you do that.
$query = $_POST['forminput']; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.google.com/search?&q=$query"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_close($ch); preg_match("/pattern that has the results/", $output, $var); PHP: This is as far as I've gotten. I'm equally new, and I have been trying to figure this out. I'm stuck on the pattern matching part.
OK so after trying to figure this out I have come up with th efollowing solution. It is probably a really dumb way of doing this but here goes <?php $query = $_POST['forminput']; $url = "http://www.google.com/search?q=$query"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); preg_match("/(Results <b>1)(<\/b>)( - <b>10)(<\/b>)( of about <b>)(.*)(<\/b>)( for)/", $output, $var); echo $var[6]; ?> PHP:
Shit, i forgot, google needs plus signs not spaces, so my previous example only works with one word keywords. This will work. <?php $query = $_POST['query']; $words = explode(" ", $query); $i=0; $a=""; $found=""; reset ($words) ; foreach($words as $line){ $a .= $words[$i]."+"; $i++; } $url = 'http://www.google.com/search?q=$i'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); preg_match("/(Results <b>1)(<\/b>)( - <b>10)(<\/b>)( of about <b>)(.*)(<\/b>)( for)/", $output, $var); echo $var[6]; ?> PHP: