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

2023-11-30php开发问题
2

本文介绍了用于 PHP 中 Bing 搜索的 Windows Azure 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用 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!

这篇关于用于 PHP 中 Bing 搜索的 Windows Azure 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17