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

报告相同问题?