E-mail: info@arlotta.net

Utilizzare le API di Bing per generare contenuti aggiuntivi

Un webmaster che possiede un  blog  con buoni contenuti potrebbe permettersi il lusso di aggiungere più contenuti al proprio sito utilizzando le API di Bing. Il motore di ricerca mette a disposizione le proprie API ai programmatori.

Di seguito il codice da utilizzare con WordPress per estrarre risultati dalle API di Bing.

Per iniziare è necessario registrarsi alla seguente pagina ed ottenere la propria chiave personale: http://www.bing.com/developers/createapp.aspx

Ecco il codice per generare contenuti dalle API di Bing.

<?php
define(‘BING_API_KEY’, ”);
function pete_curl_get($url, $params){$post_params = array();
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(‘,’, $val);
$post_params[] = $key.’=’.urlencode($val);
}
$post_string = implode(‘&’, $post_params);
$fullurl = $url.”?”.$post_string;
$ch = curl_init();curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($ch, CURLOPT_URL, $fullurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040608’); //you can use another user agent, please see www.user-agents.org
$result = curl_exec($ch);curl_close($ch);
return $result;
}function perform_bing_web_search($termstring){$searchurl = ‘http://api.bing.net/json.aspx?’;
$searchurl .= ‘AppId=’.’E71EB199AA96DF4903388D858EDF6C2BC4609AE8′; //change the value with your Bing API
$searchurl .= ‘&Query=’.urlencode($termstring);
$searchurl .= ‘&Sources=Web’;
$searchurl .= ‘&Web.Count=5’; //search number
$searchurl .= ‘&Web.Offset=0’;
$searchurl .= ‘&Web.Options=DisableHostCollapsing+DisableQueryAlterations’;
$searchurl .= ‘&JsonType=raw’;
$response = pete_curl_get($searchurl, array());
$responseobject = json_decode($response, true);if ($responseobject[‘SearchResponse’][‘Web’][‘Total’]==0)return array();
$allresponseresults = $responseobject[‘SearchResponse’][‘Web’][‘Results’];
$result = array();
foreach ($allresponseresults as $responseresult){$result[] = array(‘url’ => $responseresult[‘Url’],’title’ => $responseresult[‘Title’],’abstract’ => $responseresult[‘Description’],);
}return $result;
}if (isset($_REQUEST[‘s’])) {
$termstring = urldecode($_REQUEST[‘s’]);
} else {
$termstring = ”;}
?>
And add the following code before <?php endif; ?>
<?php function CleanFileNameBan($result){
$bannedkey = array(“porn”, “sex”, “blablabla”); // insert your banned list keyword
$result = str_replace($bannedkey, ”,$result);
$result = trim($result);
return $result;
}
?>
<?php $termstring = $s ?>
<?php if ($s!=”) {
$bingresults = perform_bing_web_search($termstring);
//Kamu bisa ganti kode <h1> serta test yang ada sesuai dengan kode html dan text keinginan kamu begitu juga dengan yang lainnya
print ‘<h2>Hasil Pencarian ‘.$s.’ di situs lainnya:</h2>’;
foreach ($bingresults as $result) {
print ‘<div>’;
print ‘<h2>’.htmlspecialchars(CleanFileNameBan($result[‘title’])).'</h2>’;
print ‘<p>’.CleanFileNameBan(strip_tags($result[‘abstract’])).'</p>’;
print ‘<p style=”color:#777777″>’.$result[‘url’].'</p>’;
print ‘</div>’;
}
}
?>
Open theme function.php and add the following code:
/** search permalinks **/
function cws_nice_search_redirect() {
if ( is_search() && strpos( $_SERVER[‘REQUEST_URI’], ‘/wp-admin/’ ) === false && strpos( $_SERVER[‘REQUEST_URI’], ‘/search/’ ) === false ) {
wp_redirect( home_url( ‘/archieve/’ . str_replace( array( ‘ ‘, ‘%20’ ),  array( ‘+’, ‘+’ ), get_query_var( ‘s’ ) ) ) );
exit();
}
}
add_action( ‘template_redirect’, ‘cws_nice_search_redirect’ );

<?phpdefine(‘BING_API_KEY’, ”);function pete_curl_get($url, $params){$post_params = array();foreach ($params as $key => &$val) {if (is_array($val)) $val = implode(‘,’, $val);$post_params[] = $key.’=’.urlencode($val);}$post_string = implode(‘&’, $post_params);$fullurl = $url.”?”.$post_string;$ch = curl_init();curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($ch, CURLOPT_URL, $fullurl);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040608’); //you can use another user agent, please see www.user-agents.org$result = curl_exec($ch);curl_close($ch);return $result;}function perform_bing_web_search($termstring){$searchurl = ‘http://api.bing.net/json.aspx?’;$searchurl .= ‘AppId=’.’E71EB199AA96DF4903388D858EDF6C2BC4609AE8′; //change the value with your Bing API$searchurl .= ‘&Query=’.urlencode($termstring);$searchurl .= ‘&Sources=Web’;$searchurl .= ‘&Web.Count=5’; //search number$searchurl .= ‘&Web.Offset=0’;$searchurl .= ‘&Web.Options=DisableHostCollapsing+DisableQueryAlterations’;$searchurl .= ‘&JsonType=raw’;$response = pete_curl_get($searchurl, array());$responseobject = json_decode($response, true);if ($responseobject[‘SearchResponse’][‘Web’][‘Total’]==0)return array();$allresponseresults = $responseobject[‘SearchResponse’][‘Web’][‘Results’];$result = array();foreach ($allresponseresults as $responseresult){$result[] = array(‘url’ => $responseresult[‘Url’],’title’ => $responseresult[‘Title’],’abstract’ => $responseresult[‘Description’],);}return $result;}if (isset($_REQUEST[‘s’])) {$termstring = urldecode($_REQUEST[‘s’]);} else {$termstring = ”;}

?>
And add the following code before <?php endif; ?>
<?php function CleanFileNameBan($result){$bannedkey = array(“porn”, “sex”, “blablabla”); // insert your banned list keyword$result = str_replace($bannedkey, ”,$result);$result = trim($result);return $result;}?>
<?php $termstring = $s ?>

<?php if ($s!=”) {$bingresults = perform_bing_web_search($termstring);//Kamu bisa ganti kode <h1> serta test yang ada sesuai dengan kode html dan text keinginan kamu begitu juga dengan yang lainnyaprint ‘<h2>Hasil Pencarian ‘.$s.’ di situs lainnya:</h2>’;foreach ($bingresults as $result) {print ‘<div>’;print ‘<h2>’.htmlspecialchars(CleanFileNameBan($result[‘title’])).'</h2>’;print ‘<p>’.CleanFileNameBan(strip_tags($result[‘abstract’])).'</p>’;print ‘<p style=”color:#777777″>’.$result[‘url’].'</p>’;print ‘</div>’;}}?>
Open theme function.php and add the following code:
/** search permalinks **/function cws_nice_search_redirect() {if ( is_search() && strpos( $_SERVER[‘REQUEST_URI’], ‘/wp-admin/’ ) === false && strpos( $_SERVER[‘REQUEST_URI’], ‘/search/’ ) === false ) {wp_redirect( home_url( ‘/archieve/’ . str_replace( array( ‘ ‘, ‘%20’ ),  array( ‘+’, ‘+’ ), get_query_var( ‘s’ ) ) ) );exit();}}

add_action( ‘template_redirect’, ‘cws_nice_search_redirect’ );

Questo è tutto! Modificando due file abbiamo ottenuto quello che volevamo. Chi possiede un minimo di dimestichezza con WordPress non dovrebbe avere nessun problema.

Scrivi un commento

ARLOTTA.NET
info@arlotta.net
Website: http://www.arlotta.net
Email: info@arlotta.net

Ultimi articoli