Can't add user to a group using MailChimp API 3.0 - php

I'm trying to update my code for subscribing new users to my MailChimp newsletter.
I would like it to add the user to a group, so that I can distinguish between users when sending out newsletters. This code, however, subscribes the user but does not add it to any group. What am I doing wrong? I am using the MailChimp API 3.0.
My code:
$apiKey = 'XXXX';
$list_id = 'XXXX';
$memberId = md5(strtolower($data['email']));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . $memberId;
$json = json_encode(array(
'email_address' => $data['email'],
'status' => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
'merge_fields' => array(
'FNAME' => $data['firstname'],
'LNAME' => $data['lastname'],
'GROUPINGS' => array(
0 => array(
'name' => 'Interests',
'groups' => array( $data['tag'] ),
),
),
),
));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

You are passing Interest grouping in the wrong array.
you need to pass the interest group id in interests array with value as true.
$json = json_encode(array(
'email_address' => 'test#example.com',
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => 'FirstName',
'LNAME' => 'LastName',
),
'interests' => array(
'9143cf3bd1' => true,
'789cf3bds1' => true
),
));
You will get the id of interest groups [9143cf3bd1,789cf3bds1 in above example] of the list by requesting to this URL
/lists/{list_id}/interest-categories
see doc here
If you want to remove the subscriber from the group, you need to set its value to false in your update request.

Related

OneSignal notification redirect to invalid URL

I have a OneSignal setup for sending notification to specific users via external user id as follows
$fields = [
'app_id' => env( 'app.onesignalAppKey' ),
'priority' => 10, // High priority.
'include_external_user_ids' => [ md5( $contact['email'] ) ],
'channel_for_external_user_ids' => 'push',
'headings' => [ 'en' => $this->subject ],
'contents' => [ 'en' => $this->message ],
'url' => [ 'en' => base_url( $this->args['log_extra']['url'] ) ]
];
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, env( 'app.Onesignalurl'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic '. env( 'app.onesignalAPI' )));
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 = curl_exec($ch);
curl_close($ch);
While testing I'm receiving notification, but clocking the notification redirect to wrong url.
https://example.com/onesignal/{"en"=>"https://example.com/correct-path"}
OneSignal service worker installed with scope set to /onesignal/
Am I don't something wrong here..?
Just put the URL as string in $fields array, not an array.
like:
'url' => base_url( $this->args['log_extra']['url'] ),

Trying to send a POST request using php, No matter what i do i get "HTTP ERROR 500"

To make an HTTP request, someone suggested I try using PHP and gave me a piece of code to work on:
$url = 'https://example.com/dashboard/api';
$data = array('to' => PHONE_NUMBER, 'from' => SENDER_ID, 'message' => TEXT, 'email' => EMAIL, 'api_secret' => SECRET, 'unicode' => BOOLEAN, 'id' => IDENTIFIER);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
So I took the code, edited the fields that I needed to, pasted it into a .php file, uploaded on my web server (running PHP 5.6) and then while trying to run the .php file, I get HTTP ERROR 500.
I'm a complete newbie to all this and I'm not even sure if I am doing everything correctly.
$url = 'https://domainname.com/dashboard/api';
$params = array('to' => PHONE_NUMBER, 'from' => SENDER_ID, 'message' => TEXT, 'email' => EMAIL, 'api_secret' => SECRET, 'unicode' => BOOLEAN, 'id' => IDENTIFIER);
$query_content = http_build_query($params);
$context = stream_context_create([
'http' => [
'header' => [
'Content-type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($query_content)
],
'method' => 'POST',
'content' => $query_content
]
]);
$result = file_get_contents($url, false, $context);
$url = 'https://domainname.com/dashboard/api';
$header = [
'Content-type: application/x-www-form-urlencoded',
];
$params = array('to' => PHONE_NUMBER, 'from' => SENDER_ID, 'message' => TEXT, 'email' => EMAIL, 'api_secret' => SECRET, 'unicode' => BOOLEAN, 'id' => IDENTIFIER);
$c = curl_init();
curl_setopt($c, CURLOPT_URL,$url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $params);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_HTTPHEADER, $header);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
$res = curl_exec($c);
var_dump($res);

Integrate Coinbase Commerce in PHP

