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

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

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

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

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

Testing AJAX/XMLHttpRequest pages functionally in Symfony2

Is it possible to simulate/make an XMLHttpRequest request (ajax) in symfony2 tests?
After search with "Problematic" answer, the correct syntax is:
$crawler = $client->request('GET', '/foo/', array(), array(), array(
'HTTP_X-Requested-With' => 'XMLHttpRequest',
));
The Request#isXmlHttpRequest() method simply checks if the X-Requested-With header is equivalent to XMLHttpRequest. If that's the method you're using to determine if a request is an ajax call, then you can simulate the behavior in the test client by adding the appropriate header to the request:
class FooFunctionalTest extends WebTestCase
{
$client = static::CreateClient();
$crawler = $client->request('GET', '/foo/', array(), array(), array(
'X-Requested-With' => 'XMLHttpRequest',
));
// ...
}
More information can be found about the Request object in the source code.
For POST, PUT:
$crawler = $client->request('POST', '/foo/', array('param' => 'value'), array(),
array(
'HTTP_X-Requested-With' => 'XMLHttpRequest',
));
For POST, PUT with raw JSON body:
$crawler = $client->request('POST', '/foo/', array(), array(), array(
'HTTP_X-Requested-With' => 'XMLHttpRequest',
'CONTENT_TYPE' => 'application/json',
), '{"param": "value"}');
If you are working with Symfony 3.x or 4.x this is the correct way to do it using POST method.
$data = ['some' => 'value'];
$client = static::createClient();
$client->request('POST', '/some_uri', ['data' => $data], [],; [
'HTTP_X-Requested-With' => 'XMLHttpRequest',
]);

Resources