How to get emails sent from gmail API - php

I have a script that get incoming emails to my gmail but now i also want to get emails that i send via email.
Here is the code that i have to get incoming emails
Please let me know what i need for outgoing emails as well
At the moment this code runs via pub sub on console.google.com
$ch = curl_init('https://www.googleapis.com/gmail/v1/users/me/messages?labelIds=Label_56&maxResults=10');
curl_setopt_array($ch, array(
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '. $tokenval,
'Content-Type: application/json',
),
));
// execute request and get response
$result = curl_exec($ch);
$allmessages = json_decode($result);
$allmessage = $allmessages->messages;
for($i=0;$i<count( $allmessage);$i++)
{
//echo $allmessage[$i]->id;
$checkoldmsgid = $this->Customer_m->getCount('messages',array('massageid'=>$allmessage[$i]->id ));
if( $checkoldmsgid ==0)
{
$ch = curl_init('https://www.googleapis.com/gmail/v1/users/me/messages/'.$allmessage[$i]->id);
curl_setopt_array($ch, array(
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '. $tokenval,
'Content-Type: application/json'
),
));
// execute request and get response
$resultm = curl_exec($ch);
$msgval = json_decode($resultm);
$sendernum =explode('#',$msgval->payload->headers[19]->value);
$recivernum =explode('#',$msgval->payload->headers[0]->value);
$createdat = date('Y-m-d H:i:s',strtotime($msgval->payload->headers[18]->value));
$massage = $msgval->snippet;
$single_message = $service->users_messages->get("me", $allmessage[$i]->id);
$payload = $single_message->getPayload();
$body = $payload->getBody();
$FOUND_BODY = $this->decodeBody($body['data']);

You'll probably want to use the SENT system label (instead of the Label_56 one) in your curl_init() call:
SENT
Applied automatically to messages that are sent with drafts.send
or messages.send, inserted with messages.insert and the
user's email in the From header, or sent by the user through the
web interface.

Related

Coinmarketcap API Call with PHP

I tried my first API call but something is still wrong. I added my API-Key, choose the symbol and tried to echo the price. But it is still not valid. But my echo is still 0. Maybe someone show me what i did wrong. Thank you!
<?php
$coinfeed_coingecko_json = file_get_contents('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?symbol=ETH');
$parameters = [
'start' => '1',
'limit' => '2000',
'convert' => 'USD'
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: XXX'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
print_r(json_decode($response)); // print json decoded response
curl_close($curl); // Close request
$coinfeed_json = json_decode($coinfeed_coingecko_json, false);
$coinfeedprice_current_price = $coinfeed_json->data->{'1'}->quote->USD->price;
?>
<?php echo $coinfeedde = number_format($coinfeedprice_current_price, 2, '.', ''); ?>
API Doc: https://coinmarketcap.com/api/v1/#operation/getV1CryptocurrencyListingsLatest
There is a lot going on in your code.
First of all $url was not defined
Second of all you made two requests, one of which I have removed
Third; you can access the json object by $json->data[0]->quote->USD->price
Fourth; I removed the invalid request params
I have changed a few things to make it work:
$url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: ___YOUR_API_KEY_HERE___'
];
$request = "{$url}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
$json = json_decode($response);
curl_close($curl); // Close request
var_dump($json->data[0]->quote->USD->price);
var_dump($json->data[1]->quote->USD->price);

Discord/PHP - Assign user role

Is it possible, using PHP/Discord API to assign a role to a user?
I currently use OAuth2 for my website visitors to register with Discord. I would then later want to assign the registered website users with a role on a discord server when an admin have reviewed their membership, and I've already configured a bot that have been granted the required permissions on the server.
I have been looking at the documents - but unfortunately they do not really give me the dummy-proof guidance I need. https://discordapp.com/developers/docs/resources/guild#modify-guild-member
Could I do this with Curl maybe?
https://discord.com/developers/docs/resources/guild#add-guild-member-role
You can make a cURL call to this API-endpoint, passing a bot token who has the MANAGE_ROLES permission. Here is a code snippet:
$authToken = "bot_token";
$guildid = "guild_id";
$userid = "user_id";
$roleid = "role_id";
$url = "https://discordapp.com/api/v6/guilds/" . $guildid . "/members/" . $userid . "/roles/" . $roleid;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array(
'Authorization: Bot '.$authToken,
"Content-Length: 0"
),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_SSL_VERIFYPEER => 0
));
$response = curl_exec($ch);
//It's possible to output the response at this place for debugging, so remove the comment if needed
/*
print $response;
print "<pre>";
print_r(json_decode($response));
print "</pre>";
*/
curl_close($ch);

Gmail API Not Reading Full email

