Andy's Blog » bing API key, ajax 类, bing, Bing API KEY 申请, 如何申请bing api key, PHP调用bing 图片, php调用bing search api, php Bing api程序, curl bing appid, bing翻译php api网站源码, 如何使用bing API, bing api key申请 » PHP 使用 Bing API

PHP 使用 Bing API

首先你要去申请一个 Bing API KEY
申请地址: http://cn.bing.com/developers/

使用方法:

<?php
// Include the Bing API PHP Library
require 'library/BingAPI.php';
 
// Simply start the class with your AppID argumented
$search = new BingAPI('YOUR APPID KEY');
 
    
    
$search->query($searchWeb)
           ->
setSources('Web') # To use multiple resources simply do ->setSources('Web+Image') , it must match the source type bling.com provides
           ->
setFormat('xml')
           ->
setOptions(array(
                            
'Web.Count' => '20',
                            
'Web.Offset' => '0',
                            
'Adult' => 'Moderate',
                            
'Options' => 'EnableHighlighting'));
 
    
// Contains the search
    
$results = $search->getResults();
    
    
/* To use highlighting features use
    /* $search->setHighlightFormat(
                                "<span class='hl'>",
                                "</span>",
                                $SOURCE_TO_FORMAT
                            );
    */

 
    
/* If you don't want to highlight your title use
       $search->resetHighlight($string_containing_bing's title);
    */

 
    
/* For advanced cURL Users, setProxy() has been removed, you will utilize $search->setCurlArray(array()); */
 
/* And for developers who constantly lurks in bing forums or other developer forums there is $search->getRequestedQueryURL(); it will retrieve the link you use
   to query bing.com servers. eg.
http://api.search.live.net/xml.aspx?AppId=YOUR APPD ID&Query=da&Sources=Image&Image.Count=20&Image.Offset=0&Image.Filters=Size:Small&Adult=Moderate&Options=EnableHighlighting
   Note that it will return THE CURRENT URL.
*/

Bing API PHP Library

BingAPI.php
<?php
/**
* Bing API PHP Library
* Version: 1.0
* by David González (
http://routecafe.com/ )
*
* Copyright (c) 2009 David González
 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
 
*/

class BingAPI {
 
    
private $_appID; # Application ID
    
private $_queryTerms; # The input to search
 
    
# Optional queries
    
public $_version = "2.0";
    
public $_market = "en-us";
 
    
public $_sources; # The SourceType of the query
 
    
public $_options; # An array containing optional parameters
    
public $_format# What document will it be output into? XML, SOAP, jSON
    
public $_debugQuery;
    
    
private $_holder;
    
protected $_curlArray;
    
const BINGURL = "http://api.search.live.net/"; #URL of bing.com api
    
/**
     *
     *
@param <type> $appid Contains the application ID provided by bing.com
     */

    
public function __construct($appid) {
 
        
try {
 
            
$this->_appID = $appid;
 
            
if(empty($this->_appID)) { throw new Exception('Please insert your Application ID'); }
 
        
} catch(Exception $e) {
 
 
            
echo "An error has been detected: ".$e->getMessage();
            
die(); # Stop the execution
        
}
 
    
}
 
    
public function setSources($sources) {
        
try {
 
            
$this->_sources = $sources;
 
            
if(empty($this->_sources)) { throw new Exception('Please insert your the Source, you may only use one in these categories: Web, Image, Spell, InstantAnswer, MobileWeb, Translation, Phonebook, Video, Ad'); }
        
        
} catch(Exception $e) {
            
            
echo "An error has been detected: ".$e->getMessage();
            
die(); #Stop the execution
            
    
}
 
        
return $this;
    
}
    
/**
     * Set optional parameters, only accepts array
     *
@param array $options
     *
@return object
     *
     */

    
public function setOptions($options) {
 
        
$this->_options = (array) $options;
        
return $this;
        
    
}
    
/**
     * A format is required to get the output, there are three formats currently supported: soap, xml, json
     *
@param string $format
     *
@return object
     */

    
public function setFormat($format) {
        
switch($format) {
            
case 'xml':
                
$this->_format = 'xml.aspx?';
            
break;
            
case 'json':
                
$this->_format ='json.aspx?';
            
break;
            
case 'soap':
                
#$this->_format = 'search.wsdl?';
                
throw new Exception('SOAP Implementation not yet applied');
            
break;
            
default:
                
throw new Exception('Please select a format: xml, json');
            
break;
        
}
        
return $this;
    
}
    
public function setCurlArray($args) {
        
$this->_curlArray = $args;
    
}
    
/**
     * Deprecated, use curl array
     *
     *
@param string $addr
     *
@deprecated
     * @return object
     */

    
public function setProxy($addr) {
 
    
throw new Exception('Deprecated');
        
    
}
 
    
/**
     * The search term which you will query the server
     *
@param string $query
     *
@return object
     */

    
public function query($query) {
        
try {
 
            
$this->_queryTerms = $query;
            
if(empty($this->_queryTerms)) { throw new Exception('We can\'t search without an input'); }
            
        
} catch(Exception $e) {
            
echo "An error has been detected: ".$e->getMessage();
            
die();
        
}
 
        
return $this;
    
}
    
/**
     * This function detects and format the Highlight characters(U+E000 and U+E001 private use) bing.com provides
     *
@param string $start
     *
@param string $end
     *
@param string $sources
     *
@return string
     */

    
public function setHighlightFormat($start, $end, $sources) {
              
$HighlightPatterns = array(
                  
$start => "/(\xEE\x80\x80)/uix",
                  
$end => "/(\xEE\x80\x81)/uix");
 
        
$Formatted = preg_replace(array_values($HighlightPatterns), array_keys($HighlightPatterns), $sources);
        
return $Formatted;
    
}
    
/**
     * resetHighlight reverses the highlight characters, for example if you want
     * the title clear of highlight characters then this will make it possible
     * This also can be used on description if desired. or any text that contains U+E000 or U+E001
     *
     *
@param string $sources
     *
@return string
     */

    
public function resetHighlight($sources) {
        
$HighlightPatterns = array(
                
" " => "/(\xEE\x80\x80)/uix",
                
"" => "/(\xEE\x80\x81)/uix");
 
    
$Formatted = preg_replace(array_values($HighlightPatterns), array_keys($HighlightPatterns), $sources);
    
return trim($Formatted);
    
}
 
    
/**
     * Returns the whole query in a string and all its parameter, useful in debugging process
     * ex.
http://api.search.live.net/xml.aspx?AppId=APPID&Query=Gimp&Sources=Image
     *
@return string
     */

    
public function getRequestedQueryURL() {
        
return $this->_debugQuery;
    
}
 
    
public function getResults() {
 
        
try {
 
            
if(empty($this->_format)) { throw new Exception('Please select a format: xml or json'); }
 
        
} catch(Exception $e) {
 
            
echo "An error has been detected: ".$e->getMessage();
            
die();
 
        
}
 
        
$optionalParams = null;
        
        
if(is_array($this->_options)) {
            
            
foreach($this->_options as $query => $value) {
 
                
$optionalParams .= '&'.$query.'='.$value;
 
            
}
 
        
}
    
# Start curl
    
$queryServer = curl_init(self::BINGURL.$this->_format."AppId=".$this->_appID."&Query=".$this->_queryTerms."&Sources=".$this->_sources.$optionalParams);
    
$this->_debugQuery = self::BINGURL.$this->_format."AppId=".$this->_appID."&Query=".$this->_queryTerms."&Sources=".$this->_sources.$optionalParams;
    
curl_setopt($queryServer, CURLOPT_HEADER, 0);
    
curl_setopt($queryServer, CURLOPT_RETURNTRANSFER, 1);
 
    
if(is_array($this->_curlArray)) {
    
    
curl_setopt_array($queryServer, $this->_curlArray);
    
}
    
    
# Deliver
 
    
$this->_holder = curl_exec($queryServer);
    
return $this->_holder;
    
    
# Have a great day!
    
curl_close($queryServer);
 
    
}
 
}
?>

Incoming search terms:

Tags: API, Bing

本文地址: http://blog.21andy.com/20100923/1963.html

3 评论 to “PHP开源CMS之MODx”

  1. 企众网 于 2010-09-24 07:50:52 发表:

    Bing API 都能做些啥啊,我简单看了下他们的网站,好像也没有什么特别明显的功能特点相对于 Google Code API

  2. 拆墙部队 于 2010-09-24 15:09:14 发表:

    必应的 API 现在拿出来没用,无实际用途。

  3. arlinger 于 2012-08-02 17:00:47 发表:

    Hi, 楼主能再说的具体一些吗?我相信很多人看完后,有些云里雾里~