gpt4 book ai didi

php - Azure 表存储 Rest API

转载 作者:行者123 更新时间:2023-12-03 03:10:11 25 4
gpt4 key购买 nike

如何使用 PHP 发出请求?

另外,如何满足授权和日期要求?

作为示例,互联网上的所有内容都已过时或使用 Visualstudio 发出请求或对其进行格式化。我正在寻找纯 PHP 或一个可供我入门的示例。

请不要链接 2013 年之类的内容,因为它不起作用

我已经知道有一个用于 Azure 表存储的 PHP,但我想使用该 API,因为我只会查询数据,而不是插入或更新

这是我现在的请求

 GET /Data2()?filter=Username%20eq%20'{username}' HTTP/1.1
Host: {tablename}.table.core.windows.net
Authorization: SharedKey {accountname}:{accountkey}
x-ms-date: 2016-09-16T00:31:08Z
Content-Type: application/json;odata=nometadata
Cache-Control: no-cache

我得到了这个回复

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<code>AuthenticationFailed</code>
<message xml:lang="en-US">Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:bf8eea79-0002-0107-15b1-0fad02000000
Time:2016-09-16T00:31:13.7387356Z</message>
</error>

最佳答案

@Gaurav 是正确的,您需要生成签名并以 Authorization:SharedKey {storage_account}:{signature} 格式在 Authorization header 中设置其余部分要求。您可以引用https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx?f=255&MSPPError=-2147217396#Constructing_Element更多细节。

这里有一个 PHP 代码片段供您引用:

const AZURE_ACC_NAME = {storage_account};
const AZURE_PRIMARY_KEY = {storage_key};
const AZURE_TABLE = {table_name};

$date = gmdate('D, d M Y H:i:s T',time());
$StringToSign = 'GET' . "\n" .
''. "\n" . //Content-MD5
'' . "\n" . //Content-Type
$date. "\n" .
'/'.AZURE_ACC_NAME.'/'.AZURE_TABLE.'()';
echo $StringToSign;
$sig = base64_encode(
hash_hmac('sha256', urldecode($StringToSign), base64_decode(AZURE_PRIMARY_KEY), true)
);

$endpoint = 'https://'.AZURE_ACC_NAME.'.table.core.windows.net';
$url = $endpoint.'/'.AZURE_TABLE.'()';

$headers = [
"x-ms-date:{$date}",
'x-ms-version:2014-02-14',
'Accept:application/json;odata=nometadata',
"Authorization:SharedKey ".AZURE_ACC_NAME.":{$sig}"
];
var_dump($headers);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
var_dump($response);
echo curl_error($ch);
curl_close($ch);

关于php - Azure 表存储 Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39522061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com