Azure Table Storage Rest API


Azure Table Storage Rest API

如何用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是正确的,您需要为Rest请求生成Authorization:SharedKey {storage_account}:{signature}格式的Authorization标头中的签名和设置。您可以参考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);