用复杂的json进行php POST请求

时间:2015-10-30 19:25:59

标签: php json google-compute-engine

我正在尝试使用PHP创建GCP实例。要创建一个实例,我需要使用下面的json POST请求。

{
  "machineType": "zones/us-central1-f/machineTypes/f1-micro",
  "name": "testvm923",
  "disks": [
    {
      "type": "",
      "boot": true,
      "initializeParams": {
        "sourceImage": "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20150909a"
      }
    }
  ],
  "networkInterfaces": [
    {
      "network": "global/networks/default",
      "accessConfigs": [
        {

        }
      ]
    }
  ]
}

但我不知道该怎么办。我在下面试过但没有用。

$body=utf8_encode('{ "machineType": "zones/us-central1-f/machineTypes/f1-micro", "name": "'.$vm_name.'", "disks": [{"type": "", "boot": true, "initializeParams": { "sourceImage": "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20150909a" } } ], "networkInterfaces": [ { "network": "global/networks/default", "accessConfigs": [ { } ] } ] }');

$opts = json_decode($body);
// $opts = json_decode($body, true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$opts);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($ch);
if ($data === FALSE) {
    die("Curl failed: " . curL_error($ch));
}
curl_close ($ch);

我也尝试过以下但没有工作

$opts =  array (
     "machineType" => "zones/us-central1-f/machineTypes/f1-micro",
     "name" => "testvm923",
     "disks" => array (
                   1 => array (
                             "type" => "",
                             "boot" => true,
                             "initializeParams" => array (
                                               "sourceImage" => "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20150909a" )
                )
      ),
      "networkInterfaces" => array (
      0 => array ( "network" => "global/networks/default" ,
                       "accessConfigs" => array (
                          0 => array()
                       )
          )
       )
   );

每次出现以下错误:

Curl failed: malformed

修改

这是完整的功能。但仍然收到错误:Curl failed: malformed

function create_instance($project_id, $vm_name, $zone="us-central1-f") {
    $api = "https://www.googleapis.com/compute/v1/projects/";
    $url = $api."/".$project_id."/zones/".$zone."/instances?access_token=".$token;

    $body=utf8_encode('{ "machineType": "zones/us-central1-f/machineTypes/f1-micro", "name": "'.$vm_name.'", "disks": [{"type": "", "boot": true, "initializeParams": { "sourceImage": "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20150909a" } } ], "networkInterfaces": [ { "network": "global/networks/default", "accessConfigs": [ { } ] } ] }');
    $opts = $body;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$opts);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $data = curl_exec($ch);
    if ($data === FALSE) {
        die("Curl failed: " . curL_error($ch));
    }
    curl_close ($ch);
    echo "$data";
}

修改-2

现在从服务器获取以下错误:Bad Request Error 400 我觉得内容类型应该在application / json中。

任何帮助?

0 个答案:

没有答案