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

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>
  • 写回答

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)