Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ritesh-patel/1d0a1b2e7c6ada0fa2c00a784c43262b to your computer and use it in GitHub Desktop.
Save Ritesh-patel/1d0a1b2e7c6ada0fa2c00a784c43262b to your computer and use it in GitHub Desktop.
<?php
/**
* Get names from google search
*
* @param array $args
* @param array $assoc_args
*/
public function get_names( $args = array(), $assoc_args = array() ) {
$names_json = '{
"1081": "errolmorris",
"1082": "harveyweinstein",
"1086": "mosessoyer",
"1088": "jerrysiegel",
"1089": "georgebushsr",
"1090": "jackvance",
"1094": "monicalewinksy",
"1097": "myrahindley",
"1098": "johnkricfalusi",
"1101": "courtneylove",
"1102": "richardwilliams",
"1109": "guyfawkes",
"1113": "billythekid",
"1115": "clydebarrow"
}';
$slug_names = json_decode( $names_json );
$result_names = array();
foreach( $slug_names as $slug_name ) {
//TODO get your API key and cx param, see https://developers.google.com/custom-search/json-api/v1/using_rest
$url = 'https://www.googleapis.com/customsearch/v1?key=INSERT_YOUR_API_KEY&cx=017576662512468239146:omuauf_lfve&q=' . $slug_name;
$request = wp_remote_get( $url );
$names = array();
if( 200 == wp_remote_retrieve_response_code( $request ) ) {
$body = wp_remote_retrieve_body( $request );
$res_data = json_decode( $body, true );
if( isset( $res_data['items'] ) && is_array( $res_data['items'] ) ) {
foreach( $res_data['items'] as $item ) {
if( isset( $item['pagemap'] ) && isset( $item['pagemap']['hcard'] ) && is_array( $item['pagemap']['hcard'] ) ) {
foreach( $item['pagemap']['hcard'] as $hcard ) {
if( isset( $hcard['fn'] ) ) {
$names[] = $hcard['fn'];
}
}
}
}
}
}
$result_names[$slug_name] = $names;
}
var_dump( json_encode( $result_names ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment