Mailchimp API 3.0 batch/bulk untag - mailchimp

I have a problem with untag the subscribers in a bulk with the Mailchimp API.
In the documentation https://mailchimp.com/developer/guides/how-to-use-tags/#Tag_multiple_contacts_in_bulk is the example:
{
"members_to_remove": [
"john.smith#example.com",
"brad.hudson#example.com"
]
}
Below can you see my PHP code where I with the $methode variable give the value members_to_remove and the $email value is an array with email addresses.
But the script does only add tags with a bulk to the subscriptions instead of remove.
What do I wrong?
public function tag_mailchimp($list_id, $email, $tag, $method) {
$authToken = 'HERE MY KEY';
// The data to send to the API
$postData = array(
$method => $email
);
// Setup cURL
$ch = curl_init('https://us2.api.mailchimp.com/3.0/lists/'.$list_id.'/segments/'.$tag);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: apikey '.$authToken,
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
return $response;
}

It works, but I don't know what I do else.., the code:
foreach($members as $member) {
$list[] = $member->email;
}
$Mailer->tag_mailchimp('12345678', $list, 123456, 'members_to_remove');
And in the class I have the next function:
public function tag_mailchimp($list_id, $email, $tag, $method) {
$authToken = 'HERE MY KEY';
// The data to send to the API
$postData = array(
$method => $email
);
// Setup cURL
$ch = curl_init('https://us2.api.mailchimp.com/3.0/lists/'.$list_id.'/segments/'.$tag);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: apikey '.$authToken,
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
return $response;
}

Related

Convert Curl in Laravel Http facade

i want refactor curl method which is work with laravel Http facade and now i'm encounter some error
public function get($data)
{
$post_data = http_build_query($data, '', '&');
$sign = hash_hmac('sha512', $post_data, env('INDODAX_SECRET_KEY'));
$response = Http::withHeaders([
'Key' => env('INDODAX_API_KEY'),
'Sign' => $sign,
])->withOptions([
'form_params' => $data,
])
->withBody('post_data', $post_data)
->post(config('indodax.private.endpoint'), [
'CURLOPT_POSTFIELDS' => $post_data,
]);
return $response->collect();
}
public static function curl($data)
{
$post_data = http_build_query($data, '', '&');
$sign = hash_hmac('sha512', $post_data, env('INDODAX_SECRET_KEY'));
$headers = ['Key:'.env('INDODAX_API_KEY'),'Sign:'.$sign];
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_HTTPHEADER => $headers,
CURLOPT_URL => config('indodax.private.endpoint'),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($curl);
return json_decode($response, true);
}
i think i'm missing CURLOPT_POSTFIELDS part, how to set CURLOPT_POSTFIELDS in Http facade
i'v tried with withOptions and still same
reff PHP Curl proxy option in Laravel Http Facade

How would I make the same curl request in something like Guzzle?

I've got this php curl request that I stumbled onto that works but would like to use something like Guzzle instead and cant quite figure out how to make the conversion. Anything I try is giving me status code 400, Bad Authentication data.
Any help or references would be much appreciated.
$header = array($this->oauth, 'Expect:' );
$header['Content-Type'] = $multipart ? 'multipart/form-data' : 'application/x-www-form-urlencoded';
$options = [
CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => $return,
CURLOPT_TIMEOUT => 30,
];
if (!is_null($this->postFields)){
$options[CURLOPT_postFields] = http_build_query($this->postFields, '', '&');
}
else if ($this->getField !== ''){
$options[CURLOPT_URL] .= $this->getField;
}
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
// $this->httpStatusCode = curl_getinfo($feed, CURLINFO_HTTP_CODE);
if (($error = curl_error($feed)) !== '')
{
curl_close($feed);
throw new \Exception($error);
}
curl_close($feed)
First install guzzle, and use code like this
$client = new \GuzzleHttp\Client();
$header = array($this->oauth, 'Expect:' );
$header['Content-Type'] = $multipart ? 'multipart/form-data' :
'application/x-www-form-urlencoded';
try
{
$response = $client->request('GET', $url, [ 'headers' => $header]);
}
catch(\GuzzleHttp\Exception\ClientException $e)
{
return response(['status' => 0, 'error' => $e->getMessage()]);
}
catch(\GuzzleHttp\Exception\ServerException $e)
{
return response(['status' => 0, 'error' => 'Something went wrong']);
}

