Please I need to send daily a group of data from my LARAVEL application using Sendinblue API [this how the external server API looks like ( api.sendinblue.com/v3/contacts/identifier ) ], I've managed to used Sendinblue PHP code to send one contact on the time.
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/identifier",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\"attributes\":{\"newKey\":\"New Value\"}}",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Content-Type: application/json",
"api-key: My API"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
My question there is a way to send all data at once and daily in an automatic way?
Should I use a For each in the controller or there is another ways?
Thank you in advance.
Yes, you can do it with Laravel scheduler.
Read More about scheduler
Related
my slack bot can't send a message to a channel with space or - like "channel-name" but works well for a one word channel.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://slack.com/api/chat.postMessage",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{ \"channel\": \"channel-withspace\",\"text\": \"hello\"}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer <here>"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Here is the error:
{"ok":false,"error":"not_in_channel","warning":"missing_charset","response_metadata":{"warnings":["missing_charset"]}}
You can add following scope to your bot to post messages to public channel without inviting the bot to the channel.
Scope : chat:write.public
Send messages to channels #your_slack_app isn't a member of
https://api.slack.com/scopes/chat:write.public
Found the answer, turns out I need to invite the bot by typing a message in channel:
/invite #bot-name
Found the answer here: https://stackoverflow.com/a/61369364/5159914
We are getting the following error when executing this:
[description] => Authentication values are missing [code] => 1001
Can anyone see what I am doing wrong.
I tried removing the base64_encode function but this didn't have any impact.
I got the integration key and password from opayo (sagepay) - the test environment.
My php code follows:
$key = base64_encode("My Integration Key");
$password = "My Integration Password";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://pi-test.sagepay.com/api/v1/merchant-session-keys",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{ "vendorName": "MY VENDOR" }',
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $key:$password",
"Cache-Control: no-cache",
"Content-Type: application/json"
)
));
$response = curl_exec($curl);
$response = json_decode($response, true);
$err = curl_error($curl);
curl_close($curl);
Tia for any help
Both the key and password had to be base 64 encoded.
ie $key = base64_encode("My Integration Key:My Integration Password");
and then fed in to the curl http header as
"Authorization: Basic $key",
I had the same issue, so created a NuGet package for this API-client code: https://github.com/joeratzer/opayo-admin-and-reporting-api-client.
You can find the NuGet package here:
https://www.nuget.org/packages/Joe.Opayo.Admin.Api.Client/1.1.0
The client app allows code like this:
var client = new OpayoAdminApiClient();
var isTest = true;
var request = new OpayoApiRequest(ApiCommandType.GetTransactionDetail, "password", isTest, "user-name", "vendor-name");
var commandSpecificXml = "<vendortxcode>01Jan2010Transaction12345</vendortxcode>";
return await client.ProcessApiCommandAsync<TransactionDetail>(request, commandSpecificXml);
I am having issues in accessing contact from Xero. I keep getting an error - AuthorizationUnsuccessful
Below please find the code I am using to send my request as well as the response I am getting:
'scopes' => (
'openid',
'email',
'profile',
'offline_access',
'accounting.settings',
'accounting.contacts'
)
in my xero config file
Request passing in Postman
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.xero.com/api.xro/2.0/Contacts",
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(
"accept: application/json",
"authorization: Bearer eyJh.......N6mbaw",
"cache-control: no-cache",
"postman-token: 51048d11-4f31-ba27-16c7-48739f14c6f0",
"xero-tenant-id: e8672ad4-ea92-4698-87aa-a69f5b049265"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
Response:
{
"Type": null,
"Title": "Unauthorized",
"Status": 401,
"Detail": "AuthorizationUnsuccessful",
"Instance": "14ae9993-dc1b-4d8d-a4c0-15b2c343f337",
"Extensions": {}
}
Your assistance would be greatly appreciated.
In your headers, I don't think there is no need for postman-token, so it should look like,
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Authorization: Bearer eyJh.......N6mbaw",
"Cache-control: no-cache",
"Xero-tenant-id: e8672ad4-ea92-4698-87aa-a69f5b049265"
)
Note the capital first letters.
The other option, easier & more organised one is to use guzzle, as you are using laravel(if > 5.1), you have guzzle already in laravel or can use HTTP Client even.
Using Guzzle
You can use Guzzle to handle curl requests,
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
public function yourFunction()
{
try {
$client = new Client();
$guzzleResponse = $client->get(
'https://api.xero.com/api.xro/2.0/Contacts', [
'headers' => [
"Accept" => "application/json",
"Authorization" => "Bearer eyJh.......N6mbaw",
"Cache-control" => "no-cache",
"Xero-tenant-id" => "e8672ad4-ea92-4698-87aa-a69f5b049265"
],
'allow_redirects' => [
'max' => 10,
],
'connect_timeout' => 30
]);
if ($guzzleResponse->getStatusCode() == 200) {
$response = json_decode($guzzleResponse->getBody(),true);
}
} catch (RequestException $e) {
// you can catch here 400 response errors and 500 response errors
// see this https://stackoverflow.com/questions/25040436/guzzle-handle-400-bad-request/25040600
} catch(Exception $e){
//other errors
}
}
You can refer more about guzzle from guzzle docs
I'm developing a restful API with laravel. Authorization is done using a SECRET_KEY & PUBLIC_KEY as shown in the sample request code below.
How do i retrieve the Authorization header key value from the Request?
I would also appreciate links to any resources that would guide me through better implementation & best practice in creating a Restful API with Laravel.
$curl = curl_init();
$url = "http://api.example.com/v1/users";
$secretKey = env('SECRET_KEY');
$publicKey = env('PUBLIC_KEY');
$firstName = "John";
$lastName = "Doe";
$email = "example#gmail.com";
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"firstName\": \"$firstName\",\r\n \"lastName\": \"$lastName\",\r\n \"email\": \"$email\",\r\n \"secretKey\": \"$secretKey\",\r\n}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
'Authorization: Bearer ' . $publicKey,
),
));
$response = curl_exec($curl);
curl_close($curl);
The request() helper contains everything from the request.
Just dd(request()->header()) to see the full list of the request headers.
You can check the official documentaion : https://laravel.com/docs/7.x/eloquent-resources#introduction
Also you might want to use a tool like Postman to test your API easily.
Am trying to get microsoft outlook mail users profile photos using microsoft 365 graph API.
I have set the following permissions User.Read, User.ReadWrite, User.ReadBasic.All, User.Read.All, User.ReadWrite.All on the apps ends but its not displaying any profile photos whereas the user has Profile Photos already uploaded on her outlook mail account....
Below is the code
<?php
session_start();
echo $acc= $_SESSION['access_token'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.microsoft.com/v1.0/me/photo",
//CURLOPT_URL => "https://graph.microsoft.com/v1.0/me",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//CURLOPT_CUSTOMREQUEST => "GET",
//CURLOPT_POSTFIELDS => "$data",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer $acc"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
print_r($response);
$json = json_decode($response);
$res = $json->{'items'};
if ($err) {
echo "cURL Error #:" . $err;
} else {
//echo $response;
}
?>
You're almost there. What you want to use is
https://graph.microsoft.com/v1.0/me/photo/$value