I want to integrate Coinbase Commerce API in one of my web application. I have refer this link https://commerce.coinbase.com/docs/ and create a demo in local server. I successfully get the below output
and below screen
Now i want to know After getting the last screen which give me address what to do with that code. does i need to open it in any specific application or I have to use it in my code. If i need to use in code provide me example code.
Also I have try to make curl request of "Create Charge" with following code but I didn't get any response.
$metadata = array(
'customer_id' => '123456',
'customer_name' => 'adarsh bhatt'
);
$request_body = array(
'X-CC-Api-Key' => 'd59xxxxxxxxxxxxxxb8',
'X-CC-Version' => '2018-03-22',
'pricing_type' => 'fixed_price',
'name' => 'Adarsh',
'description' => ' This is test donation',
'local_price' => array(
'amount' => '100.00',
'currency' => 'USD'
),
'metadata' => $metadata
);
$req = curl_init('https://api.commerce.coinbase.com/charges');
curl_setopt($req, CURLOPT_RETURNTRANSFER, false);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($req, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen(json_encode($request_body))));
curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($request_body));
$respCode = curl_getinfo($req, CURLINFO_HTTP_CODE);
$resp = json_decode(curl_exec($req), true);
curl_close($req);
echo '<pre>';
print_r($output);
exit;
The following should be a valid request to create a charge and return the hosted URL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.commerce.coinbase.com/charges/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
"name" => "E currency exchange",
"description" => "Exchange for Whatever",
"local_price" => array(
'amount' => 'AMOUNT',
'currency' => 'USD'
),
"pricing_type" => "fixed_price",
"metadata" => array(
'customer_id' => 'customerID',
'name' => 'ANY NAME'
)
);
$post = json_encode($post);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "X-Cc-Api-Key: YOUR-API-KEY";
$headers[] = "X-Cc-Version: 2018-03-22";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close ($ch);
$response = json_decode($result);
return $response->data->hosted_url;
You can try this way. It's work for me
$curl = curl_init();
$postFilds=array(
'pricing_type'=>'no_price',
'metadata'=>array('customer_id'=>10)
);
$postFilds=urldecode(http_build_query($postFilds));
curl_setopt_array($curl,
array(
CURLOPT_URL => "https://api.commerce.coinbase.com/charges",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postFilds,
CURLOPT_HTTPHEADER => array(
"X-CC-Api-Key: APIKEY",
"X-CC-Version: 2018-03-22",
"content-type: multipart/form-data"
),
)
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
Hello you can use php sdk for coinbase commerce.
https://github.com/coinbase/coinbase-commerce-php
composer require coinbase/coinbase-commerce
use CoinbaseCommerce\Resources\Charge;
use CoinbaseCommerce\ApiClient;
ApiClient::init('PUT_YOUR_API_KEY');
$chargeData = [
'name' => 'The Sovereign Individual',
'description' => 'Mastering the Transition to the Information Age',
'local_price' => [
'amount' => '100.00',
'currency' => 'USD'
],
'pricing_type' => 'fixed_price'
];
$chargeObj = Charge::create($chargeData);
var_dump($chargeObj);
var_dump($chargeObj->hosted_url);

How to send one signal push notification with large image using PHP

Code sends only a title and message but icon not getting received. I am new to one signal API. Here is my code:
<?php
$fields = array(
'app_id' => 'my app id',
'include_player_ids' => ['player_id'],
'contents' => array("en" =>"test message"),
'headings' => array("en"=>"test heading"),
'largeIcon' => 'https://cdn4.iconfinder.com/data/icons/iconsimple-logotypes/512/github-512.png',
);
$fields = json_encode($fields);
//print("\nJSON sent:\n");
//print($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 M2ZNDYtMjA4ZGM2ZmE5ZGFj'
));
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 = curl_exec($ch);
curl_close($ch);
print_r($response);
?>
You just simply add:
"chrome_web_image" => "image url";
$fields = array(
'app_id' => 'my app id',
'include_player_ids' => ['player_id'],
'contents' => array("en" =>"test message"),
'headings' => array("en"=>"test heading"),
'chrome_web_image' => 'https://cdn4.iconfinder.com/data/icons/iconsimple-logotypes/512/github-512.png',
);
To further reading please go to: doc appearance of onesignal
Result:
They key for the icon should be large_icon not largeIcon
Check the API for more details
You can use ios_attachments parameter and it must be in array.
This is how I did it:
$iCon = array(
"ios_attachments" => 'https://Your.Image.png'
);
$fields = array(
// OneSignal - Personal ID
'app_id' => "Your_OneSignal_ID",
'included_segments' => array('All'),'data' => array("foo" => "bar"),
// Submitting Title && Message && iCon
'headings' => $headings,
'contents' => $content,
'ios_attachments' => $iCon,
);
$fields = array(
'app_id' => $app_id,
'headings' => array("en"=>$data['title']),
'contents' => ["en" => $data['description']] ,
'big_picture' => $data['file'],
'large_icon' => $data['file'],
'url' => $data['launch_url']
);

Subsrcibe a user in MailChimp API V3 give bad response

When I try to subscribe a user on mailchimp, it gives an error response pasted below:
‹<»nÃ0E…Вű2tòèt+ŠB‘[€.I¹0‚ü{i»è(Þ‹ÃC=Œ¬3šÁL"ómÀS‘úìbòSÌsïk¶¡ú–±ˆ“X‹ýÏìØb#¶HTé<¦Êìhµ¦3%mÜ·²¸\‘k#±R›áåréL#Q˜ß'ú+·[Ž"À×–”pCØQNÇ=¼V‚{ÄÎ<£÷èá#qŒ¢¸Ó®Å'pDníuu,º¼øML_Gl†‡ÙQÇ4£1n•+~·H±§?H¸«ÌT½;€ÛW|¹TÍóóùÿÿI*¯Q`
Here is the PHP code:
$apiKey = 'YOUR API KEY';
$listId = 'YOUR LIST ID';
$url2 = 'https://us12.api.mailchimp.com/3.0/lists/'.$listId.'/members/';
$args = array(
'email' => 'abc#xyz.com',
'status' => 'subscribed',
'merge_fields' => array(
'first_name' => 'WOW',
'last_name' => 'WOW',
'mobile'=>'',
'message'=>'',
'ID'=>'000'
));
function syncMailchimp($url,$apiKey,$args) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "user apikey:" . $apiKey);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Is the server sending bad data or is client side using a wrong encoding and how do I fix it?
Changing the key called email to email_address appears to have fixed it. Perhaps that bad key caused the server to puke.
$args = array(
'email' => 'abc#xyz.com',
'status' => 'subscribed',
'merge_fields' => array(
'first_name' => 'WOW',
'last_name' => 'WOW',
'mobile'=>'',
'message'=>'',
'ID'=>'000'
));
to
$args = array(
'email_address' => 'abc#xyz.com',
'status' => 'subscribed',
'merge_fields' => array(
'first_name' => 'WOW',
'last_name' => 'WOW',
'mobile'=>'',
'message'=>'',
'ID'=>'000'
));

Categories