2015-12-23 6 views
0

Я использую этот код, но он дает ошибку. исключения 'Exception' с сообщением «Bad Response:?Bing API поиска и Azure для поиска по ключевым словам

https://api.datamarket.azure.com/Bing/SearchWeb/Web $ формата = JSon & Query =% 27LNCT + Group + из + Colleges +% 3A% 3A + Самые большие + Образование + Group + в + Центральной + Индии% 27 'в C: \ XAMPP \ HTDOCS \ Бинг \ BingSearch.php: 114

Стек след: # 0 C: \ XAMPP \ HTDOCS \ Бинг \ BingSearch.php (88): BingSearch-> getJSON (' https://api.dat. .. 'массив)

# 1 C: \ XAMPP \ HTDOCS \ Бинг \ BingSearch.php (40): BingSearch-> запрос (' Web», 'LNCT Группа C ...')

# 2 C: \ XAMPP \ HTDOCS \ Бинг \ example.php (19): BingSearch-> queryWeb ('LNCT Группа C ...')

# 3 {главная}

один файл example.php

/* 
* sample example code for BingSearch.php class 
* @author Daniel Boorn [email protected] 
* @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; 
    } 


} 

ответ

0

Как локон проверка сертификата необходим запрос в отношении к 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; 

Он отлично работает на моей стороне.

Смежные вопросы