新的必应API PHP示例不起作用


New Bing API PHP example doesnt work

Microsoft自己的用于新Bing API的PHP示例不起作用。我尝试了很多方法,它只是显示:

服务器错误
401-未经授权:由于凭据无效,访问被拒绝
您没有查看此目录或页面的权限使用您提供的凭据。

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

'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, 0, $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方法,而不是不能将正确的身份验证信息从Linux客户端传递到BING服务的file_get_contents

<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, true);
                curl_setopt($ch, CURLOPT_AUTOREFERER, true);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                $rs = curl_exec($ch);
            echo($rs);
                curl_close($ch);
                return $rs;
        }
            ?>
        </form>
    </body>
</html>

我不得不添加

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

为了让它发挥作用,至少在我的本地副本(WAMP)中是这样。

希望它能帮上忙,我一整天都在搞这个。

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

这是问题的一部分

这不会给网址bing正在寻找

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

它将是

https://api.datamarket.azure.com/Bing/Search/Image?$format=json&Query=%27love+message%27 

而你想要一个网页而不是图像搜索,以及格式和其他参数shld在查询之后

"图像"应为"web"

我只是花了3天的时间试图让它发挥作用。

我刚刚发布了一个如何使用Unirest库连接到Bing/Azure API的示例:https://stackoverflow.com/a/20096151/257815