2016-09-16 95 views
0

如何使用PHP發出請求?Azure Table Storage Rest API

另外如何做授權和日期要求?

互聯網上的所有內容都是過時的,或者使用visualstudio進行請求或格式化。我正在尋找純PHP或一個例子讓我開始。

請不要從2013一樣的東西聯繫起來的原因,它不會工作

我已經知道有天青爲表存儲一個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> 
+0

對於初學者來說,你不能傳遞您的存儲賬戶在您的授權頭。您需要根據您的請求計算授權標頭值。請查看此處:https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx查看授權標頭的計算方式。 –

回答

1

@Gaurav是正確的,您需要生成簽名並以Authorization:SharedKey {storage_account}:{signature}的格式在Authorization標頭中設置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); 
+0

你是救命的人謝謝 –

+0

你知道我會怎樣做一個過濾器嗎?所以過濾一個ParititonKey或在我的情況下過濾一個屬性,所以找到用戶名eq'用戶' –

+0

沒關係得到它 –