Ok so i am using Pub/Sub subscrition and using Push into an endpoint URL to read email from gmail.
It works perfect for short emails but once and email is over mayber200 characters it only reads first 100 or so characters.
Here is the screen shot on my google api console
and here is the code in the receiver file
$ch = curl_init('https://www.googleapis.com/gmail/v1/users/me/messages?labelIds=Label_56&maxResults=5');
curl_setopt_array($ch, array(
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '. $tokenval,
'Content-Type: application/json',
),
));
// execute request and get response
$result = curl_exec($ch);
$allmessages = json_decode($result);
$allmessage = $allmessages->messages;
for($i=0;$i<count( $allmessage);$i++)
{
$checkoldmsgid = $this->Customer_m->getCount('messages',array('massageid'=>$allmessage[$i]->id ));
if( $checkoldmsgid ==0)
{
$ch = curl_init('https://www.googleapis.com/gmail/v1/users/me/messages/'.$allmessage[$i]->id);
curl_setopt_array($ch, array(
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '. $tokenval,
'Content-Type: application/json'
),
));
// execute request and get response
$resultm = curl_exec($ch);
$msgval = json_decode($resultm);
$sendernum =explode('#',$msgval->payload->headers[19]->value);
$recivernum =explode('#',$msgval->payload->headers[0]->value);
$createdat = date('Y-m-d H:i:s',strtotime($msgval->payload->headers[18]->value));
Is there a line of code that i need to enter to read full emails?
According to the documentation, you can pass an optional parameter for the format:
Acceptable values are:
"full": Returns the full email message data with body content parsed in the payload field; the raw field is not used. (default)
"metadata": Returns only email message ID, labels, and email headers.
"minimal": Returns only email message ID and labels; does not return the email headers, body, or payload.
"raw": Returns the full email message data with body content in the raw field as a base64url encoded string; the payload field is not used.
Try changing your endpoint to include the format parameter:
$id = $allmessage[$i]->id;
$endpoint = "https://www.googleapis.com/gmail/v1/users/me/messages/$id?format=full";
$ch = curl_init($endpoint);

Coinbase - php curl sendmoney - invalid signature error

I am trying coinbase api to send and get money and going to use in game,on running below code for sending money getting invalid signature error, not sure where I am wrong. I tried getting account detail, which is working fine and I am able to get account details.
<?php
$API_VERSION = '2016-02-01';
$curl = curl_init();
$timestamp = json_decode(file_get_contents("https://api.coinbase.com/v2/time"), true)["data"]["epoch"];
$req = "/v2/accounts/:account_id/transactions";
$url = "https://api.coinbase.com".$req;
$cle = "xxxxxxx";
$secret = "xxxxxxxx";
$params=['type'=>'send', 'to'=>'xxxxxxxxxx', 'amount'=>0.0001, 'currency'=>'BTC'];
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_USERAGENT, 'local server',
CURLOPT_POSTFIELDS => json_encode($params),
CURLOPT_HTTPHEADER => array(
"CB-VERSION:" . $API_VERSION,
"CB-ACCESS-SIGN:" . hash_hmac('sha256', $timestamp."GET".$req, $secret),
"CB-ACCESS-KEY:" . $cle,
"CB-ACCESS-TIMESTAMP:" . $timestamp,
'Content-Type: application/json'
),
CURLOPT_SSL_VERIFYPEER => false
));
$rep = curl_exec($curl);
curl_close($curl);
print_r($rep);
?>
In the $req URL, you need to replace :account_id with an actual account ID such as 3c04e35e-8e5a-5ff1-9155-00675db4ac02.
Most importantly, since this is a post request, the OAuth signature needs to include the payload (POST data) in the signature.
hash_hmac('sha256', $timestamp."POST".$req.json_encode($params), $secret),
When I encountered this error, it ended up being the account id, which is different for each of your currency accounts. Spent way too much time trying to figure out what was wrong with my signature... Anyways, I'd definitely try that out as GETs worked for me, but every other request type ended up with the invalid signature error.

How to get/set Header in Rest Server API?

i have a chriskacerguis Rest Server ,that listen for a client request as usually an API Server do.
base on client request i want to send/response some data to client in header only.
my questions are:
how do i access Client header first? then
how do i set Header in Rest Server?
This is how i send a request to REST SERVER:
function request_curl($url = NULL) {
$utc = time();
$post = "id=1&CustomerId=1&amount=2450&operatorName=Jondoe&operator=12";
$header_data = array(
"Content-Type: application/json",
"Accept: application/json",
"X-API-KEY:3ecbcb4e62a00d2bc58080218a4376f24a8079e1",
"X-UTC:" . $utc,
);
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => 'http://domain.com/customapi/api/clientRequest',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $header_data,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post,
CURLOPT_HEADER => 1,
);
curl_setopt_array($ch, $curlOpts);
$answer = curl_exec($ch);
// If there was an error, show it
if (curl_error($ch)) {
die(curl_error($ch));
}
curl_close($ch);
echo '<pre>';
print_r($answer);
echo '</pre>';
}
Below is my REST SERVER function that listen request and will response a header:
public function clientRequest_post() {
// Getting Post Data
$entityBody = file_get_contents('php://input', 'r');
$this->response($entityBody,200);
//getting header data ,no idea
}
May be try php function getallheaders() which will fetch all the header data for you. If you want to convert it into array, use foreach.
So this will get you the header data and will convert it into array
$headers=array();
foreach (getallheaders() as $name => $value) {
$headers[$name] = $value;
}
Now if you want to get body and convert it into array as well
$entityBody = file_get_contents('php://input', 'r');
parse_str($entityBody , $post_data);
The final function will look something like this...
public function clientRequest_post() {
$headers=array();
foreach (getallheaders() as $name => $value) {
$headers[$name] = $value;
}
$entityBody = file_get_contents('php://input', 'r');
parse_str($entityBody , $post_data);
$this->response($entityBody, 200);
}
Btw, I assume $this->response($entityBody,200); will generate the response for you. Best of luck with it

Categories