gmail unread email count using curl - php

I created a script that can get the unread email list as a feed from the gmail here is my code
<?php
//function to get unread emails taking username and password as parameters
function check_email($username, $password)
{
//url to connect to
$url = "https://mail.google.com/mail/feed/atom";
// sendRequest
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);
//returning retrieved feed
return $curlData;
}
//making page to behave like xml document to show feeds
header('Content-Type:text/xml; charset=UTF-8');
//calling function
$feed = check_email("username", "password");
echo $feed;
?>
the output getting like this
<?xml version="1.0" encoding="UTF-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#">
<title>Gmail - Inbox for suneth2#gmail.com</title>
<tagline>New messages in your Gmail Inbox</tagline>
<fullcount>1282</fullcount>
<link rel="alternate" href="http://mail.google.com/mail" type="text/html" />
<modified>2012-08-01T12:33:48Z</modified>
<entry>
<title>eBCS Pro 1 August 2012</title>
<summary>bcs logo eBCS Pro 1 August 2012 Video interview Olympic IT The Met Police's director of IT, Steve</summary>
<link rel="alternate" href="http://mail.google.com/mail?account_id=test#gmail.com&message_id=138e21a3404cc7b2&view=conv&extsrc=atom" type="text/html" />
<modified>2012-08-01T12:12:44Z</modified>
<issued>2012-08-01T12:12:44Z</issued>
<id>tag:gmail.google.com,2004:1409100718455703474</id>
<author>
<name>eBCS Newsletter</name>
<email>e-bulletin#lists.bcs.org.uk</email>
</author>
</entry>
<entry>
so want to read the
<fullcount>1282</fullcount>
tag
when you pass the username and password to this function it can show the email list,
I need to get only the message count, is there any way to catch or count the items

this is the answer, i read the xml tag using php
$xmlobjc = new SimpleXMLElement($feed);
echo $xmlobjc->fullcount[0];
and replace the header with
header('Content-Type:text/html; charset=UTF-8');

This works too:
curl -u $(cat username):$(cat password) --silent 'https://mail.google.com/mail/feed/atom' | sed -n 's:.*<fullcount>\(.*\)</fullcount>.*:\1:p'

Related

How can I render data from Tally to server application?

I want to render data from tally to my application and vise versa using tally API. When I am using it locally, it is working fine, but I want a solution that when my application is on server connects to tally.
This is the code that I am using with tally API
<?php
$requestXML ='<ENVELOPE>
<HEADER>
<VERSION>1</VERSION>
<TALLYREQUEST> EXPORT</TALLYREQUEST>
<TYPE>COLLECTION</TYPE>
<ID> RTSAllVouchers_FilterForVchNoAndVchType</ID>
</HEADER>
<BODY>
<DESC>
<STATICVARIABLES>
<SVEXPORTFORMAT>$$SysName:XML</SVEXPORTFORMAT>
<!-- TODO : Specify the VoucherNo -->
<RTS_KEY>VCH-INV-1</RTS_KEY>
<!-- TODO : Specify the VoucherType here -->
<RTS_VOUCHERTYPENAME>Sales</RTS_VOUCHERTYPENAME>
</STATICVARIABLES>
<TDL>
<TDLMESSAGE>
<!-- Retrieve all Vouchers for specified VoucherNo and VoucherType -->
<COLLECTION NAME="RTSAllVouchers_FilterForVchNoAndVchType" ISINITIALIZE="Yes">
<TYPE>Voucher</TYPE>
<FETCH>ALLLEDGERENTRIES.*</FETCH>
<FETCH>ALLINVENTORYENTRIES.*</FETCH>
<FILTER>RTS_FilterForVchNoAndVchType</FILTER>
</COLLECTION>
<VARIABLE NAME="RTS_KEY">
<TYPE> String</TYPE>
</VARIABLE>
<VARIABLE NAME="RTS_VOUCHERTYPENAME">
<TYPE>String</TYPE>
</VARIABLE>
<SYSTEM TYPE="FORMULAE" NAME="RTS_FilterForVchNoAndVchType">
$VoucherNumber = $$String:##RTS_KEY and $VoucherTypeName = $$String:##RTS_VOUCHERTYPENAME
</SYSTEM>
</TDLMESSAGE>
</TDL>
</DESC> </BODY></ENVELOPE>';
$server = 'http://localhost:9000/';
$headers = array( "Content-type: text/xml","Content-length:".strlen($requestXML) ,"Connection: close");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);if(curl_errno($ch)){
print curl_error($ch);
echo " something went wrong..... try later";
}else{
echo " request accepted";
curl_close($ch);
$object = simplexml_load_string($data);print_r($object);
}
?>
$server = 'http://localhost:9000/';
This is the port of the tally its working fine in local.
I heard of the "Tally on cloud" that tally provides, can I render my data from my application to tally using that If yes, then how?

