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();
<?
//Authentication rest API magento2.Please change url accordingly your url
$adminUrl='http://localhost/magento/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "", "password" => "");
$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');
$requestUrl='http://127.0.0.1/magento/index.php/rest/V1/products';
//Please note 24-MB01 is sku
$ch = curl_init();
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$post ='{
"product": {
"sku": "MY_SKU",
"name": "My Product",
"attributeSetId": "4",
"price": 20,
"status": 1,
"visibility": 4,
"typeId": "virtual",
"weight": 0,
"extensionAttributes": {
"stockItem": {
"stockId": 1,
"qty": 20,
"isInStock": true,
"isQtyDecimal": false,
"useConfigMinQty": true,
"minQty": 0,
"useConfigMinSaleQty": 0,
"minSaleQty": 0,
"useConfigMaxSaleQty": true,
"maxSaleQty": 0,
"useConfigBackorders": false,
"backorders": 0,
"useConfigNotifyStockQty": true,
"notifyStockQty": 20,
"useConfigQtyIncrements": false,
"qtyIncrements": 0,
"useConfigEnableQtyInc": false,
"enableQtyIncrements": false,
"useConfigManageStock": true,
"manageStock": true,
"lowStockDate": "string",
"isDecimalDivided": true,
"stockStatusChangedAuto": 0,
"extensionAttributes": {}
}
},
"options": [],
"tierPrices": [],
"customAttributes": [
]
},
"saveOptions": true
}';
$options = array(
CURLOPT_URL=>$toURL,
CURLOPT_HTTPHEADER=>array(
'Content-Type: application/json',
'Content-Length: ' . strlen($post)),
CURLOPT_VERBOSE=>0,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible;)",
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>$post,
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$result= json_decode($result);
print_r($result);
?>
the above code can not add product,i have try many method,still can not add product by rest api , anyone know how to add product by rest api?please help,Thanks.
the above code can not add product,i have try many method,still can not add product by rest api , anyone know how to add product by rest api?please help,Thanks.
<?
$url = domainname;
$token_url=$url."rest/V1/integration/admin/token";
$product_url=$url. "rest/V1/products";
$ch = curl_init();
$data = array("username" => username, "password" => password);
$data_string = json_encode($data);
$ch = curl_init($token_url);
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);
$adminToken= json_decode($token);
$sampleProductData = array(
'sku' => $isbn13,
'name' => $name,
'visibility' => 4,
'type_id' => 'simple',
'price' => $price,
'status' => 1,
'attribute_set_id' => 4,
'weight' => $product_weight,
'extension_attributes' => array(
"stock_item"=>array(
'qty' => $inventory_stock,'is_in_stock' => 1,'manage_stock' => 1,'use_config_manage_stock' => 1,'min_qty' => 0,'use_config_min_qty' => 1,'min_sale_qty' => 1,'use_config_min_sale_qty' => 1,'max_sale_qty ' => 10,'use_config_max_sale_qty' => 1,'is_qty_decimal' => 0,'backorders' => 0,'use_config_backorders' => 1,'notify_stock_qty' => 1,'use_config_notify_stock_qty' => 1
),
),
'custom_attributes' => array(
array( 'attribute_code' => 'category_ids', 'value' => ["43"] ),
array( 'attribute_code' => 'description', 'value' => $description ),
array( 'attribute_code' => 'short_description', 'value' => $short_description ),
array( 'attribute_code' => 'meta_title', 'value' => $meta_title),
array( 'attribute_code' => 'meta_keyword', 'value' => $meta_keyword),
array( 'attribute_code' => 'meta_description', 'value' => $meta_description),
),
);
$productData = json_encode(array('product' => $sampleProductData));
$setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $product_url);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
This is for product upload on magento 2 thorugh rest api Description = only copy code and put code in one php file and chagne domainname to (https://--------.com/) and after you create user and roles in magento 2(https://www.youtube.com/watch?v=hMN-ZoeODlQ) and put that username in username field in this code and password also and run this code replace with all dynamic variable.(like $isbn13,$name...etc with your product detail ) see product is create or update in magento 2
$adminUrl = 'http://localhost/magento2.1.2/index.php/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "", "password" => "");
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type=> application/json'));
$token = curl_exec($ch);
$token = json_decode($token);
use your magento admin username and password and get the token id.
Pass the $post json variable to this cURL call.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://localhost/magento2.1.2/index.php/rest/V1/products",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $post,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer $token",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
In request URL $requestUrl='http://127.0.0.1/magento/index.php/rest/V1/products' change "products" to product and try.
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 have a Facebook App that POST a picture to a specific album created also by my app. If I run the app the second time it will create another album and POST the new picture there. I would like if the user POST a new one, the picture to be placed in the same album and if is not there to be created. Here is my code so far:
$graph_url = "https://graph.facebook.com/me/albums?" . "access_token=". $access_token;
$album_name = 'MyApp Album';
$album_description = 'Album description';
$postdata = http_build_query(
array('name'=> $album_name,
'message'=> $album_description
));
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=> 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
));
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false, $context));
$album_id = $result->id;
$file = $_POST['photo'];
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = "https://graph.facebook.com/".$album_id."/photos?access_token=" . $access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
print_r(json_decode($data,true));
I don't wanna use FQL queries if isn't absolutely neccesary. If I can search probably for the album name and then return the ID it would be great.
I have an application that uses the old Facebook API but now I'm migrating it. The application works good until I try to upload a photo.
I knew how to do it in the old way, but now... I'm in troubles.
This is the way I used to do it:
$args = array
(
'method' => 'photos.upload',
'v' => $ver,
'api_key' => $key,
'uid' => $uid,
'call_id' => $cid,
'format' => 'XML',
'caption' => $caption
);
signRequest($args, $sec);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'http://api.facebook.com/restserver.php?method=photos.upload';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
Any ideas??
Thanks
The API url starts with https:// not http://. That could be the issue.
I found the solution here:
Uploading a picture to facebook
There is shown how to use the new Facebook Graph API with the PHP Curl function and a valid session token.