Last active
January 26, 2016 20:29
-
-
Save qstudio/fb96c2ca76dfad41344d to your computer and use it in GitHub Desktop.
Query Google using wp_remote_post() function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Run a Google query | |
* | |
* @since 0.1 | |
* @return void | |
*/ | |
public static function google_query( $query = null ){ | |
if ( is_null( $query ) ) { | |
return false; | |
} | |
// urlencode query ## | |
$query = urlencode( $query ); | |
// user IP ## | |
$userip = self::get_ip(); | |
// build query ## | |
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={$query}&userip={$userip}"; | |
// fire it off ## | |
$response = wp_remote_post( $url, | |
array( | |
'method' => 'GET', | |
'timeout' => 45, | |
'redirection' => 5, | |
'httpversion' => '1.0', | |
'blocking' => true, | |
'headers' => array(), | |
'body' => array(), | |
'cookies' => array() | |
) | |
); | |
if ( is_wp_error( $response ) ) { | |
$error_message = $response->get_error_message(); | |
return | |
sprintf( | |
__( "Google query failed: %s", self::text_domain ) | |
, $error_message | |
); | |
} | |
// now, process the JSON string | |
$json = json_decode( $response["body"] ); | |
// test ## | |
#wp_die( self::pr( $json ) ); | |
// kick it back ## | |
return $json; | |
} | |
/** | |
* Get current IP address | |
* | |
* @since 0.1 | |
* @return String Current IP Address | |
*/ | |
public static function get_ip() | |
{ | |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { | |
return $_SERVER['HTTP_CLIENT_IP']; | |
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
return $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} else { | |
return $_SERVER['REMOTE_ADDR']; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment