php - 新的 Bing API PHP 示例不起作用

标签 php rest bing

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

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% 确定我的 key 是正确的,当我在浏览器 url 中输入它时它工作正常,即

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

(你需要把API key 作为你的密码,用户名可以是任何东西)

<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>

我已经尝试过 google API 和 Yahoo 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>

关于php - 新的 Bing API PHP 示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11451178/

相关文章:

php - 如何在 PHP 单元测试中处理对未定义函数的调用

php - 基本 : CakePHP data not being saved nor validation working?

java - 私有(private) REST API 成熟度级别和额外的复杂性层

javascript - 如何使用图像搜索 api [BING]

css - 谷歌和/或必应爬虫是否惩罚隐藏的推特 Bootstrap 标签内的内容

php - 在查询中不使用空格和短表别名有什么好处吗?

php - 你如何创建像 pinterest 这样的搜索栏?

java - JAVA中的Microsoft Office 365邮件REST API

JavaEE6+REST : How do I get all REST resources at runtime?

bing - Bing 运行在什么平台和软件堆栈上?