用于 PHP 中 Bing 搜索的 Windows Azure 身份验证

2022-01-11 00:00:00 http azure authentication header php

我正在尝试使用 Windows Azure Marketplace API 执行必应搜索,我已经下载了他们的指南和示例代码.该代码准备了一个带有基本身份验证的 HTTPS 请求,但是我不断收到以下错误:

I am trying to perform a Bing Search by using the Windows Azure Marketplace API, I have downloaded their guide and sample code. The code prepares a HTTPS request with basic authentication, however I am constantly getting the following error:

警告:file_get_contents(https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query=%27washburn%27&Adult=%27Off%27&$top=50&$format=Atom): 无法打开流:连接被拒绝

php 代码(来自微软的文档):

The php code (from Microsoft's document):

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

请问有谁知道是什么原因造成的?我已正确设置 $accountKey 并在浏览器中对其进行了测试.让我有点困惑的是 127.0.0.1:8888base64_encode($accountKey.":".$accountKey) ,你怎么需要 $accountKey 之前和之后: 使用浏览器时,您应该将用户名留空,只需在密码字段中输入帐户密钥?

Does anyone know what is causing the error please? I have correctly set the $accountKey and I tested it in a browser. What puzzles me a little is 127.0.0.1:8888 and also base64_encode($accountKey.":".$accountKey) , how come you need the $accountKey both before and after the : while when using a browser you are supposed to leave the username blank and just input the account key into the password field?

推荐答案

我已经解决了,下面是我为将来会做同样事情的人发现的:

I have solved it and here is what I have found for future people that would be doing the same thing:

我注释掉了上面写着的那一行:

I commented out the line where it says:

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

并设置 base64_encode("ignored:".$accountKey) 代替

根据我在 MSDN 上阅读的内容,据说用户名部分被忽略,因此它的值应该是什么并不重要.我在想可能是密钥中的长度或特殊字符搞砸了,所以我用 ignored (或其他任何东西)替换它.

Base on what I read on MSDN, the username part is said to be ignored, so it shouldn't matter what value it is. I was thinking perhaps the length or the special characters in the key screwed things up so I replaces it with ignored (or anything really).

成功了,我可以解析返回的 JSON 数据.祝你好运!

That did the trick and I can parse the returned JSON data. Good luck!

相关文章