Bing 搜索的基本身份验证 API

Basic Authentication of Bing Search API

我读过这个post:Bing search API and Azure

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

<?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 格式的数据,有什么建议吗?非常感谢您的帮助!

如果我理解正确的话,你正试图为你的呼叫做一些代理之类的东西..但是你没有正确设置你的 HTTP header 字段,就像你的 php 文件从 api.datamarket.azure.com,所以你需要设置那些请看这个answer and set HTTP headers properly and also make your ajax call as mentioned here