How to solve the error - "Submitted URI too large!"

I am using a curl method for getting the response in laravel. And after that I decode the response data and send on view page from the controller but I got the error -
Submitted URI too large!
My code is here on detailController.php-
public function details(Request $request)
{
$json_data = array('requested data on array');
$data_json = json_encode($json_data);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "url where we send the request",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 20,
CURLOPT_TIMEOUT => 50,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data_json,
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"api-key: key provided from url",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "error"; }
else {
$getdetails = json_decode($response, true);
return view('viewpageurl', compact('getdetails'));
}
}
viewpageurl.blade.php
#if($getdetails)
#foreach($getdetails as $getname)
<h2>{!! $getname->details->name !!}</h2>
#endforeach
#endif

Login a User in laravel application after API endpoint authentication

I am working on this application where i created an API to do the business logic and using Laravel as the core part of my application that calls the API endpoints for resources.
Authentication is been done by the API which only returns a JSON resource. I am a bit confused as to what is the best way to log a user into my laravel application after successful authentication from the API.
//
public function login(Request $request)
{
$data = [
'username' => $request->username,
'password' => $request->password,
];
$data = Json_encode($data, TRUE);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://localhost:8080/api/v1/auth/login",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$response = Json_decode($response);
// var_dump($response);
if ($err) {
echo "cURL Error #:" . $err;
} else {
if ($response->status === 'failed') {
if ($response->message === "Incorrect login details") {
return redirect()->back()->with('error' , $response->message);
}elseif ($response->message === "User's account has not been activated") {
return view('activation', ['email' => $response->data]);
}
}elseif ($response->status === 'success' && $response->message === "Ok") {
$user = $response->data;
Auth::login($user , true);
return view('dashboard', ['user' => $user]);
}
}
After getting the required response from the API, intend logging the user straight into the application.
What is the best way for this?
If you have same database to connect with on the user. you can just simply call
Auth::loginUsingId($id);
this will automatically log in user with that id.

Get Profile image from Microsoft Graph

I am trying to get the profile image of the current user with Microsoft Graph. I am using the msgraph-sdk-php.
The code below gets the the photo, but returns the binary data of the jpeg file.
if (session_status() == PHP_SESSION_NONE)
session_start();
$graph = new Graph();
$graph->setAccessToken($_SESSION['access_token']);
$photo = $graph->createRequest("GET", "/me/photo/\$value")
->execute();
return $photo->getRawBody();
It seems that I need to set the response type to blob before I can use the image in a more normal way, but how do I do that with Guzzle?
I also tried it with cUrl, but the same issue, all I get is binary data:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.microsoft.com/v1.0/me/photos/48x48/\$value",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer $at",
"cache-control: no-cache",
"Content-type: image/jpeg",
"Accept: blob",
"postman-token: caccedb3-8253-e6aa-7e30-25052bc28f2f"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
return $response;
}
Assuming you're using Laravel (which you tagged)
Add the Content-Type header to your response, so the browser understands what type of data it is:
return response($response)
->header('Content-Type', 'image/jpeg');
Alright found it finally:
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.microsoft.com/v1.0/me/photos/48x48/\$value",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer $at",
"cache-control: no-cache",
"postman-token: 2d4b85a3-5490-3f58-ff74-52e0a98286ec"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
return base64_encode($response);
}
And in the template:
<img src="data:image/jpeg;base64,{{\O365\Profile::photo()}}"/>

Resources