Migrate this Zend Gdata code from ZF to ZF2 - zend-framework

Sorry for such a bad question but I spend 2 hours without any success. Zend Docs are horrible ...
I have found this Zend_Gdata library and Picasa data API -- loader.php file missing, but its crashing at line 2 Class 'Application\Controller\Zend\Loader\StandardAutoloader' not found, which obviously isn't the correct path.
I am not sure why ZF does not use
...\vendor\ZF2\library\Zend\Loader\
Im using https://github.com/zendframework/ZendSkeletonApplication which is working, but nothing else works out of box with zf2 and all help topics are described incompletely. A mess in my eyes ...
However here is the code.
//Change this for your domain
$domain = 'yourdomain.com';
$email = 'ad...#yourdomain.com';
$passwd = 'p#ssword';
$user = 'jsmith';
$newuserpassword = 'secretp#assword';
//Connect as admin to Google Apps
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Http_Client');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
try {
$client = Zend_Gdata_ClientLogin::getHttpClient($email, $passwd, Zend_Gdata_Gapps::AUTH_SERVICE_NAME);
} catch (Zend_Gdata_App_CaptchaRequiredException $cre) {
echo 'URL of CAPTCHA image: ' . $cre->getCaptchaUrl() . "\n";
echo 'Token ID: ' . $cre->getCaptchaToken() . "\n";
} catch (Zend_Gdata_App_AuthException $ae) {
echo 'Problem authenticating: ' . $ae->exception() . "\n";
}
$gdata = new Zend_Gdata_Gapps($client, $domain);
//Now change the user's password
$updateUser = $gdata->retrieveUser($user);
$updateUser->login->password = $newuserpassword;
$updateUser = $updateUser->save();

There's a new API that you can use even if Zend 1 or 2:
https://github.com/google/google-api-php-client
Here's the page with installation instructions and a getting started:
https://developers.google.com/api-client-library/php/
And here are the available services:
https://github.com/google/google-api-php-client/tree/master/src/Google/Service
Hope it help.

There's no such thing as Zend_Loader in zf2 and the code you have posted is for zf1. If you have the barebones application working, then you will already have the autoloader working correctly (I presume you're using MVC and this code is to go in a controller, not a single file).
If you have the autoloader setup correctly, you also don't need to use Zend_Loader::loadClass.. as they will be autoloaded.
As for Gdata in zf2 - you will need to get the package, which can be found here https://packages.zendframework.com/. Good instructions are here: Zend Framework 2.0.2 YouTube API
Converting the code from zf1 to zf2 should be pretty easy.
However, unfortunately the gdata package is no longer maintained, so you are advised to use https://code.google.com/p/google-api-php-client/

Related

how to call using the rest_client in codeigniter

I am new to CodeIgniter and Rest API. I am trying to implement REST API in CodeIgniter and have used Phil Sturgeon's rest-client and rest-server. I have watched few tutorials and have successfully implemented the Rest-Server part (checking with Chrome's Rest Client APP). But, to implement the Rest-Client, I am having few troubles.
Do I need to have cURL and CodeIgniter's cUrl Library?
If yes, how should I set it up?
I watched this tutorial too by Phil Sturgeon but in this tutorial, he has only used the Rest-Client function to call the Server. But not defined where to put it. Here's the code
function rest_client($id){
$this->load->library('rest', array(
'server' => 'http://localhost/rest/index.php/restgetcontroller/',
));
$user = $this->rest->get('user', array('id' => $id), 'json');
echo $user->name;
}
I am sorry if it is too simple.
Thank You
Edit: I made a Client controller and put there a method to call it. But when I load the page, I get this error.
Call to undefined method CI_Loader::spark()
You can use wherever you need to retrieve a value from your API.
$user will have a value you can use for your purpose.
Basically, you would use the API where you used to use a Model, because now the interaction with the database is made with the API, not from your Controllers directly.
To call RESTful APIs you'll require CURL, there's a library called Guzzlehttp to use CURL more efficiently.
You can use composer to install the library or simply download the zip and require it in your controller.
Example Usage:
try {
$guzzleHttp = new GuzzleHttp\Client([
'verify' => false
]);
$http_response = $guzzleHttp->request('GET', 'http://localhost/rest/index.php/restgetcontroller/');
$response = json_decode($http_response->getBody()->getContents());
return $data;
} catch (Exception $e) {
log_message('error', $e->getMessage());
return false;
}

Cant tell if phpactiverecord is connecting to database

I am writing a web service for a droid app adn trying to use phpactiverecord. I am not using an mvc. I cannot tell whether it is connecting to my database correctly because I don't know what it returns when it fails or if it just can't be used without an mvc.
<?php
require_once '../libs/php-activerecord/ActiveRecord.php';
ActiveRecord\Config::initialize(function($cfg)
{
$cfg->set_model_directory('../libs/php-activerecord/models');
$cfg->set_connections(array(
'development' => 'mysql://username:password#localhost/dbname'));
});
//$json=$_GET ['json'];
$json = file_get_contents('php://input');
$obj = json_decode($json);
//echo $json;
$data = Users::all();
$user_name = $data->user_name;
echo($data);
$password = $data->password;
$posts = array(name =>$user_name,
password =>$password
);
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
?>
It throws a 500 error when I try to go to the page which isn't very helpful.
I figured it out but thanks for trying to help. My hosting server has a wierd structure that required my path to include a bunch of extra stuff that would be unlikely to happen to anyone else... basically my include pat was wrong. turning on error reporting pointed out the problem quickly.

