新的Bing API PHP示例不起作用

微软自己的新Bing API示例不起作用.我试过很多方面,它只是表明:

Server Error
401 – Unauthorized: Access is denied due to invalid credentials.
You do not have permission to view this directory or page
using the credentials that you supplied.

示例在官方文档中给出的编码如下,它分解为

'proxy' => 'tcp://127.0.0.1:8888',

我100%确定我的密钥是正确的,当我在浏览器URL中输入它时它工作正常,即

https://api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27love+message%27

(您需要将API密钥作为密码,用户名可以是任何东西)

<html>
    <head>
        <link href="styles.css" rel="stylesheet" type="text/css" />
        <title>PHP Bing</title>
    </head>
    <body>
        <form method="post" action="<?PHP echo $_SERVER['PHP_SELF'];?>">
            Type in a search:

            <input type="text" id="searchText" name="searchText"
                value="<?PHP
                        if (isset($_POST['searchText']))

                                   {
                            echo($_POST['searchText']);
                        }
                        else
                        {
                            echo('sushi');
                        }
                       ?>"
            />

            <input type="submit" value="Search!" name="submit" id="searchButton" />
            <?PHP
                if (isset($_POST['submit']))
                {
                    // Replace this value with your account key
                    $accountKey = 'BKqC2hIKr8foem2E1qiRvB5ttBQJK8objH8kZE/WJVs=';

                    $ServiceRootURL = 'https://api.datamarket.azure.com/Bing/Search/';

                    $WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query=';

                    $context = stream_context_create(array(
                        'http' => array(
                            //'proxy' => 'tcp://127.0.0.1:8888','request_fulluri' => true,'header' => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
                        )
                    ));

                    $request = $WebSearchURL . urlencode( '\'' . $_POST["searchText"] . '\'');

                    echo($request);

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

                    print_r($response);

                    $jsonobj = json_decode($response);

                    echo('<ul ID="resultList">');

                    foreach($jsonobj->d->results as $value)
                    {
                        echo('<li class="resultlistitem"><a href="' . $value->MediaURL . '">');

                        echo('<img src="' . $value->Thumbnail->MediaUrl. '"></li>');
                    }

                    echo("</ul>");
                }
            ?>
        </form>
    </body>
</html>

我已经尝试了谷歌API和雅虎API,这些都没有这么困难.

经过几天与微软技术支持的争论,他们认为它没有用

这是使用CURL的正确编码在BING API中执行此操作,应用CURL方法而不是file_get_contents,它无法将正确的身份验证信息从Linux客户端传递到BING服务.

<html>
    <head>
        <title>PHP Bing</title>
    </head>
    <body>
        <form method="post" action="<?PHP echo $_SERVER['PHP_SELF'];?>">
            Type in a search:

            <input type="text" id="searchText" name="searchText"
                value="<?PHP
                        if (isset($_POST['searchText']))

                                   {
                            echo($_POST['searchText']);
                        }
                        else
                        {
                            echo('sushi');
                        }
                       ?>"
            />

            <input type="submit" value="Search!" name="submit" id="searchButton" />
            <?PHP


                if (isset($_POST['submit']))
                {

            $credentials = "username:xxx";

                $url= "https://api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27{keyword}%27";        
                $url=str_replace('{keyword}',urlencode($_POST["searchText"]),$url);
                $ch = curl_init();

            $headers = array(
                    "Authorization: Basic " . base64_encode($credentials)
                );

                $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL,$url);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); 
                curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
                curl_setopt($ch,CURLOPT_FAILONERROR,true);
                curl_setopt($ch,CURLOPT_FOLLOWLOCATION,CURLOPT_AUTOREFERER,CURLOPT_TIMEOUT,10);
            curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);

                $rs = curl_exec($ch);
            echo($rs);
                curl_close($ch);
                return $rs;

        }

            ?>
        </form>
    </body>
</html>

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...