如何通过api创建GCE实例

时间:2014-04-09 10:42:54

标签: php google-app-engine curl google-compute-engine


因变量是:

$ randomString 这是随机字符串

$ num_rows [0] 这是oAuth 2.0流程后正确的access_token


Requet:

  

$ url =“https://www.googleapis.com/compute/v1/projects/plenary-ability-439/zones/us-central1-a/disks?sourceImage=https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-6-v20140318&access_token=”。$ num_rows [0];

    $params = array(

                "kind" => "compute#disk",
                "zone" => "https://www.googleapis.com/compute/v1/projects/plenary-ability-439/zones/us-central1-a",
                "name" => $randomString,
                "description" => "any description "
            );
          $ch = curl_init();

            curl_setopt($ch, CURLOPT_HEADER,0);           

          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
          $headers = array("Content-Type: application/json" ); 
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);


          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          $result = curl_exec($ch);
          $res = json_decode($result, true);
     

Respnse:

     

{

     

“错误”:{

     

“错误”:[

     

{

"domain": "global",

"reason": "parseError",

"message": "This API does not support parsing form-encoded input."
     

}

     

],

     

“code”:400,

     

“message”:“此API不支持解析表单编码输入。”

     

}

     

}

$ headers = array(“Content-Type:application / json”); 到目前为止我知道了,我已经正确设置了

* 第二件事是:我也尝试了这个,但我得到了相同的回复*

     $headers = array(
        "Content-Type: application/json",
        "Authorization: Bearer $num_rows[0]", 
    );

1 个答案:

答案 0 :(得分:0)

您正在向Google发送http GET请求。所以使用这个:

$sourceImage = urlencode('https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-6-v20140318');
$access_token = urlencode($num_rows[0]);
$url = "https://www.googleapis.com/compute/v1/projects/plenary-ability-439/zones/us-central1-a/disks?sourceImage=$sourceImage&access_token=$access_token";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
$res = json_decode($result, true);