Cumulocity: Call all event by deviceId and datetime - php

Hi all i using cumulocity api with php , is all ready.
I have problem with get event/events by deviceId.
$url = 'https://*********.iot.a1.digital/event/events?dateFrom=' . $date . 'T' . $timeNew . '.000Z&pageSize=1000&type=lwm2m_log';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
// curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Acept: application/vnd.com.nsn.cumulocity.operation+json',
));
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
My url is
https://********_live.iot.a1.digital/event/events?dateFrom=2020-08-28T14:02:07.000Z&pageSize=1000&type=lwm2m_log&id=1234567
but return data is all event for all device.
answer
"events": [
{
"creationTime": "2020-12-07T09:49:13.879+01:00",
"source": {
"name": "860922049984564",
"self": "https://t14580169.iot.a1.digital/inventory/managedObjects/1973856",
"id": "1973856"
},
"type": "lwm2m_log",
"self": "https://t14580169.iot.a1.digital/event/events/13113138",
"time": "2020-12-07T09:49:13.874+01:00",
"text": "Registration update: RegistrationUpdate [registrationId=ivWv8hUHh2, identity=Identity /80.75.32.47:24140[unsecure], lifeTimeInSec=null, smsNumber=null, bindingMode=null, objectLinks=[</31101/0>, </5/0>, </3/0>, </6/0>]]\nUpdated registration: Registration [registrationDate=Mon Nov 23 09:19:00 CET 2020, identity=Identity /80.75.32.47:24140[unsecure], lifeTimeInSec=360, smsNumber=null, lwM2mVersion=1.0, bindingMode=U, endpoint=860922049984564, registrationId=ivWv8hUHh2, objectLinks=[</31101/0>, </5/0>, </3/0>, </6/0>], lastUpdate=Mon Dec 07 09:49:13 CET 2020]",
"id": "13113138"
}
]
How to create url get by deviceId

To query events (or other data types) by source device ID, please use the query parameter "source" instead. See https://cumulocity.com/guides/reference/events/#events-api
Your query should look like this:
https://********_live.iot.a1.digital/event/events?dateFrom=2020-08-28T14:02:07.000Z&pageSize=1000&type=lwm2m_log&source=1234567

Related

How can I create a form, and create a task on a ClickUp task

For few days I am trying to use the ClickUp API. The goal is:
We have a form, and when the form is submitted, it creates a task on a space on ClickUp.
It is a PHP form, and even with the API Doc here, I am not able to understand how it is working.
I found this: https://jsapi.apiary.io/apis/clickup20/reference/0/tasks/create-task.html
But it requires some access token, from the user, but normally, It's not supposed to work like that.
Here the script that I have taken from the doc:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickup.com/api/v2/list/17229593/task");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
\"name\": \"New Task Name\",
\"content\": \"New Task Content\",
\"assignees\": [
183
],
\"tags\": [
\"tag name 1\"
],
\"status\": \"Open\",
\"priority\": 3,
\"due_date\": 1508369194377,
\"due_date_time\": false,
\"time_estimate\": 8640000,
\"start_date\": 1567780450202,
\"start_date_time\": false,
\"notify_all\": true,
\"parent\": null,
\"custom_fields\": [
{
\"id\": \"0a52c486-5f05-403b-b4fd-c512ff05131c\",
\"value\": 23
},
{
\"id\": \"03efda77-c7a0-42d3-8afd-fd546353c2f5\",
\"value\": \"Text field input\"
}
]
}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: "access_token"",
"Content-Type: application/json"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
When I try that, I get the error 500, and when I go to the API's link, I get an Auth problem.
Is someone know something about this API?

Creating the issue with assigning the customer as the reporter with Jira API?

