Get Profile image from Microsoft Graph - laravel

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()}}"/>

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 to setup session and user_id as soon as the method is called? (Codeigniter)

I have these two methods responsible for adding users from another system in that system.
private function api_login($email, $password){
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.#.com.br/v1/driver/login",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_ENCODING => "",
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 3000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode(['email' => $email, 'password' => $password, 'device_name' => 'web']),
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'Content-Type: application/json'
],
]);
$response_body = curl_exec($curl);
$response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$response_error = curl_errno($curl);
$err = curl_error($curl);
curl_close($curl);
$token = false;
if (!$err) {
if($response_code == 200){
$json_decode = json_decode($response_body);
$token = $json_decode->token;
}
}
return $token;
}
private function api_user_info(){
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.#.com.br/v1/driver/user",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_ENCODING => "",
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 3000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
'Authorization: Bearer '.$this->session->userdata('token_#'),
'Accept: application/json',
'Content-Type: application/json'
],
]);
$response_body = curl_exec($curl);
$response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$response_error = curl_errno($curl);
$err = curl_error($curl);
curl_close($curl);
$json_decode = '';
if (!$err) {
if($response_code == 200){
$json_decode = json_decode($response_body);
}
}
return $json_decode;
}
I want to set the parameters of the following method:
public function stripe_payment($user_id = "", $payment_request_from = "", $session_id = "") {
if ($this->session->userdata('user_login') != 1 && $payment_request_from == 'from_web'){
$this->session->set_flashdata('error_message', get_phrase('please_login_first'));
redirect('home/login', 'refresh');
}
//THIS IS HOW I CHECKED THE STRIPE PAYMENT STATUS
$response = $this->payment_model->stripe_payment($user_id, $session_id);
if ($response['payment_status'] === 'succeeded') {
// STUDENT ENROLMENT OPERATIONS AFTER A SUCCESSFUL PAYMENT
$check_duplicate = $this->db->get_where('bundle_payment', array('user_id' => $user_id, 'session_id' => $session_id, 'transaction_id' => $response['transaction_id']))->num_rows();
if($check_duplicate <= 0):
$this->course_bundle_model->bundle_purchase('stripe', $response['paid_amount'], $response['transaction_id'], $session_id);
$this->email_model->bundle_purchase_notification($user_id);
$this->session->set_userdata('checkout_bundle_price', null);
$this->session->set_userdata('checkout_bundle_id', null);
else:
$this->session->set_flashdata('error_message', site_phrase('session_time_out'));
redirect('home/course_bundles', 'refresh');
endif;
if($payment_request_from == 'from_web'):
$this->session->set_flashdata('flash_message', site_phrase('payment_successfully_done'));
redirect('home/my_bundles', 'refresh');
else:
//for mobile
endif;
}else{
if($payment_request_from == 'from_web'):
$this->session->set_flashdata('error_message', $response['status_msg']);
redirect('home', 'refresh');
else:
//for mobile
endif;
}
}
And i need to set up this params with the result of the api above.

Mailchimp API 3.0 batch/bulk untag

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;
}

How to submit multiple file images in API Laravel with using CURLOPT?

I'm trying to upload multiple images in the API of Laravel but it keeps getting 500 errors.
I'm using CURLOPT PHP for the API here are the codes below.
If you have an alternative way to do this in PHP that's ok for me.
$data = array(
"images" => $request->file('input_images'),
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://sample/api/name",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
curl_close($curl);
$result = json_decode($response);
print_r($result);

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

Resources