Getting Gmail messages via cURL using PHP - 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

Related

Tweet by PHP, forbidden?

<?php
// Set username and password
$username = 'XXX';
$password = 'XXX';
// The message you want to send
$message = 'is twittering from php using curl';
// The twitter API address
$url = 'https://twitter.com/statuses/update.xml';
// Alternative JSON version
// $url = 'https://twitter.com/statuses/update.json';
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
// check for success or failure
if (empty($buffer)) {
echo 'failure';
} else {
echo 'success';
}
echo $buffer;
?>
Hallo, I have an Problem with the Code above. I have it from an Website. It returns at $buffer, that the Server understood the request but is an forbidden one.
The Twitter API does not support Tweeting with a username and password, you need to use OAuth. Also, the Twitter API URL that you are using in this code is very old (like, about 14 years old). You need to use the official Twitter API. There are some current PHP libraries available.
It's probably disabled within your server's configuration file. You should look into your specific server settings and fiddle around, curl_exec might be disabled by default.
You can check if the setting is enabled using the following command;
<?php phpinfo(); ?>
If you need help with configuring the php.ini file, please check this topic; enable curl_exec on php.ini. You would have to look for a line with disabled_functions, curl_exec might be added there.

using external API in php: Links are incorrect

I'm new to using and API in php.
I have an existing call to an external API, which works and looks like this:
// $id = "227";
// $url = "external-api.com/places/$id?;
// url above works, too
$url = "external-api.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$partner_code:$secret_code");
$result = curl_exec($ch);
curl_close($ch);
echo($result);
On my local server the page is displayed correctly when I enter http://localhost/mytest/mytest.php
The page also displays links and when I click on of them, it shows me the page could not be found. This is because the target is localhost/places/227 and there is no such page.
What I want now is that a call to localhost/places/227 shows a result of an API call to external-api.com/places/227. Just like in the example above, except that this time $url has changed to external-api.com/places/227.
But this shouldn't happen to every link on my website. Just to those links that were retrieved by the API call.
So I think this is what I want: When the link to /places/227 is clicked I want the above php script to be executed again except that the varaible for $url has changed to $url = "external-api.com/places/227;

PHP: cURL or header - faster and better way to send cross domain data and redirect

I create one small API what collect contact form informations from one website and store in database on another website in managment application what I also build.
On website where is contact form is this code:
// collect all fields
$save = array(
'oAuth'=>'{KEY}',
'secret'=>'{SECRET-KEY}',
'category'=>'Category',
'name'=>'Jon Doe',
'email'=>'jon#doe.com',
'phone'=>'123 456 7890',
// ... etc. other fields
);
// made GET request
$fields=array();
foreach($save as $key=>$val){
$fields[]=$key."=".rawurlencode($val);
}
// Set cross domain URL
$url='http://api.mydomain.com/';
// Send informations in database
$cURL = curl_init();
curl_setopt($cURL,CURLOPT_URL, $url.'?'.join("&",$fields));
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($cURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($cURL, CURLOPT_TIMEOUT, 2);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$output=curl_exec($cURL);
curl_close($cURL);
usleep(1000);
// redirect
header("Location: http://somemysite.com/thank-you-page");
session_destroy();
exit;
My question is do to use cURL like now for this or to use header() function to send GET?
I ask because I not use output here and sometimes redirection start before cURL finish request.
What is faster way to send GET info and not lost data?
The redirection will not start before cURL finishes. What may happen is that cURL fails to load you remote page, and you don't handle that error. You should add something like this:
if($output === false || curl_getinfo($cURL, CURLINFO_HTTP_CODE) != 200) {
// Here you handle your error
}
If you are looking for an alternative to load remote webpages, you can set allow_url_fopen to true in your php.ini and then use this simple function instead of cURL:
$output = file_get_contents($url.'?'.join("&",$fields));

login to dropbox with curl ( user login )

Am trying to log in to Dropbox as a user using cURL and PHP.
$ch = curl_init();
$data = array(
't'=>'hxdlvCcN7SKKcfKCvpEO8-s2',
'lhs_type'=>'anywhere',
'login_email'=>'myemail#mail.com',
'password'=>'mypass',
'login_submit'=>1,
'remember_me'=>'on',
'login_submit_dummy'=>'Sign in'
);
// set cURL options and execute
curl_setopt($ch, CURLOPT_URL, "https://www.dropbox.com/login?lhs_type=anywhere");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$request_token_response = curl_exec($ch);
var_dump($request_token_response );
I get 403 page with this text:
It seems you tried to do something we can't verify. Did you log into a different Dropbox account in a different window? Try clicking here to go back to the page you came from, or just go home.
What am I doing wrong?
It is possible, and there's a current class available to make it easy. https://github.com/jakajancar/DropboxUploader/
It's as easy as
require 'DropboxUploader.php';
$uploader = new DropboxUploader('email#address.com', 'password');
The $uploader->loggedin will return if you are logged in or not.
I think you'll find that the 't' value can only be used once. You need to call for a fresh one each time you go to log in.
If you look at the class suggested in the other post, you will see that it does just that.

Send post with curl php

I'm trying to send some parameters for an autentication on a web page this is the code
$url = "http://www.webpage.com/account/submit";
$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $url);
curl_setopt($handler, CURLOPT_POST,true);
curl_setopt($handler, CURLOPT_POSTFIELDS, "username=user#webmail.com&remember=true&password=123456");
$response = curl_exec ($handler);
curl_close($handler);
When I run this script shows nothing, Im trying to autenticate me succesfully into a website without the HTML form . . . there is another way to do this ???
Ofcourse it shows nothing. You would at least have to echo something (and likely do something with $response to actually check the result of the login). Also, make sure error reporting is enabled.

Categories