Im trying to get people to authorize with spotify and then subscribe their email to a Mailchimp list.
I got two pieces of code that i need to tie together:
-
The spotify authorize (which works since i am able to make them follow a user)
if($_SESSION['accessToken']){ require 'vendor/autoload.php'; $api = new SpotifyWebAPI\SpotifyWebAPI(); $api->setAccessToken($_SESSION['accessToken']); $following = $api->followArtistsOrUsers('artist', '####'); $me = $api->me(); echo $me->email;
At the end I retrieve the users e-mail adres with this piece of code that should be right:
$me = $api->me();
echo $me->email;
The second piece of code is the subscribe to mailchimp
- mailchimp subscribe:
this one works, im able to subscribe testmail@test.com to the right list.
<?php
$email = 'testmail@test.com';
$list_id = '#####';
$api_key = '####';
$data_center = substr($api_key,strpos($api_key,'-')+1);
$url = 'https://'. $data_center .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members';
$json = json_encode([
'email_address' => $email,
'status' => 'subscribed', //pass 'subscribed' or 'pending'
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo $status_code;
}
?>
However, i seem to be unable to subscribe the e-mail retrieved from spotify to the mailchimplist using I tried:
if($_SESSION['accessToken']){
require 'vendor/autoload.php';
$api = new SpotifyWebAPI\SpotifyWebAPI();
$api->setAccessToken($_SESSION['accessToken']);
$following = $api->followArtistsOrUsers('artist', '####');
$me = $api->me();
echo $me->email;
$email = $me->email;
$list_id = '#####';
$api_key = '####';
etc. etc.
can you guys help me out?
Thank you! Sidenote: im just starting out with code, sorry for my rookie level :)