fetch emails with php lib that suports oauth 2

I'm trying to get mail(imap) from google by oauth authorization . I have got the authorization to work, but I can not retrieve the emails. As I understand it should be posible. But Google does not have any api to retrieve mail(?). However, I found the following:
https://developers.google.com/google-apps/gmail/oauth_overview
That says:
Accessing mail using IMAP and sending mail using SMTP is often done
using existing IMAP and SMTP libraries for convenience. As long as
these libraries support the Simple Authentication and Security Layer
(SASL), they should be compatible with the OAuth mechanism supported
by Gmail. In addition to using a library which supports IMAP and SMTP,
developers also will want to use one of the many existing libraries
for handling OAuth
Do anyone know a existing library that i can use and that has some documentation as well. Im using google-api-php-client.
The code
session_start();
ini_set('display_errors',1);
error_reporting(-1);
require_once '../../src/apiClient.php';
$client = new apiClient();
$client->setApplicationName('Mailendar');
$client->setScopes("http://www.google.com/m8/feeds/");
// Documentation: http://code.google.com/apis/gdata/docs/2.0/basics.html
// Visit https://code.google.com/apis/console?api=contacts to generate your
// oauth2_client_id, oauth2_client_secret, and register your oauth2_redirect_uri.
$client->setClientId('secret');
$client->setClientSecret('secret');
$client->setRedirectUri('secret');
$client->setDeveloperKey('secret');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
echo "token is set";
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
$client->revokeToken();
}
if ($client->getAccessToken()) {
MAGIC HAPPENS HERE!!!...but is unkown for me ofc
// The access token may have been updated lazily.
$_SESSION['token'] = $client->getAccessToken();
} else {
$auth = $client->createAuthUrl();
}
if (isset($auth)) {
print "<a class=login href='$auth'>Connect Me!</a>";
} else {
print "<a class=logout href='?logout'>Logout</a>";
}
Thanks!
Google doesnt allow you to retrieve mail with oauth 2.0 at the moment.
Now you can fetch mails using OAuth.
Implemented a simple library.
Delete mail function is not yet added. But you can take a look if it satisfies your need.
Try example. https://github.com/vmuthal/VivOAuthIMAP

Is there any tools to check what is happening in Kohana at Auth?

So, there is problem - I have the order to write web-app on Kohana (no choice) and until yesterday I had no experience with this framework (only in Rails). But anyway I googled and created the authentification on site via net steps:
1: using standart SQL-code in Kohana I created tables for Auth.
2: tutn on ORM and Auth
3: create action login()
public function action_login()
{
$auth = Auth::instance();
if ($auth->logged_in())
{
return $this->request->redirect("welcome/view");
};
if ($_POST)
{
$user = ORM::factory("user");
$status = $auth->login($_POST["username"],$_POST["password"]);
if ($status)
{
$this->request->redirect("welcome/view");
}
else
{
echo "Failed to login";
}
};
$this->response->body(View::factory("login"));
}
But whatever I do I get echo "Failed to login";.
Is there any tools to define what's wrong happening ? Some logs ?
Or, may be, I do something wrong at common....
To answer your question: no there are no obvious tools to dig into Auth. You're best off going into modules/auth and look into the files and dump out information to Kohana::log or Kohana::debug to debug the errors.
I think the problem with your code is that in Kohana 3.2, $_POST is unset. You should use this instead:
$status = $auth->login($this->request->post("username"), $this->request->post("password"));
Should work after that.

Zend Framework google translate usage

I'm trying to translate automatic a string using the google translator! Using Zend_Http_CLient is not able to log in in the application and retrieve the translated words. It returns to me an authentication error.
I google and searched here something about it but had no success. Can someone give a hand on it and tell me where can I find some idea on how to use Zend_Gdata with Google Translate and authenticate at the service?
Thanks a lot, best regard's.
As far as i know Zend GData classes doesn't support google translate yet
source : http://framework.zend.com/manual/en/zend.gdata.html
in the same page you would find a link to this : http://code.google.com/p/gtranslate-api-php/
i had give it a simple try and it seems to be working BUT note the comment in the class declaration
Google requires attribution for their
Language API, please see:
http://code.google.com/apis/ajaxlanguage/documentation/#Branding
hopefully that would help
please provide the error message to make your question more clear
thanks
$client = new Zend_Http_Client('http://ajax.googleapis.com/ajax/services/language/translate', array(
'maxredirects' => 0,
'timeout' => 30));
$client->setParameterGet(array(
'v' => '1.0',
'q' => 'Привет',
'langpair' => 'ru|en'
));
$response = $client->request();
$data = $response->getBody();
$server_result = json_decode($data);
$status = $server_result->responseStatus; // should be 200
$details = $server_result->responseDetails;
$result = $server_result->responseData->translatedText;
echo $result;
die;
There's no official Google Translate, yet. But this translation adapter might help:
http://www.zfsnippets.com/snippets/view/id/35
Also, if you do use this, be sure to CACHE your results! You don't want to hammer the service over and over again for the same translations.