I would like to create new customer if it does not exist and create new issue setting this customer as reporter in PHP with Jira API.
I create new customer using API ‘servicedeskapi/customer’ with POST fields ‘fullName’ and ‘email’ and then I create new issue using API ‘api/2/issue’ with parameters ‘fullName’ and ‘email’ of parameter ‘reporter’.
I am trying do this in this way:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERPWD, 'user:password');
$postvars = array('fields' => array('project' => array('key' => 'AP'), 'summary' => $body, 'description' => $body, 'reporter' => null));
$postvars['fields']['issuetype'] = array("id" => '10108');
$postvars['fields']['reporter']['fullName'] = $name; //POST field of customer's name to set customer as the reporter of new issue
$postvars['fields']['reporter']['email'] = $email; //POST field of customer's email address to set customer as the reporter of new issue
curl_setopt($ch, CURLOPT_URL, 'https://jira-address/rest/servicedeskapi/customer');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json', 'X-ExperimentalApi: opt-in'));
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode(array('fullName' => $reporterName, 'email' => $email))); //POST fields for creating new customer
$r = curl_exec($ch); //sending request for creating new customer
echo json_encode(json_decode($r), JSON_PRETTY_PRINT); //printing response of request for creating new customer
curl_setopt($ch, CURLOPT_URL, 'https://jira-address/rest/api/2/issue/');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postvars));
$r = curl_exec($ch); //sending request for creating new issue
echo json_encode($postvars, JSON_PRETTY_PRINT); //printing POST fields of the request of creating new issue
echo json_encode(json_decode($r), JSON_PRETTY_PRINT); //printing the response of the request for creating new issue
If the customer does not exist, the response is:
{
"name": "kamilszmit2#live.com",
"key": "kamilszmit2#live.com",
"emailAddress": "kamilszmit2#live.com",
"displayName": "kamilszmit2",
"active": true,
"timeZone": "Europe\/Warsaw",
"_links": {
"jiraRest": "https:\/\/jira-address\/rest\/api\/2\/user?username=kamilszmit2%40live.com",
"avatarUrls": {
"48x48": "https:\/\/jira-address\/secure\/useravatar?avatarId=10122",
"24x24": "https:\/\/jira-address\/secure\/useravatar?size=small&avatarId=10122",
"16x16": "https:\/\/jira-address\/secure\/useravatar?size=xsmall&avatarId=10122",
"32x32": "https:\/\/jira-address\/secure\/useravatar?size=medium&avatarId=10122"
},
"self": "https:\/\/jira-address\/rest\/api\/2\/user?username=kamilszmit2%40live.com"
}
}{
"fields": {
"project": {
"key": "AP"
},
"summary": "test",
"description": "test",
"reporter": {
"fullName": "kamilszmit2",
"email": "kamilszmit2#live.com"
},
"issuetype": {
"id": "10108"
}
}
}{
"id": "11187",
"key": "AP-351",
"self": "https:\/\/jira-address\/rest\/api\/2\/issue\/11187"
}
The customer and the issue are created but the customer is not set as reporter of the issue. The same problem occurs if customer already exists and when only the parameter ‘fullName’ or ‘email’ of the parameter ‘reporter’ is used.
What am I doing wrong? How to create Jira issue with the customer as the reporter? Could you help me?
via REST, I have managed to set the reporter by using the name parameter, instead of fullname.
"fields": {
"project": {
"key": "AP"
},
"summary": "test",
"description": "test",
"reporter": {
"name": "kamilszmit2"
},
"issuetype": {
"id": "10108"
}
Other things I would check:
The Reporter field is added to the Create Issue screen
The credentials these calls are run with, have Modify reporter
project permission (You will run into 405 error if they do not have
this permission)

Binance 400 response and duplicate values for a parameter detected

still trying to get my head around API's and connections.
I am trying to do a buy order to Binance but keep getting a 400 response. I know it's been an issue for some others but I just can't seem to suss out where I am going wrong. So hoping someone can help me out.
It's only issues with POST's, all the GET requests are working fine, and so to the signature and timestamp... or at least I am getting responses for my account so I assume so.
The first CURL is just for the serverTime, but the second CURL is for a buy/sell order.
This is the response I am getting now...
"Object ( [code] => -1102 [msg] => Mandatory parameter 'side' was not
sent, was empty/null, or malformed. )"
If I type the in the string manually it works just fine, but for one reason or another when I pass $qs it presents the above fault. I echoed $qs to the screen and copied that instead of passing $qs and it worked when I put in the new timestamp. I am stumped...
"symbol=TRXUSDC&side=SELL&type=LIMIT&timeInForce=GTC&quantity=63000.00000000&price=0.02550000&recvWindow=1000000&timestamp=1550922237310"
any advice?
$header = array('X-MBX-APIKEY:' . KEY);
$url = BINANCE . 'api/v1/time';
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_URL,$url);
$response = curl_exec($curl);
if (FALSE === $response){
echo curl_error($curl), curl_errno($curl);
}
$serverTime = json_decode($response);
curl_close($curl);
$url = BINANCE . "api/v3/order";
$signature = NULL;
$queryString = NULL;
$query = array(
"symbol" => "TRXUSDC",
"side" => "SELL",
"type" => "LIMIT",
"timeInForce" => "GTC",
"quantity" => number_format(63000.00000000,8,'.',''),
"price" => number_format(0.02550000,8,'.',''),
"recvWindow" => 1000000,
"timestamp" => $serverTime->serverTime);
$qs = htmlentities(http_build_query(array_unique($query)));
$query['signature'] = hash_hmac('SHA256', $qs, SECRET );
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_ENCODING, "application/x-www-form-urlencoded");
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl,CURLOPT_FAILONERROR,FALSE);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS,$qs);
curl_setopt($curl, CURLOPT_URL, $url);
$response = curl_exec($curl);
if (FALSE === $response){
echo curl_error($curl).':'.curl_errno($curl);
}
$obj = json_decode($response);
curl_close($curl);
I tried this code and var_dump $qs
symbol=BTCUSDT&side=BUY&...
so I think you must remove amp;
Based on Binance spot api docs:
It is recommended to use a small recvWindow of 5000 or less! The max cannot go beyond 60,000!
so edit your query params with:
"symbol=TRXUSDC&side=SELL&type=LIMIT&timeInForce=GTC&quantity=63000.00000000&price=0.02550000&recvWindow=60000&timestamp=1550922237310"
Also you can check your other query params with exchange info that you can get with https://api.binance.com/api/v3/exchangeInfo that we have for your symbol:
{
"symbol": "TRXUSDC",
"status": "TRADING",
"baseAsset": "TRX",
"baseAssetPrecision": 8,
"quoteAsset": "USDC",
"quotePrecision": 8,
"quoteAssetPrecision": 8,
"baseCommissionPrecision": 8,
"quoteCommissionPrecision": 8,
"orderTypes": [
"LIMIT",
"LIMIT_MAKER",
"MARKET",
"STOP_LOSS_LIMIT",
"TAKE_PROFIT_LIMIT"
],
"icebergAllowed": true,
"ocoAllowed": true,
"quoteOrderQtyMarketAllowed": true,
"isSpotTradingAllowed": true,
"isMarginTradingAllowed": false,
"filters": [
{
"filterType": "PRICE_FILTER",
"minPrice": "0.00001000",
"maxPrice": "1000.00000000",
"tickSize": "0.00001000"
},
{
"filterType": "PERCENT_PRICE",
"multiplierUp": "5",
"multiplierDown": "0.2",
"avgPriceMins": 5
},
{
"filterType": "LOT_SIZE",
"minQty": "0.10000000",
"maxQty": "9000000.00000000",
"stepSize": "0.10000000"
},
{
"filterType": "MIN_NOTIONAL",
"minNotional": "10.00000000",
"applyToMarket": true,
"avgPriceMins": 5
},
{
"filterType": "ICEBERG_PARTS",
"limit": 10
},
{
"filterType": "MARKET_LOT_SIZE",
"minQty": "0.00000000",
"maxQty": "659177.02430556",
"stepSize": "0.00000000"
},
{
"filterType": "MAX_NUM_ORDERS",
"maxNumOrders": 200
},
{
"filterType": "MAX_NUM_ALGO_ORDERS",
"maxNumAlgoOrders": 5
}
],
"permissions": [
"SPOT"
]
}
for other possible reasons check spot api docs in the link https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md
I tested the code and saw a few glitches in it :
$qs = htmlentities(http_build_query(array_unique($query)));
gave me an error ({"code":-1102,"msg":"Mandatory parameter 'side' was not sent, was empty/null, or malformed."}), so i replaced it with:
$qs = http_build_query(array_unique($query));
After that you created your signature, but never added it to you request. So do the following :
$signature = hash_hmac('sha256', $qs, $apiSecret);
$qs .= "&signature=" . $signature;
Also update your 'recvWindow' to a max of 60000 (I suggest 50000 or less (see the answer of #Tofiq Samali)) and you are ready to go!

Sending post JSON to an api - no output

I'm working with a new API and I'm trying to understand how to correctly login and send post requests to the api.
Here are some screenshots:
Also , should this authentication be done every time a request is sent or ??(it says something about a token every 8 hours in the notes)
Here is what I did so far but I'm getting no output from that print_r(probably because I haven't authenticated as shown in the notes(Something I don't know how to do correctly):
$data_string = '{
"nameOnCard":"Johnny",
"userDetail": {
"lastName":"Smith",
"firstName":"John",
"middleInitial":"xx",
"dateOfBirth":"1975-02-28",
"addressLine1":"222333 PEACHTREE PLACE",
"city":"Winchester",
"zipCode":"GU14 7JF",
"country":"GB",
"mobileNumber":"2071234567",
"landlineNumber":"57647564234",
"email":"demouser#mailinator.com",
"currencyCode":"EUR",
"externalReferenceId":"WC34930",
"registeredFromIp":"10.10.10.10",
"acceptTermsAndConditions": true,
"acceptEsign" : true
},
"channelType": "1",
"cardProgramId": "0",
"localeTime": "2013-06-04T00:00:00.000+05:00",
"refId": "1",
"dlvAddress": {
"addressLineOne":"222333 DELIVERY PLACE",
"city":"Winchester",
"zipCode":"GU14 7JF",
"country":"GB"
}
}';
$ch = curl_init('https://wcapi.wavecrest.in/apisandbox/cards');
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',
'Developerid: xxxxxxxxxx',
'Developerpassword: xxxxxxx',
'X-Method-Override: login',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
print_r($result);
Would appreciate if anyone can help me, thanks!
EDIT: other post got inactive , editing it wouldn't have helped me.

Elasticsearch Bulk data insertion - JsonParseException[Unexpected character - Using PHP

While indexing the bulk data using PHP curl method I am getting the exception as "error":"JsonParseException[Unexpected character (':' (code 58)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B#14abb68; line: 1, column: 18]]","status":500}
Kindly find below code which I am using for the same and do let me know what could be wrong over here.
<?php
$ch = curl_init();
$method = "POST";
$url = "http://192.168.1.204/myindex/test/_bulk";
$qry = '
{"index":{"_index": "myindex","_type":"test"}}
{
"product_id": 1,
"title": "mobile"
}
{"index":{"_index": "myindex","_type":"test"}}
{
"product_id": 2,
"title": "laptop",
}
';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, 9200);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
You are not using the correct format (and there is an additional comma at the second element).
It should be:
action_and_meta_data\n
optional_source\n
action_and_meta_data\n
optional_source\n
....
action_and_meta_data\n
optional_source\n
Extracted from the documentation of elasticsearch - bulk
So, you should add the data using the following format:
{"index":{"_index": "myindex","_type":"test"}}
{"product_id": 1,"title": "mobile"}
{"index":{"_index": "myindex","_type":"test"}}
{"product_id": 2,"title": "laptop"}

Categories