Azure Stack API Rest 在 PHP 中使用 CURL 创建存储帐户

Azure Stack API Rest create storage account with CURL in PHP

我尝试在 PHP 站点中使用 curl 在我的 Azure Stack 中创建一个存储帐户,但不幸的是我总是收到错误 "HTTP Error 411. The request must be chunked or have a content length."。

我输入了“"Content-Length: 0,"”或正确的数字,但仍然出现同样的错误。

这是我的代码(由 Postman 生成):

    $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://management.myazurestack.com/subscriptions/XXXXXX/resourcegroups/YYYYYYYY/providers/Microsoft.Storage/storageAccounts/sa01?api-version=2017-10-01",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYPEER => 0, 
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "PUT",
  CURLOPT_POSTFIELDS => "{\r\n                \"sku\": {\r\n                  \"name\": \"Standard_LRS\"\r\n                },\r\n                \"location\": \"myregion\"\r\n              }",
  CURLOPT_HTTPHEADER => array(
    "Accept: */*",
    "Accept-Encoding: gzip, deflate",
    "Authorization: ". $fBearer,
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Length: 0",
    "Content-Type: application/json",
    "Host: management.myazurestack.com",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

有什么想法吗?

谢谢

您必须使用 Content-Length 或分块,但不能同时使用。

你是这样使用的 Content-Length:

Content-Length: 1024

这就是你如何使用 Transfer-Encoding:

Transfer-Encoding: chunked

有一个 Azure SDK for PHP 让事情更容易管理。