Hi Im writing a simple class which I am going to use for some testing. My problem being it is only returning 4 results per search phrase. The output is:
foo
http://en.wikipedia.org/wiki/Foobar
bar
http://en.wikipedia.org/wiki/Bar_(unit)
my class is:
<?php
class true_seo {
public $string, $amount;
private $arr;
public function __construct(){}
public function set_g_key( $key ) {
$this->g_key = $key;
}
public function set_phrase( $string ){
if( is_string ( $string ) ) {
$string = array( $string );
}
if( is_array ( $string ) ) {
$this->phrases = $string;
}else{
Throw new exception("incorect input for phrase, string or array");
}
}
public function get_sites( $amount ) {
require_once "simple_html_dom.php";
$arr = array();
foreach( $this->phrases as $phrase ) {
$APIparams = array("key" => $this->g_key, "q" => $phrase, "start" => 0, "maxResults" => $amount, "filter" => true, "restrict" => "", "safeSearch" => false, "lr" => "lang_en", "ie" => "", "oe" => "");
if( $data = true_seo::google_search_api( $APIparams ) ) {
foreach( $data as $obj ) {
$arr[$phrase]['data'][] = $obj;
}
}else{
Throw new exception("Request error: no results returned from Google.");
}
}
$this->links = $arr;
}
public function google_search_api($args, $referer = 'http://localhost/seo/', $endpoint = 'web'){
$url = "http://ajax.googleapis.com/ajax/services/search/".$endpoint;
if ( !array_key_exists('v', $args) ) {
$args['v'] = '1.0';
}
$url .= '?'.http_build_query($args, '', '&');
if( $result = @file_get_contents($url) ) {
return json_decode($result);
}else{
Throw new exception("No data returned from url: $url");
}
}
}
?>
and called using:
<?php
try{
require "./classes/class_true_seo.php";
$seo = new true_seo();
$seo->set_g_key('ABQIAAAAsWzmZ4RXdIk0a-LqpqKCBRSW9rnkYkKNylryTgzW0Ct2FO9LJBSysazpU6aDIzBYBBRMiLQ9syYp0g');
$seo->set_phrase(array("foo","bar"));
$seo->get_sites(10);
foreach( $seo->links as $key => $phrase_return ){
echo "<h2>" . $key . "</h2>";
foreach( $phrase_return['data'][0]->results as $results ){
echo "<p>" . $results->url . "</p>";
}
}
}catch(Exception $err){
echo $err->getMessage();
}
?>
Any help is much appreciated