post xml using curl

i'm trying to post xml via HTTP POST method...the code works fine and displays the desired message.."Data received successfully"..problem is, on looking it up on the backend, the data is not posted on the Agenda Creative backend..where could i have messed up??thanks in advance.
<?php
function sendXmlOverPost($url, $xml) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// For xml, change the content-type.
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // ask for results to be returned
// Send to remote and return data to caller.
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$postargs = '<?xml version="1.0" encoding="utf-8"?>
<Vacancy>
<Poster>LOGICMELON</Poster>
<Action>Add</Action>
<Username>agendacreative</Username>
<Password>A12!.ndasa55</Password>
<JobReference>AC Test 1</JobReference>
<JobTitle>AC Test 1</JobTitle>
<JobType>F</JobType>
<JobHours>F</JobHours>
<Industry>137</Industry>
<SalaryCurrency>GBP</SalaryCurrency>
<SalaryFrom>100.0000</SalaryFrom>
<SalaryTo>200.0000</SalaryTo>
<SalaryPer>D</SalaryPer>
<SalaryBenefits>Benefits</SalaryBenefits>
<Salary>£100 - 200 per day + Benefits</Salary>
<JobLocation>4</JobLocation>
<JobCountry>GB</JobCountry>
<JobDescription><p>LOGIC MELON TEST VACANCY - PLEASE DO NOT APPLY!!!</p><p>Testing the initial integration to Agenda Creative to make sure that the template is compiled properly andt hat the fields are getting sent through correctly.</p><p>THIS IS A LOGIC MELON TEST VACANCY - PLEASE DO NOT APPLY FOR THIS ROLE AS IT DOES NOT EXIST!!!</p><p>&euro; - Testing Euro Sign</p><p>&pound; - Testing Pound Sign</p></JobDescription>
<ContactName>Logic Melon Support</ContactName>
<ContactEmail>jade.sinclair.1267EC41.0#applythis.net</ContactEmail>
<ApplicationEmail>jade.sinclair.1267EC41.0#applythis.net</ApplicationEmail>
<ApplicationURL></ApplicationURL>
<WeeksToAdvertise>1</WeeksToAdvertise>
</Vacancy>
';
$request = "http://agendacreativerecruitment.co.uk/feed.php";
echo (sendXmlOverPost($request, $postargs));
?>
I don't know about Agenda Creative. But try something like this?
curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml' => $xml));
where 'xml' is the post field your xml data should be in.

PHP cURL XML example sometimes returns blank, most of the time

Here are my code samples:
function xmlPostStore($post_xml, $url, $port) {
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number
curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_xml); // add POST fields
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
what i'm sending:
$XML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<SendConfig>
<Request>
<key>passphrase</key>
</Request>
</SendConfig>";
$reply = xmlPostStore($XML, "http://www.urlhere.com/test.php", "80");
test.php is just a simple XML return:
echo "<?xml version='1.0' encoding='utf-8'?>
<response>
<config>
<test>it works</test>
</config>
</response>";
When I test this on one server, it works 100% of the time. I receive a response and there's no issues.
When I test it on our main server, it returns back nothing, most of the time, about 98% of the time it's blank. Without any code changes, it will randomly work and randomly stop. I'm stumped.
Turns out what I believed to be opening the firewall to allow incoming and outgoing connections was only allowing incoming, not outgoing. I also resolved a DNS error under resolv.conf and things are now working correctly.

Getting Gmail messages via cURL using PHP

