简体   繁体   中英

PHP Curl Json or XML return in to Array

I am running in to bit of annoyance more than anything. Maybe because I am tired?

My issue is the following code keeps dumping out into a string. Am I missing something? If I am not mistaken this should be a curl into xml in the variable and then I should be able to parse that?

<?php
$vcenter = "https://vcenter-65-ent.domain.com/rest/vcenter/vm/";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $vcenter);
//curl_setopt($curl, CURLOPT_FAILONERROR,1);
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('vmware-api-session-id: SOMESESSIONID', 'Accept: application/xml', 'Content-Type: application/xml'  ));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_HEADER, 0);
$str = curl_exec($curl);

echo $str;
curl_close($curl);
?>

and here how I see what type of output it is. This one is json:

<?php
$vcenter = "https://vcenter-65-ent.corp.intusurg.com/rest/vcenter/vm/vm-9520";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $vcenter);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('vmware-api-session-id: 6088820e0383d5b42110b208d642859d', 'Accept: application/json', 'Content-Type: application/json'  ));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$str = curl_exec($curl);

curl_close($curl);
$arr = json_decode($str);
echo gettype($arr);
?>

output:

{"value":{"cdroms":[{"value":{"start_connected":true,"backing":{"iso_file":"[M70-vmware-Linux-1] iso/rhel-server-7.2-x86_64-dvd.iso","type":"ISO_FILE"},"allow_guest_control":true,"label":"CD/DVD drive 1","ide":{"primary":false,"master":true},"state":"CONNECTED","type":"IDE"},"key":"3002"}],"memory":{"hot_add_increment_size_MiB":128,"size_MiB":12288,"hot_add_enabled":true,"hot_add_limit_MiB":196608},"disks":[{"value":{"scsi":{"bus":0,"unit":0},"backing":{"vmdk_file":"[DATASTORE] Server.vmdk","type":"VMDK_FILE"},"label":"Hard disk 1","type":"SCSI","capacity":53687091200},"key":"2000"}],"parallel_ports":[],"sata_adapters":[],"cpu":{"hot_remove_enabled":false,"count":4,"hot_add_enabled":true,"cores_per_socket":2},"scsi_adapters":[{"value":{"scsi":{"bus":0,"unit":7},"pci_slot_number":160,"label":"SCSI controller 0","type":"PVSCSI","sharing":"NONE"},"key":"1000"}],"power_state":"POWERED_ON","floppies":[{"value":{"start_connected":false,"backing":{"type":"CLIENT_DEVICE"},"allow_guest_control":true,"label":"Floppy drive 1","state":"NOT_CONNECTED"},"key":"8000"}],"name":"Server","nics":[{"value":{"start_connected":true,"pci_slot_number":192,"backing":{"network_name":"VM_VLAN240","type":"STANDARD_PORTGROUP","network":"network-666"},"mac_address":"00:00:00:00:00:00","mac_type":"ASSIGNED","allow_guest_control":true,"wake_on_lan_enabled":true,"label":"Network adapter 1","state":"CONNECTED","type":"VMXNET3","upt_compatibility_enabled":false},"key":"4000"}],"boot":{"delay":0,"retry_delay":10000,"enter_setup_mode":false,"type":"BIOS","retry":false},"serial_ports":[],"guest_OS":"RHEL_6_64","boot_devices":[],"hardware":{"upgrade_policy":"NEVER","upgrade_status":"NONE","version":"VMX_08"}}}

Your headers suggest XML.

For an xml string:

$array = simplexml_load_string($strXML);
print_r($array);

For json string:

$array = json_decode($strJSON, true);
print_r($array);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM