Gmail API Not Reading Full email - php

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

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

how to make multiple api call at the same time?

CoinMarketCap Api offer a lot of data by making calls to different links. Every time you make a call it cost 1 credit and of course, if that call return 5,000 coins then it cost 25 credits. So, I can't make call to different link every minute. How can I make a call to at least 4 links such as:
https://pro-api.coinmarketcap.com/v1/cryptocurrency/trending/latest
https://pro-api.coinmarketcap.com/v1/cryptocurrency/trending/gainers-losers
https://pro-api.coinmarketcap.com/cryptocurrency/listings/latest?limit=5000
https://pro-api.coinmarketcap.com/v2/cryptocurrency/info
and here is the code that CoinMarketCap offer and it works (tested):
$url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest';
$parameters = [
'start' => '1',
'limit' => '5000',
'convert' => 'USD'
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: *********-****-****-****-***********'
];
$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
you can use the curl_multi_init to handle the multiple curl request asynchronously. you can read here more about multi curl

How to get emails sent from gmail API

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.

PHP CURL POST method is giving 500 response code

Hi I know that there are similar questions already in there but I have ruled them out.
So I am trying to make a simple POST method with curl and keep getting error 500. Have I missed something ?
// Get cURL resource
$curl = curl_init();
//POST request body
$post_data = array(
'subscription_uuid' => $subscription_uuid,
'merchant' => $merchant_id
);
echo "JSON in POSTFIELDS:" . json_encode($post_data, JSON_PRETTY_PRINT) . "\n";
// Set Headers, endpoint and option to output response as string
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://subscriptions-jwt.fortumo.io/subscriptions/cancel',
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => array(
'Content Type: application/json' ,
'Authorization: Bearer' . " " . $jwt
),
CURLOPT_POSTFIELDS => json_encode($post_data)
));
// Send the request & save response
$unsubscribe_response = curl_exec($curl);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
//Request URL:
echo "Unsubscribe Request URL:\n" . curl_getinfo($curl, CURLINFO_EFFECTIVE_URL) . "\n";
echo "Error Code:" . $statusCode . "\n";
And here is the response I get from that code block(simplified this using echo-s):
JSON in POSTFIELDS:{
"subscription_uuid": "<<MY-TOKEN>>",
"merchant": "<<MY-TOKEN>>"
}
Unsubscribe Request URL:
https://subscriptions-jwt.fortumo.io/subscriptions/cancel
Error Code:500
Wierd thing is that using exactly same set of headers, JSON postfields and URL in a tool such as Advanced REST client everything works fine and I get 200 response with no problem.
Something is wrong with my code. Can anyone please spot the issue? Thanks in advance!

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.

Categories