search engine result page using curl php

Configuration script which will have domain, function for url validate and check the keywords.

[sourcecode language=”php”]
<?php
define("domain","http://www.google.com");

function getValidateLink($url) {
if(!filter_var($url, FILTER_VALIDATE_URL,FILTER_FLAG_QUERY_REQUIRED)) {
echo domain.$url;
}
}

$strKeywords = '';
if(isset($_GET["q"])) {
$strKeywords = (isset($_GET["q"])) ? $_GET["q"] : "codeasearch.com";
}
?>
[/sourcecode]

Now html form with one textbox and submit button.

[sourcecode language=”html”]
<form action="index.php" method="get">
Enter the keyword : <input type="text" name="q" value="<?php echo $strKeywords;?>" />
<input type="submit" name="btnSubmit" value="Go!" />
</form>
[/sourcecode]

Final script which will send the keywords to check from google and display the response to our end using curl.

[sourcecode language=”php”]
<?php
if(isset($_GET["q"])) {
$strUrl = "http://www.google.com/search?q=$strKeywords";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $strUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
$data = curl_exec($ch);
curl_close($ch);

$regex = '#<a( [a-zA-Z_\:][a-zA-Z0-9_\:\.-]*\="[^"]*")* href\="([^"]*)"
( [a-zA-Z_\:][a-zA-Z0-9_\:\.-]*\="[^"]*")*>(.*?)</a>#';
preg_match_all($regex, $data, $links);

for($i=0;$i<count($links[2]);$i++) {
echo "<li>".$links[4][$i]."<br />".getValidateLink($links[2][$i])."</li>";
}
}
?>
[/sourcecode]


[ Download ] | [ Demo ]
Share