I try to retrieving data from my firebase using kreait php but it's always return NULL even though the data is exist in my firebase database.
here is my firebase
here is my code
connection.php
<?php
require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
$factory = (new Factory)->withServiceAccount('./includes/secret/db-wind-2ceca-672d70f2f4a8.json')->withDatabaseUri('https://db-wind-2ceca-default-rtdb.asia-southeast1.firebasedatabase.app');
$auth = $factory->createAuth();
$database = $factory->createDatabase();
?>
home.php
<?php
session_start();
if($_SESSION['user'] == false) {
header('Location:index.php');
}
set_include_path(__DIR__);
require('includes/koneksi.php');
$reference = $database->getReference('db-wind-2ceca-default-rtdb/Crosswind1/');
$value = $reference->getValue();
var_dump($value);die; // return: null
?>
Remove the db-wind-2ceca-default-rtdb/ prefix from your getReference() call and it should work. That's the name of the database, which is already set through the withDatabaseUri() call.
well done bro, actually I have never used firebase in a php project. But i'll try to help.
I think you should modify in your connection.php
from this
require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
$factory = (new Factory)->withServiceAccount('./includes/secret/db-wind-2ceca-672d70f2f4a8.json')->withDatabaseUri('https://db-wind-2ceca-default-rtdb.asia-southeast1.firebasedatabase.app');
$auth = $factory->createAuth();
$database = $factory->createDatabase();
to this
require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__ . '/includes/secret/db-wind-2ceca-672d70f2f4a8.json');
$factory = (new Factory)->withServiceAccount($serviceAccount)->withDatabaseUri('https://db-wind-2ceca-default-rtdb.asia-southeast1.firebasedatabase.app')->create();
$database = $factory->getDatabase();
Right now I am trying to create a user in ejabberd using PHP. I have checked many sources From Git. As well as I tried to follow the same answer as given in this question
But sometimes user is going to be created, and sometimes not, so any debug points.
https://github.com/fabiang/xmpp
As per my knowledge there is issue related to time limit between create user one after another in ejabberd.
For example :
My script is going to run and create a user at a time.
Then I have to wait around 8-9 minutes, then I am try executing my script then it will create another user for me on first try but IF I try to create another user just after some time.
Is that possible?
CODE :
<?php
require 'vendor/autoload.php';
error_reporting(-1);
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Fabiang\Xmpp\Options;
use Fabiang\Xmpp\Client;
use Fabiang\Xmpp\Protocol\Register;
use Fabiang\Xmpp\Protocol\Roster;
use Fabiang\Xmpp\Protocol\Presence;
use Fabiang\Xmpp\Protocol\Message;
$logger = new Logger('xmpp');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
$hostname ='192.168.1.12';
$port = 5222;
$connectionType = 'tcp';
$address = "$connectionType://$hostname:$port";
$adminUsername ="admin";
$adminPassword = "admin";
$name ="87878";
$name ="87890";
$password = "maddy";
$options = new Options($address);
$options->setUsername($adminUsername)
->setPassword($adminPassword)
->setContextOptions(['ssl' => ['verify_peer' => false,'verify_peer_name' => false]]);
$client = new Client($options);
$client->connect();
$register = new Register($name, $password);
$client->send($register);
$client->disconnect();
?>
Any suggestions?
I dont ask many questions as I like to research for myself but this has me stumped.
I have an existing Codeigniter2(CI) application and am trying to integrate an existing API for a payment system (MangoPay). I have added it as a library and also preloaded it in autoload.php, it is being included with no errors.
My question is about setting up the class structure and addressing the class from my application.
Now, if you were to get this working from a plain old PHP file, the code would look like this (and btw it works on my machine with no issue from a plain php file)
<?php
require_once('../vendor/autoload.php');
$mangoPayApi = new MangoPay\MangoPayApi();
$mangoPayApi->Config->ClientId = 'user_id';
$mangoPayApi->Config->ClientPassword = 'password_here';
$mangoPayApi->Config->TemporaryFolder = 'c:\\wamp\\tmp/';
$User = new MangoPay\UserNatural();
$User->Email = "test_natural#testmangopay.com";
$User->FirstName = "Bob";
$User->LastName = "Briant";
$User->Birthday = 121271;
$User->Nationality = "FR";
$User->CountryOfResidence = "ZA";
$result = $mangoPayApi->Users->Create($User);
var_dump($result);
?>
So, I have created a new class in the libraries folder and if i was to var_dump() the contents of mangoPayApi as below, it throws all kinds of stuff which proves that it is working (ie no PHP errors).
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once('/vendor/autoload.php');
class MangoPayService {
private $mangoPayApi;
private $user;
public function __construct()
{
$this->mangoPayApi = new MangoPay\MangoPayApi();
$this->mangoPayApi->Config->ClientId = 'user_id_here';
$this->mangoPayApi->Config->ClientPassword = 'password_here';
$this->mangoPayApi->Config->TemporaryFolder = 'c:\\wamp\\tmp/';
//var_dump($mangoPayApi);
}
I thought I could just write a method in the class like this
function add_user(){
//CREATE NATURAL USER
$this->user = new user();
$user->Email = 'test_natural#testmangopay.com';
$user->FirstName = "John";
$user->LastName = "Smith";
$user->Birthday = 121271;
$user->Nationality = "FR";
$user->CountryOfResidence = "ZA";
$add_userResult = $this->mangoPayApi->Users->Create($user);
var_dump($add_userResult);
}
and acces it in my application like
<?php echo $this->mangopayservice->add_user() ?>
But i get errors Fatal error: Class 'user' not found in C:\wamp\www\mpapp\application\libraries\MangoPayService.php on line 25 (which is this->user = new user(); this line)
Can anyone explain how to correctly set up this scenario and how to integrate correctly with the API.
if I can get something to create a user simply when a page is opened, I think I can work it from there using the solution as a roadmap.
I will be writing all the integration code once I understand how to make this work.
Thank in advance
MangoPay requires a NaturalUser class. You try to instantiate a user class.
Simply replace your first line of the add_user function with :
$user = new MangoPay\UserNatural();
I have been looking for the help to configure SDK for my Allocator.
I have already followed the steps given in the documentation.
https://github.com/MyAllocator/myallocator-ota-php
From the above documentation I have installed myAllocator using composer and I can see MyAllocator directory inside vendor.
Moving further I tried to copy the code from MaReceiver.php to my controller but this does not work out.
I have also checked how to configure the facebook sdk but this also does not help me to get a good idea to work out with MyAllocator SDK.
Again now tried to created a separate module in zf2 but I did not get any success.
It would be really helpful if anyone can guide me of give me any reference for SDK configuration in zend framework 2.
Please find the code
$request = $this->getRequest();
if ($request->isPost()) {
$data = $request->getPost('id');
$propertyId = $request->getPost('pand_id');
$password = $data['pand_password'];
$guid = empty($data['guid'])? '' : $data['guid'];
$verb = empty($data['verb'])? 'SetupProperty' : $data['verb'];
$booking_id = empty($data['booking_id'])? '' : $data['booking_id'];
$myallocator_pid = empty($data['pid'])? '' : $data['pid'];
// Instantiate backend interface that implements MaInboundInterface
$interface = new MaInboundInterfaceStub();
$interface->mya_property_id = $myallocator_pid;
$interface->ota_property_id = $propertyId;
$interface->verb = $verb;
$interface->guid = $guid;
$interface->shared_secret = 'xxxx';
$interface->ota_regcode = $password;
$interface->booking_id = $booking_id;
$interface->mya_property_id = 'M not in else ';
$router = new \MyAllocator\phpsdkota\src\Api\Inbound\MaRouter($interface);
// Process request
$post_body = file_get_contents('php://input');
$post_body = json_encode($interface);
$response = $router->processRequest($post_body);
header('Content-Type: application/json');
echo json_encode($response);exit;
}
With the above code am successfully able to call the function and work perfectly fine with the value on interface object
But now the problem is that am not able to get the request parameter from MyAllocator
ive been using Zend Gdata for a while now, and today im getting an error of
Notice: Undefined offset: ClientLogin.php on line 150
via php, this has been working for a while now, and today without changing anything it stopped working, im guessing some deprecated service on behalf of google with the zend gdata maybe the Zend_Gdata_ClientLogin::getHttpClient( ) method or something, can any one confirm or help me with this issue. the code im using to connect is as follows:
require_once('Zend/Loader.php');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Docs');
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
require_once 'Zend/Gdata.php';
require_once 'Zend/Gdata/AuthSub.php';
require_once 'Zend/Gdata/Spreadsheets.php';
require_once 'Zend/Gdata/Spreadsheets/DocumentQuery.php';
require_once 'Zend/Gdata/Spreadsheets/ListQuery.php';
require_once 'Zend/Loader.php';
$sourceUser = "myemail";
$sourcePass = "mysuperawesomepassword";
$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$sourceClient = Zend_Gdata_ClientLogin::getHttpClient($sourceUser, $sourcePass, $service);
$connection = new Zend_Gdata_Spreadsheets($sourceClient);
i am using the zend gdata with the google spreadsheets
also the error points specifically to this line
$sourceClient = Zend_Gdata_ClientLogin::getHttpClient($sourceUser, $sourcePass, $service);
as i said, i was using this for a while now, and nothing has changed on my end
I uses ClientLogin for a server to server application.
needed to switch to oAuth2 in a hurry today. I merged some examples I found to get the authorisation token using a 'Service account'
function get_token() {
$client_email = '0-1.apps.googleusercontent.com';
$client_email = '0-1#developer.gserviceaccount.com';
$private_key = file_get_contents('abc.p12');
$scopes = array('https://spreadsheets.google.com/feeds');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key,
'notasecret', // Default P12 password
'http://oauth.net/grant_type/jwt/1.0/bearer' // Default grant type
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$tokenData = json_decode($client->getAccessToken());
return $tokenData->access_token;
}
I needed to use the developer email not the app email, same email must be added as a user to the spreadsheet
the generated token can then be used with php-google-spreadsheet-client
$accessToken = get_token();
$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
$spreadsheet = $spreadsheetFeed->getByTitle('Hello World');
In the end I'm ending with something like this(very raw code so far, but enought for those who's seaching for solution. You need php google spreadsheet client of https://github.com/asimlqt/php-google-spreadsheet-client). Thats tiny example of inserting one row to my spreadsheet (sorry about my code, but showing just working example)
Thanks for bram brambring showing better way to authorization - answer by bram brambring
<?php
/*
* Google Spreadsheet class to work with google spreadsheets obviously ;D [using OAuth 2.0, as Zend Gdata is not anymore working]
*/
require_once('/Google/Spreadsheet/ServiceRequestInterface.php');
require_once('/Google/Spreadsheet/DefaultServiceRequest.php');
require_once('/Google/Spreadsheet/ServiceRequestFactory.php');
require_once('/Google/Spreadsheet/Spreadsheet.php');
require_once('/Google/Spreadsheet/SpreadsheetFeed.php');
require_once('/Google/Spreadsheet/SpreadsheetService.php');
require_once('/Google/Spreadsheet/Exception.php');
require_once('/Google/Spreadsheet/UnauthorizedException.php');
require_once('/Google/Spreadsheet/Spreadsheet.php');
require_once('/Google/Spreadsheet/Util.php');
require_once('/Google/Spreadsheet/Worksheet.php');
require_once('/Google/Spreadsheet/WorksheetFeed.php');
require_once('/Google/Spreadsheet/ListFeed.php');
require_once('/Google/Spreadsheet/ListEntry.php');
require_once('/Google/Spreadsheet/CellFeed.php');
require_once('/Google/Spreadsheet/CellEntry.php');
require_once('/Google/Config.php');
require_once('/Google/Client.php');
require_once('/Google/Auth/Abstract.php');
require_once('/Google/Auth/OAuth2.php');
require_once('/Google/Http/Request.php');
require_once('/Google/Utils.php');
require_once('/Google/IO/Abstract.php');
require_once('/Google/IO/Curl.php');
require_once('/Google/Http/CacheParser.php');
require_once('/Google/Logger/Abstract.php');
require_once('/Google/Logger/Null.php');
require_once('/Google/Exception.php');
require_once('/Google/Auth/Exception.php');
require_once('/Google/Auth/AssertionCredentials.php');
require_once('/Google/Cache/Abstract.php');
require_once('/Google/Cache/File.php');
require_once('/Google/Signer/Abstract.php');
require_once('/Google/Signer/P12.php');
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;
class Google_Spreadsheet
{
private $default = array(
'worksheetCols' => 12,
'worksheetRows' => 25
);
private $spreadsheetKey;
private $spreadsheetName;
private $worksheetName;
private $spreadsheetFeed;
public $initialized = true;
public function __construct($spreadsheetKey, $worksheetName, $spreadsheetName = '')
{
$this->spreadsheetKey = $spreadsheetKey;
$this->worksheetName = $worksheetName;
$this->spreadsheetName = $spreadsheetName;
$this->initialized = $this->initialize();
return true;
}
private function getToken() {
$client_email = '318977712937456456454656563tcfjblgoi#developer.gserviceaccount.com';
$private_key = file_get_contents('API Project-f10e456456b60.p12');
$scopes = array('https://spreadsheets.google.com/feeds');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key,
'notasecret', // Default P12 password
'http://oauth.net/grant_type/jwt/1.0/bearer' // Default grant type
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$tokenData = json_decode($client->getAccessToken());
return $tokenData->access_token;
}
public function initialize(/*$reInitialized = false*/)
{
// load OAuth2 token data - exit if false
$tokenData = $this->getToken();
$serviceRequest = new DefaultServiceRequest($tokenData);
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
try {
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
} catch (\Google\Spreadsheet\UnauthorizedException $e) {
Google_Spreadsheet::warnAdmin($e->getMessage());
return false;
}
$this->spreadsheetFeed = $spreadsheetFeed;
return true;
}
public function insertRow($rowData, $default_fields = array()) {
$spreadsheetFeed = $this->spreadsheetFeed;
$spreadsheet = $this->spreadsheetKey ? $spreadsheetFeed->getByKey($this->spreadsheetKey) : $spreadsheetFeed->getByTitle($this->spreadsheetName);
if(!$spreadsheet && !empty($this->spreadsheetName)) {
$spreadsheet = $spreadsheetFeed->getByTitle($this->spreadsheetName);
}
if(!$spreadsheet) {
Google_Spreadsheet::warnAdmin('No spreadsheet', serialize($rowData));
return false;
}
$worksheetFeed = $spreadsheet->getWorksheets();
$worksheet = $worksheetFeed->getByTitle($this->worksheetName);
if(!$worksheet) {
//create worksheet if not exist
$worksheet = $spreadsheet->addWorksheet($this->worksheetName, $this->default['worksheetRows'], $this->default['worksheetCols']);
$cellFeed = $worksheet->getCellFeed();
for( $i= 1 ; $i <= $this->default['worksheetCols']; $i++ ) {
if(isset($default_fields[$i])) {
$cellFeed->editCell(1, $i, $default_fields[$i]);
}
else {
$cellFeed->editCell(1, $i, "head");
}
$cellFeed->editCell(2,$i,"content");
}
}
if(!$worksheet) {
Google_Spreadsheet::warnAdmin('No worksheet', serialize($rowData));
return false;
}
$listFeed = $worksheet->getListFeed();
$data = array();
foreach ($listFeed->getEntries() as $entry) {
$values = $entry->getValues();
$data[] = $values;
break; //only first row needed, as we need keys
}
$keys = array();
if(!count($data)) {
Google_Spreadsheet::warnAdmin('No data', serialize($rowData));
return false;
}
foreach ($data[0] as $key => $value) {
$keys[] = $key;
}
$newRow = array();
$count = 0;
foreach($keys as $key) {
if(isset($rowData[$count])) {
$newRow["$key"] = $rowData[$count];
}
else {
$newRow["$key"] = '';
}
$count++;
}
$listFeed->insert($newRow);
return true;
}
static function warnAdmin($reason = '', $content = '') {
//temporal function to warn myself about all the stuff happening wrong :)
}
}
And in main model I'm using:
$spreadsheet = new Google_Spreadsheet("spreadsheet name or ID", $worksheetname, "My spreadsheet name");
if(!$spreadsheet->initialized) {
Google_Spreadsheet::warnAdmin('cannot initialize spreadsheet', serialize($rowValues));
}
if(!$spreadsheet->initialized || !$spreadsheet->insertRow($rowValues, $this->default_fields)) {
Google_Spreadsheet::warnAdmin('failed to insert row ');
}
#Edit, thanks to bram brambring for his token solution, seems more easier than my. Working like a charm now, I hope his way it gonna refresh token normally. THANK YOU DUDE!
Been stuck with this issue aswell, seems like google's ClientLogin was finally removed
Important: Do not use ClientLogin for new applications. Instead, use
the more secure OAuth authentication protocol. ClientLogin is a
deprecated authentication protocol and is being turned down on April
20, 2015. At that time, ClientLogin requests will no longer be
answered. If you have existing applications that use ClientLogin, we
encourage you to migrate to OAuth. The ClientLogin support in this
library will be removed in the next major release.
Going to try remake my spreadsheets with OAuth now, this may be the solution: removed cuz no more reputation, will post link into commets
edit; having some troubles with magnetikonline, so gonna try this solution: removed cuz no more reputation, will post link into comments
edit2; /'\ this solution is much better, I must go now, but I've got some success with this library, it works much better as far as I see and provide more functionality, try it.
For your problem with token, try https://github.com/asimlqt/php-google-oauth . THis way also worked for me and its much easier, I reveived array with my token info(if u gonna try that token with second library, ur token gonna be array, while u need only $accessToken['access_token'] part in order to make $serviceRequest = new DefaultServiceRequest($accessToken);
Another big tutorial I found: http://konstantinshkut.com/blog/2014/11/01/how_to_get_data_from_google_spreadsheet_in_yii_php_application (thats for second solution, asimlqt/php-google-spreadsheet-client), step 6 helps a lot to understand how my file must be looking.
I also have had php scripts using Zend for Google Spreadsheets. They have been running great for years, but stopped working today at noon for no reason. What has changed on Google's side and how to easily fix it?
That deprecated ClientLogin, it works again!
It was such a surprise on the eve of the conference Google I / O?
I had to write a self-made solution. Third-party libraries were in basically unusable,
I wrote about 10 methods, I think it is better than the alternatives, which are too complex and cumbersome. You can buy from me))
Not yet ready to put on github)