2016-04-03 171 views
1

我读过这篇文章的基本身份验证:我Bing search API and AzureBing搜索API

并用下面的代码来模仿它:

<?php    
if (isset($_GET['bingquery'])){ 
    // Replace this value with your account key 
    $accountKey = '***myaccountkey***'; 

    $WebSearchURL = 'https://api.datamarket.azure.com/Bing/Search/v1/' + 'News?$format=json&Query='; 

    $cred = sprintf('Authorization: Basic %s', base64_encode($accountKey . ":" . $accountKey)); 

    $context = stream_context_create(array(
     'http' => array(
      'header' => $cred 
     ) 
    )); 

    $request = $WebSearchURL . urlencode('\'' . $_GET["bingquery"] . '\''); 

    $response = file_get_contents($request, 0, $context); 

    echo $response; 

} 
?> 

我的AJAX调用是:

var bingquery = "bingquery=" + $('#query').val(); 

    $.ajax({ 
     url: "bingsearch.php", 
     method: "get", 
     dataType: "json", 
     data: bingquery, 
     success: function(jsondata){ 
      console.log(jsondata); 
     } 
     }); 

但是,我仍然无法从Bing搜索获取JSON格式的数据,有什么建议吗?非常感谢你的帮助!

回答

1

如果我理解正确,你正在尝试做一些像你的电话代理......但你没有正确设置你的HTTP头字段,就像你的PHP文件从api.datamarket.azure.com收到,所以你需要设置那些请看到这个answer并正确设置HTTP标头,并让你的AJAX调用如上所述here