Azure Table 存储休息 API

Azure Table Storage Rest API

如何向 PHP 提出请求?

另外,如何进行授权和日期要求?

例如,互联网上的所有内容都已过时或使用 visualstudio 提出请求或格式化。我正在寻找纯粹的 PHP 或一个让我开始的例子。

请不要link 2013 年的东西,因为它不起作用

我已经知道有一个 PHP 用于 Azure Table 存储,但我想使用 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 header 中以格式 Authorization:SharedKey {storage_account}:{signature} 设置 Rest 请求。您可以参考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);