doufengsui7449
2016-09-16 00:07
浏览 49
已采纳

Azure表存储休息API

How would one make a request with PHP?

Also how would one do the authorization and date requirement?

Everything on the internet as an example is outdated or using visualstudio to make the request or format it. I'm looking for pure PHP or a example for me to get started.

Please don't link something from like 2013 cause it won't work

I already know there is a PHP for Azure Table Storage, but I want to use the API as I will only be querying data not inserting or updating

This is my request right now

 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

and i get this response

    <?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>

图片转代码服务由CSDN问答提供 功能建议

如何用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 
   
 
 

我得到 此响应

 &lt;?xml version =“1.0”encoding =“utf-8”standalone =“yes”?&gt; 
&lt; error xmlns =“http:/  /schemas.microsoft.com/ado/2007/08/dataservices/metadata">
&lt; code&gt; AuthenticationFailed&lt; / code&gt; 
&lt; message xml:lang =“en-US”&gt;服务器无法验证 请求。 确保正确形成Authorization标头的值,包括签名。
RequestId:bf8eea79-0002-0107-15b1-0fad02000000 
时间:2016-09-16T00:31:13.7387356Z&lt; / message&gt; 
&lt; / error&gt; \  n   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtkyayvldeaqhl7151 2016-09-16 02:52
    已采纳

    @Gaurav is correct, you need to generate the signature and set in the Authorization header in the format Authorization:SharedKey {storage_account}:{signature} for the Rest request. You can refer to https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx?f=255&MSPPError=-2147217396#Constructing_Element for more details.

    And Here is a code snippet in PHP for your information:

    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' . "
    " . 
                ''. "
    " . //Content-MD5
                '' . "
    " . //Content-Type
                $date. "
    " .
               '/'.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);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题