0

I'm trying to create a GCP instance using PHP. To create a instance I need to POST the request with below json.

{
  "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": [
        {

        }
      ]
    }
  ]
}

But I'm not sure how to do it. I tried below but didn't work.

$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);

I also tried like below but didn't work

$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()
                       )
          )
       )
   );

Every time I getting below error:

Curl failed: malformed

Edit:

Here is the full function. But still getting error : 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";
}

Edit-2:

Getting below error now from server: Bad Request Error 400 I feel the Content Type should be in application/json.

Any help?

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
gangadhars
  • 2,584
  • 7
  • 41
  • 68
  • I've just tried parsing just your JSON and it not formatted corrected... – An0nC0d3r Oct 30 '15 at 19:30
  • 1
    Possible duplicate of [Posting multidimensional array with PHP and CURL](http://stackoverflow.com/questions/3772096/posting-multidimensional-array-with-php-and-curl) – Dan Oct 30 '15 at 19:30
  • ^ But curl should not care if your JSON is whack anyway. You might get a json_ function error.... Is the error `CURLE_URL_MALFORMAT (3)`? Can you share URL? Or do you get same error if `$opts` is a simple string? – ficuscr Oct 30 '15 at 19:31
  • you have `$url = $url."/".$pr...` in there but url isnt defined before this – RisingSun Oct 30 '15 at 20:02

0 Answers0