There's tons of info on logging in to Gmail and displaying the inbox and getting contacts etc, but I cannot figure out how to get the email itself into a variable so I can do stuff with it in PHP.
Here's what I have:
function inbox($username, $password){
$url = "https://mail.google.com/mail/feed/atom";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}
//calling the function
$em = "email#gmail.com";
$pw = "pass";
$feed = inbox($em, $pw);
$x = new SimpleXmlElement($feed);
echo "<ul>";
foreach($x->entry as $msg){
//extracting the link to the message from xml
$href = $msg->link->attributes()->href;
//create a link to the message and display title, summary
echo "<li>".$msg->title."<br />".$msg->summary."</li>";
}
echo "</ul>";
Now when I click on the link I just created it just opens the message in gmail. I want to access the html of the message in a string/variable. I've tried all kinds of things. I've tried forwarding the message link to another page to open in curl but instead of showing me the message google sends some html with yet another link to the message. If the link is clicked in the browser it again, opens in gmail, but if I try to curl a third time to this link it shows me a blank page.
The point is, my work server doesn't have imap/pop enabled and cURL is the last think I know of that can accomplish this.
I ended up using imap remotely then cURLing it back to the server in question. I've determined that gmail doesn't allow messages to be sent via cURL, it's one of those google things, like where they don't allow frames, etc

spell check using google.com/tbproxy/spell

I am using google spell check in my project and doing everything as suggested in the blogs but it doesn't seem to work for me. Can you please look at this and tell me what I am doing incorrect.
In my JavaScript:
function makeRequest(parameters, svalue) {
console.log("dv-- inside makerequest 1");
console.log("svalue =", svalue);
http_request.onreadystatechange = GetResponse;
http_request.open('POST', 'http://mysite.com/Project/spellify.php?lang=en', true);
data = '<?xml version="1.0" encoding="utf-8" ?>';
data +='<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>';
data += svalue;
data += '</text></spellrequest>';
console.log("data =", data)
http_request.send(data);
}
function GetResponse(){
console.log("dv-- inside GetResponse-1 http_request.readyState =", http_request.readyState)
if (http_request.readyState == 4) {
console.log("dv-- inside GetResponse-2 http_request.status =", http_request.status)
if (http_request.status == 200) {
http_response = http_request.responseText;
console.log("dv --http_response =", http_response)
}
else {
console.log('There was a problem with the request' + '& http_request.status =' + http_request.status );
}
}
}
My PHP Code: spellify.php
$url="http://www.google.com/tbproxy/spell?lang=en&hl=en";
//$data = file_get_contents('php://input');
$data = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>hellow </text></spellrequest>';
$data = urldecode($data);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);
print $contents;
?>
I tried hardcoding the data in my php to make sure data was passed correctly and that is not an issue.
Firefox give an error:
Error: unclosed token
Source File: http://mysite.com/Project/spellify.php?lang=en
Line: 5, Column: 183
Source Code:
$data ='<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>hello thsi is graet</text><`/spellrequest>';
Chrome doesn't give any error, but show below in console:
dv-- inside makerequest 1
spellify.js:419svalue = hello worlda
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 1
spellify.js:427data = <?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>hello worlda </text></spellrequest>
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 2
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 3
spellify.js:432dv-- inside GetResponse-1 http_request.readyState = 4
spellify.js:435dv-- inside GetResponse-2 http_request.status = 200
spellify.js:438dv --http_response = <?php
$url="http://www.google.com/tbproxy/spell?lang=en&hl=en";
// $data = file_get_contents('php://input');
$data = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="0" ignoreallcaps="0"><text>hello thsi is graet</text></spellrequest>';
$data = urldecode($data);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);
print $contents;
?>
Please suggest what could be corrected to make this work.
Thanks
You are using code from spellify.com, some sort of credit to original developer is appreciated.
1) You have incorrectly modified spellify.php file.
2-a) Spellify basically prepare http-request and parse http-response via Javascript src/spellify.js, so its incorrect to make any changes in spellify.php, rather you should check if you are referencing spellify/src/scriptaculous.js and spellify/src/prototype.js correctly.
2-b) Under same spellify.js you have hardcoded following line incorrectly;
http_request.open('POST', 'http://mysite.com/Project/spellify.php?lang=en', true);
It should be as follows;
http_request.open('POST', 'http://mysite.com/Project/spellify.php?lang=en'+parameters, true);
3) The time when spellify.php was written, there might have no issue with SSL verification, but now it wont return anything unless you ignore the SSL Verification, add following lines in spellify.php, before $contents = curl_exec ($ch);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Here is sample implementation.
http://amitgandhi.in/2013/01/25/spell-check-utility-using-jquery-and-google-api/
You can add words dictionary. Variable $.SpellChecker.dicWords is a
javascript array which maintain this. You can use your database table
populate this array with existing words. Currently this app is not
connected to database so if you add your words then lifetime of words
would only till page reloads.
Uses google.com/tbproxy/spell api call (Written in PHP)

Categories