I am woking on a php tool to deploy ovf templates from the vCenter (vmWare) via CURL and JSON. The tool is already working and deploys an ovf template succesfully. My problem is that the network-parameters are getting ignored completly.
The massive informations in the vmWare documentary doesnt help me out. I think the solution is to find the right "key"-integer.
Here are some Dokus i used:
https://vcenter.iseage.org/apiexplorer/ -> ovfLibraryItem->deploy
vdc-repo
Also my google-searche was not realy helpful.
My currently json-code:
{
"deployment_spec": {
"accept_all_EULA": true,
"default_datastore_id": "datastore-30598",
"network_mappings": [
{
"key": "4000",
"value": "network-154"
}
],
"name": "AATest08"
},
"target": {
"folder_id": "group-v5920",
"host_id": "host-1934",
"resource_pool_id": "resgroup-43"
}}
I hope someone can help me out.
I didn't find any solution for this case. The workaround that i use now, is to change the network after deloying the VM. If someone is interest in the code to change the network of an existing VM in the vCenter with the REST API, i post the code below.
function updateNetwork($IdFromTheVM, $NewNetworkID)
{
$requestUrl = 'https://' . $yourVCenterHost . '/rest/vcenter/vm/' . $IdFromTheVM . '/hardware/ethernet/4000';
$data = array(
'spec' => array(
'backing' => array(
'type' => 'STANDARD_PORTGROUP',
'network' => $NewNetworkID,
)
));
$dataJson = json_encode($data);
$ch = curl_init();
$header = array(
'vmware-api-session-id: ' . $yourVCenterSessionID,
'content-Type: application/json',
'Accept: application/json'
);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode != 200){
return false;
}
return true;
}
Related
I want to access data i have assigned to the data variable of feilds array to be sent in onesignal message.
function sendMessage($received)
{
$content = array(
"en" => 'Current Data'
);
$data = $received;
$fields = array(
'app_id' => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
'included_segments' => array('All'),
'contents' => $content,
'data' => $data, // => This is the data i want to access on
client side.
);
$response['feilds'] = $fields;
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:
application/json; charset=utf-8',
'Authorization:Basic
Nzg0YmM2NzUtZWYyMC00NDZmLWE1ODItZDExZTU3OTXXXXXX'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response['resp'] = curl_exec($ch);
curl_close($ch);
return $response;
}
I need help that how i can access the data variable there on client side in ionic app.
Below is what i am trying.
this.oneSignal.startInit('XXXXXXX-32ae-4025-b26b-
XXXXXXXXXX','XXXXXXXXX');
this.oneSignal.handleNotificationReceived().subscribe((jsonData) => {
if(jsonData.payload.additionalData)
{
this.myService.NotirefreshData(jsonData.payload.additionalData);
let ten = JSON.stringify(jsonData.payload.additionalData);
this.storage.set('current',ten);
this.presentAlert('From Server',ten);
this.storage.get('past').then((pri) => {
this.presentAlert('From Storage',JSON.parse(pri));
});
}
How should be the below statement to access the data variable to array feild received from server?
(jsonData.payload.additionalData.??????????????)
I have tried
(jsonData.payload.additionalData.feilds)
(jsonData.payload.additionalData.current) // as current is the name of an array
You don't need that subscribe() call. Simply pass in the handler into the function.
window.plugins.OneSignal
.startInit("YOUR_APP_ID")
.handleNotificationReceived(function(notificationData) {
alert("Notification Data Received:\n" + JSON.stringify(notificationData));
console.log('Notification Data Received: ' + JSON.stringify(notificationData));
})
.endInit();
I want to get my form data back from kobo api (https://kc.kobotoolbox.org/api/v1/). I registered client application from kobo. To authorize client application, i run below url but it results in
http://localhost/kobo/o/authorize?client_id=MY_CLIENT_iD&response_type=code&state=xyz
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.9
Similarly request for access token also result in same error
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/o/token/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&\ncode=PSwrMilnJESZVFfFsyEmEukNv0sGZ8&\nclient_id=MY_CLIENT_ID&\nredirect_uri=http://localhost:30000");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "USERNAME" . ":" . "PASSWORD");
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo $result;
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.9
I am using xampp and curl is enabled.
You have to use https://kc.kobotoolbox.org/api/v1/ not https://kf.kobotoolbox.org/api/v1/
checkout docs here http://help.kobotoolbox.org/managing-your-project-s-data/rest-services
MY WORKING CODE:
$url = 'https://kc.kobotoolbox.org/api/v1/stats';
$data = array (
// 'q' => 'nokia'
);
$params = '';
foreach($data as $key=>$value)
$params .= $key.'='.$value.'&';
$params = trim($params, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.'?'.$params ); //Url together with parameters
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7); //Timeout after 7 seconds
//curl_setopt($ch, CURLOPT_HEADER , false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Token xxxxxxxx';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//curl_setopt($ch, CURLOPT_USERPWD, "USERNAME" . ":" . "PASSWORD");
$result = curl_exec($ch);
if(curl_errno($ch)) //catch if curl error exists and show it
echo 'Curl error: ' . curl_error($ch);
echo $result;
I've set up a local Magento 2.1.2 environment with sample data. I'm trying to find the correct code for updating stocks through the REST api (catalogInventoryStockRegistryV1 - Put).
This is what i've got so far:
<?php
$adminUrl = 'http://www.localurl.pro/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "admin", "password" => "66sRz97CZt7N[GdqFU");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$token = curl_exec($ch);
$token= json_decode($token);
$headers = array("Authorization: Bearer $token");
$requestUrl='http://www.localurl.pro/rest/V1/products/24-MB01/stockItems/1';
// Sample SKU
$sampleProductData = array(
"qty" => 100
);
$productData = json_encode(array('stockItem' => $sampleProductData));
// Prints: {"stockItem":{"qty":100}}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $requestUrl);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
This script gives the following error:
string(122) "{"message":"Server cannot understand Content-Type HTTP header media type application\/x-www-form-urlencoded","trace":null}"
I'm not sure what the message exactly means. Some help would be greatly appreciated.
Solution
Solution given by Mohan Gopal. 'Content-Type: application/json' was missing in the curl header.
<?php
$adminUrl = 'http://www.localhost.pro/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "admin", "password" => "66sRz97CZt7N[GdqFU");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$token = curl_exec($ch);
$token= json_decode($token);
//Use above token into header
$headers = array("Authorization: Bearer $token","Content-Type: application/json");
$skus = array(
'24-MB04' => 10,
'24-MB03' => 5
);
foreach ($skus as $sku => $stock) {
$requestUrl='http://www.localurl.pro/rest/V1/products/' . $sku . '/stockItems/1';
$sampleProductData = array(
"qty" => $stock
);
$productData = json_encode(array('stockItem' => $sampleProductData));
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $requestUrl);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
unset($productData);
unset($sampleProductData);
}
Add the content type to header and change the header from
$headers = array("Authorization: Bearer $token");
to
$headers = array("Authorization: Bearer $token","Content-Type:
application/json");
after getting the token id on line 53.
Im trying a application with phalcon and all the operations by web service .So Using Micro application for RestFul services.
See below my code:
Usertypecontroller.php
public function createAction()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($ch,CURLOPT_URL,'http://localhost/xxx/yyy/api/usertype');
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
$data = curl_exec($sh);
curl_close($ch);
$this->dispatcher->forward(array(
'controller' => "usertype",
'action' => 'index'
));
}
myrestAPI index.php file
$app->post('/api/usertype', function () use ($app) {
$usertype = new Usertype();
$usertype->name = $this->request->getPost("name");
$usertype->status = $this->request->getPost("status");
$usertype->createdon = $this->request->getPost("createdon");
$usertype->updateon = $this->request->getPost("updateon");
if (!$usertype->save()) {
foreach ($usertype->getMessages() as $message) {
$this->flash->error($message);
}
return;
}
return json_encode(
array(
'status' => 'OK',
'data' => $usertype
)
);
});
Using Curl the the values are posting fine and inserting but its not redirecting to my controller and not showing my index page .it just stops at my Curl and displaying
{"status":"OK","data":{"user_type_id":"71","name":"zczxc","status":"1"}}
I have tried with CURLOPT_RETURNTRANSFER and CURLOPT_FOLLOWLOCATION configs but nothing works.Anybody can help me would be grateful.Thanks
I'm using AtTask's API with PHP and cURL.
Is there a way to POST data instead of appending it to the end of the URL with a question mark?
I know that I can change the request name itself like CURLOPT_CUSTOMREQUEST => 'POST' and I tried adding CURLOPT_POST => true
However, the CURLOPT_POSTFIELDS => array('name' => 'Untitled Project') is still ignored.
Did anyone work with this?
Pretty sure you need to wrap your postfields array with http_build_query(). Here is an example for logging in that works for me (when I put in my username and password):
$URL = 'https://hub.attask.com/attask/api/login';
$username = 'admin';
$password = 'user';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'username'=>$username,
'password'=>$password
)));
$results = curl_exec($ch);
curl_close($ch);
print_r($results);
Hope that helps.
I just ran into the same problem you did. It's been a few months since you asked the question, but if you're still running into it (or for anyone else hitting this wall), the issue is that you need to set the proper CURL options for POST/GET.
For a GET you'll need to set CURLOPT_HTTPGET to "true". It just makes sure the headers are in the correct order for the AtTask API server.
I've just created a github repo for the changes I've made to their sample StreamClient class.
https://github.com/angrychimp/php-attask
Feel free to use that or just lift the code from it. For your login example, GreenJimmy's example is 100% accurate. For your search question, you'll need to do something like the following.
$URL = 'https://hub.attask.com/attask/api/v4.0/';
$username = 'admin';
$password = 'pass';
$URL .= "task/search/?sessionID=$sessionID&ID=$taskID";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HTTPGET, true);
$results = curl_exec($ch);
curl_close($ch);
print_r($results);
The StreamClient in my github repo allows for arrays to be used for search params and response fields. It does make things a lot easier. For example.
require_once('StreamClient.php');
$client = new StreamClient('https://hub.attask.com', '4.0');
$client->login('admin', 'pass');
$records = $client->search('task', array('ID' => $taskID), array('assignedToID:emailAddr','actualWork'));
var_dump($records);