繁体   English   中英

Bing搜索API和Azure用于关键字搜索

[英]Bing search API and Azure for keyword searching

我正在使用此代码,但它给出了错误。 消息为“不良响应”的异常“异常”:

https://api.datamarket.azure.com/Bing/SearchWeb/Web?$ format = json&Query =%27LNCT + Group + of + Colleges +%3A%3A + Largest + Education + Group + in + Central + India%27在C:\\ xampp \\ htdocs \\ bing \\ BingSearch.php:114中

堆栈跟踪:#0 C:\\ xampp \\ htdocs \\ bing \\ BingSearch.php(88):BingSearch-> getJSON(' https://api.dat ...',Array)

#1 C:\\ xampp \\ htdocs \\ bing \\ BingSearch.php(40):BingSearch-> query('Web','LNCT Group of C ...')

#2 C:\\ xampp \\ htdocs \\ bing \\ example.php(19):BingSearch-> queryWeb('LNCT Group of C ...')

#3 {main}

一个文件是Example.php

/*
 * sample example code for BingSearch.php class
 * @author Daniel Boorn info@rapiddigitalllc.com
 * @license apache 2.0
 * @bingapiurl https://datamarket.azure.com/dataset/bing/search#schema
 */


ini_set('display_errors','1');
require('BingSearch.php');

//register for key on windows azure

$apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$bing = new BingSearch($apiKey);

$r = $bing->queryWeb('LNCT Group of Colleges :: Largest Education Group in Central India');
var_dump($r);
?>
Another file is BingSearch.php

<?php 

    class BingSearch{

    protected $apiKey = '';
    protected $apiRoot = 'https://api.datamarket.azure.com/Bing/SearchWeb/';

    public function BingSearch($apiKey=false){
        if($apiKey) $this->apiKey = $apiKey;
        if($this->apiKey=="") throw new Exception("API Key Required");
    }

    public function queryImage($query){
        return $this->query('Image',$query);
    }


    public function queryWeb($query){
        return $this->query('Web',$query);
    }


    public function queryVideo($query){
        return $this->query('Video',$query);
    }


    public function queryNews($query){
        return $this->query('News',$query);
    }

    public function queryRelatedSearch($query){
        return $this->query('RelatedSearch',$query);
    }


    public function querySpellingSuggestions($query){
        return $this->query('SpellingSuggestions',$query);
    }


    public function query($type,$query){
        if(!is_array($query)) $query = array('Query'=>"'{$query}'");
        try{
            return self::getJSON("{$this->apiRoot}{$type}",$query);
        }catch(Exception $e){
            die("<pre>{$e}</pre>");
        }

    }


    protected function getJSON($url,$data){
        if(!is_array($data)) throw new Exception("Query Data Not Valid. Type Array Required");
        //$data['$format'] = 'json';
        $url .= '?$format=json&' . http_build_query($data) ;
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD,  "$this->apiKey:$this->apiKey");
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $r = curl_exec($ch);
        $json = json_decode($r);

        if($json==null) throw new Exception("Bad Response: {$r}\n\n{$url}");
        return $json;
    }


}

作为curl需要针对https端点的证书验证请求。 要请求Bing Search API,我们只需通过验证即可,并且仍然可以正常运行。

将函数getJSON()修改为:

    if(!is_array($data)) throw new Exception("Query Data Not Valid. Type Array Required");
    //$data['$format'] = 'json';
    $url .= '?$format=json&' . http_build_query($data) ;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD,  "$this->apiKey:$this->apiKey");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $r = curl_exec($ch);
    $json = json_decode($r);

    if($json==null) throw new Exception("Bad Response: {$r}\n\n{$url}");
    return $json;

它在我这边工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM