For Google adwords API I've used this library and It's working fine alone. But I am unable to integrate this library with CI 2.x.
Some code snippet is :
if (!defined('BASEPATH')) exit('No direct script access allowed');
define('SRC_PATH', APPPATH.'/third_party/Adwords/src/');
define('COMMON_PATH', 'Google/AdsApi/Common/');
define('ADWORDS_PATH', 'Google/AdsApi/AdWords/');
define('ADWORDS_VERSION', 'v201710');
// Configure include path
ini_set('include_path', implode(array(
ini_get('include_path'), PATH_SEPARATOR, SRC_PATH))
);
// Include the AdWordsUser file
require_once SRC_PATH.ADWORDS_PATH. '/AdWordsSessionBuilder.php';
require_once SRC_PATH.ADWORDS_PATH. '/Reporting/v201710/DownloadFormat.php';
require_once SRC_PATH.ADWORDS_PATH. '/Reporting/v201710/ReportDefinition.php';
require_once SRC_PATH.ADWORDS_PATH. '/Reporting/v201710/ReportDefinitionDateRangeType.php';
require_once SRC_PATH.ADWORDS_PATH. '/Reporting/v201710/ReportDownloader.php';
require_once SRC_PATH.ADWORDS_PATH. '/ReportSettingsBuilder.php';
require_once SRC_PATH.ADWORDS_PATH. '/v201710/cm/Predicate.php';
require_once SRC_PATH.ADWORDS_PATH. '/v201710/cm/PredicateOperator.php';
require_once SRC_PATH.ADWORDS_PATH. '/v201710/cm/ReportDefinitionReportType.php';
require_once SRC_PATH.ADWORDS_PATH. '/v201710/cm/Selector.php';
require_once SRC_PATH.COMMON_PATH. '/OAuth2TokenBuilder.php';
class My_adwords {
}
Getting following fatal error:
Fatal error: Interface 'Google\AdsApi\Common\AdsBuilder' not found in /var/www/html/crm2017/application/third_party/Adwords/src/Google/AdsApi/AdWords/AdWordsSessionBuilder.php on line 38
Please suggest some optimum solution.
<?php
//namespace Google\AdsApi\Examples\AdWords\v201710\Reporting;
if (!defined('BASEPATH')) exit('No direct script access allowed');
require __DIR__ . '/../third_party/Googleadwords/googleads-php-lib/vendor/autoload.php';
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\Reporting\v201710\ReportDownloader;
use Google\AdsApi\AdWords\Reporting\v201710\DownloadFormat;
use Google\AdsApi\AdWords\ReportSettingsBuilder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\AdWords\v201710\cm\ReportDefinitionReportType;
use Google\AdsApi\AdWords\v201710\cm\ReportDefinitionService;
use Google\AdsApi\AdWords\v201710\cm\CampaignCriterionService;
use Google\AdsApi\AdWords\v201710\cm\Predicate;
use Google\AdsApi\AdWords\v201710\cm\PredicateOperator;
use Google\AdsApi\AdWords\v201710\cm\Paging;
use Google\AdsApi\AdWords\v201710\cm\Selector;
use Google\AdsApi\AdWords\v201710\cm\BiddingStrategyConfiguration;
use Google\AdsApi\AdWords\v201710\cm\BiddingStrategyType;
class My_adwords {
public function __construct() {
//$oAuth2Credential = new OAuth2TokenBuilder();
}
function GetCampaignsCost() {
// Get the service, which loads the required classes.
$oAuth2Credential = (new OAuth2TokenBuilder())
->fromFile()
->build();
// See: AdWordsSessionBuilder for setting a client customer ID that is
// different from that specified in your adsapi_php.ini file.
// Construct an API session configured from a properties file and the OAuth2
// credentials above.
$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
$reportFormat = DownloadFormat::CSV;
$reportQuery = 'SELECT Cost,CampaignId,BiddingStrategyType,CampaignName FROM CAMPAIGN_PERFORMANCE_REPORT DURING YESTERDAY';
// Download report as a string.
$reportDownloader = new ReportDownloader($session);
$reportSettingsOverride = (new ReportSettingsBuilder())
->includeZeroImpressions(false)
->build();
$reportDownloadResult = $reportDownloader->downloadReportWithAwql(
$reportQuery, $reportFormat, $reportSettingsOverride);
$data = $reportDownloadResult->getAsString();
}
}
I've used use instead of require_once. Just include library once and use class which needs. I've done following steps:
Composer runs outside of CodeIgnitor and all dependencies adjusted
via composer and put same folder inside application/third_party.
Make a class inside application/library which interact with
third_party. Above snippet belongs to library class.
Load library in controller and call function of library.
Hence Solved.
Related
I want to add Middleware to my Slim project that checks the ip of the user before allowing them access.
My middleware class:
<?php
namespace App\Middleware;
Class IpFilter
{
protected $request_ip;
protected $allowed_ip;
public function __construct($allowedip = array('127.0.0.1'))
{
$this->request_ip = app()->request()->getIp();
$this->allowed_ip = $allowedip;
}
public function call()
{
$checkit = checkIp();
$this->next->call();
}
protected function checkIp()
{
if (!in_array($this->request_ip, $this->allowed_ip))
$app->halt(403);
}
}
My Bootstrap index.php:
<?php
// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
if (PHP_SAPI === 'cli-server' && $_SERVER['SCRIPT_FILENAME'] !== __FILE__) {
return false;
}
require __DIR__ . '/../vendor/autoload.php';
require '../app/middleware/ipfilter.php';
// Instantiate the app
$settings = require __DIR__ . '/../app/settings.php';
$app = new \Slim\App($settings);
$app->get('/test', function() {
echo "You look like you're from around here";
});
// Set up dependencies
require __DIR__ . '/../app/dependencies.php';
// Register middleware
require __DIR__ . '/../app/middleware.php';
// Register routes
require __DIR__ . '/../app/routes.php';
$app->add(new IpFilter);
// Run
$app->run();
I am using a slim skeleton project for my project setup. I get the following error when I run this code.
Fatal error: Class 'IpFilter' not found in
/Applications/XAMPP/xamppfiles/htdocs/slimtest/my-app/public/index.php
on line 34
I still don't properly understand how to add custom classes for middleware in slim. I've seen several tutorials that just make the class and use $app->add('new class) to add the middleware but I can't figure it out. Is there a file I need to update and I am just missing it?
It's been a long weekend with slim and not a lot of resources out there so any help would be greatly appreciated.
UPDATE:
When I remove the namespace App\Middleware from ipfilter.php I don't get the same error. This time I get
Fatal error: Call to undefined method IpFilter::request() in /Applications/XAMPP/xamppfiles/htdocs/slimtest/my-app/app/middleware/ipfilter.php on line 15
Which I understand why but I thought it might help troubleshoot and get to the root of the problem.
Okay, Finally got it to work.
Index.php
<?php
// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
if (PHP_SAPI === 'cli-server' && $_SERVER['SCRIPT_FILENAME'] !== __FILE__) {
return false;
}
use App\Middleware\IpFilter;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../app/middleware/ipfilter.php';
// Instantiate the app
$settings = require __DIR__ . '/../app/settings.php';
$app = new \Slim\App($settings);
$app->get('/test', function() {
echo "You look like you're from around here";
});
// Set up dependencies
require __DIR__ . '/../app/dependencies.php';
// Register middleware
require __DIR__ . '/../app/middleware.php';
// Register routes
require __DIR__ . '/../app/routes.php';
$app->add(new IpFilter);
// Run
$app->run();
ipfilter.php
<?php
namespace App\Middleware;
Class IpFilter
{
private $whitelist = arrray('127.0.0.1')
protected $request_ip;
public function __invoke($request, $response, $next)
{
$request_ip = $request->getAttribute('ip_address');
return $next($request, $response);
}
public function call()
{
$checkit = checkIp();
$this->next->call();
}
protected function checkIp()
{
if (!in_array($this->request_ip, $this->whitelist)
$app->halt(403);
}
}
KEY: Using App\Middleware\Ipfilter in the index.php. I though using require to add the class would be enough but apparently no.
Shout out to codecourse.com, really helped.
Using only a simple file I can get elephant.io to work, but If I'm trying to integrate it inside an class, it won't work.
The reason why it won't work is that it says I can't use use elephant ... inside a class or function.
How would I integrate elephant.io in an already existing class ?
To be more specific , I'm trying to integrate elephant.io into codeigniter framework.
function tester() {
use ElephantIO\Client,
ElephantIO\Engine\SocketIO\Version1X;
require __DIR__ . '/vendor/autoload.php';
$client = new Client(new Version1X('http://www.textbasedmafiagame.com:8080'));
$client->initialize();
$client->emit('broadcast2', ['foo' => 'utførte et biltyveri']);
$client->close();
}
and get no response or outcome, but i do get:
use not allowed inside a function
and
Parse error: syntax error, unexpected 'use'
As the error states, the use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations.
Try something along these lines
<?php
use ElephantIO\Client, ElephantIO\Engine\SocketIO\Version1X;
require __DIR__ . '/vendor/autoload.php';
class foo{
public function tester() {
$client = new Client(new Version1X('http://www.textbasedmafiagame.com:8080'));
$client->initialize();
$client->emit('broadcast2', ['foo' => 'utførte et biltyveri']);
$ client->close();
}
}
I have a view.blade.php that calls a wrapper.php that return some value for an iframe in the view.
I'm able to return that value, but I have to do some work with other methods in my laravel classes.
It's possible to instatiate the classes inside the wrapper php and call methods, or call static class methods?
Project structure:
- project
-- app
--- Http
---- Classes
----- RepositoryUtil.php
...
-- public
--- Wrapper.php
...
-- resources
--- views
---- partials
----- view.blade.php
Code inside view.blade.php:
<iframe id="reader" src="/libs/pdfjs/web/viewer.html?file=http://project.dev/Wrapper.php?id={{$encrypted}}">
</iframe>
Code inside Wrapper.php:
<?php
// tried: use App\Http\Classes\RepositoryUtil
// tried: \App\Http\Classes\RepositoryUtil::getValue();
// tried: {{RepositoryUtil::getValue()}}
$myValue = RepositoryUtil::getValue(); #not work
var_dump($myValue);
?>
Code inside RepositoryUtil.php
<?php
class RepositoryUtil{
public static function getValue(){
dd("getValue!");
return "value";
}
}
?>
The error:
( ! ) Fatal error: Class 'RepositoryUtil' not found in /home/vagrant/Code/project/public/Wrapper.php on line 14
EDIT:
I can call static class method adding include_once("../App/Http/Classes/RepositoryUtil.php"); at the top of Wrapper.php but when I call the "laravel methods" like $decrypted = Crypt::decrypt($encrypted); it return the error:
( ! ) Fatal error: Class 'Crypt' not found in /home/vagrant/Code/project/App/Http/Classes/RepositoryUtil.php on line 13
Thanks
After many and many attemps and research, I figure that out!
Post my Wrapper.php code:
<?php
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$value = RepositoryUtil::getValue();
// Your other staff here...
?>
The problem was that after calling external php, Laravel wasn't booted!
I tried to boot with require __DIR__.'/../bootstrap/autoload.php';, $app = require_once __DIR__.'/../bootstrap/app.php'; and calling $app->boot(), and it worked.
But there were other problems: Facades were not loaded, but I can see into "alias" array when log $app variable.
You have to boot kernel to get Facades back.
Using Laravel 5.1.
I am new in codeigniter . i can easily work with parse.com library on core php like this
<?php
// define location of Parse PHP SDK, e.g. location in "Parse" folder
// Defaults to ./Parse/ folder. Add trailing slash
define( 'PARSE_SDK_DIR', '../src/Parse/' );
// include Parse SDK autoloader
require_once( 'autoload.php' );
// Add the "use" declarations where you'll be using the classes
use Parse\ParseClient;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseACL;
use Parse\ParsePush;
use Parse\ParseUser;
use Parse\ParseInstallation;
use Parse\ParseException;
use Parse\ParseAnalytics;
use Parse\ParseFile;
use Parse\ParseCloud;
// Init parse: app_id, rest_key, master_key
ParseClient::initialize('id', 'id2', 'id3');
// save something to class TestObject
$testObject = ParseObject::create("TestObject");
$testObject->set("foo", "bar");
$testObject->save();
// get the object ID
echo $testObject->getObjectId();
echo '<h1>Users</h1>';
// get the first 10 users from built-in User class
$query = new ParseQuery("_User");
$query->limit(10);
$results = $query->find();
foreach ( $results as $result ) {
// echo user Usernames
echo $result->get('username') . '<br/>';
}
but i am unable to use this in codeigniter . please help me how i can add parse.com in codeigniter and use this.
please provide me some direction and example
download CI library from here
parse.com paste all code inside your application/libraries folder. make sure you have created custom config parse.php file sample
<?php
/**
* Parse keys
*/
$config['parse_appid'] = '';
$config['parse_masterkey'] = '';
$config['parse_restkey'] = '';
$config['parse_parseurl'] = 'https://api.parse.com/1/';
?>
and in your sample controller ParseSample.php try this.
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class ParseSample extends CI_Controller {
public function index()
{
$temp = $this->load->library('parse');
$testObj = $this->parse->ParseObject('testObj');
$testObj->data = array("testcol" => "it works");
$return = $testObj->save($testObj->data);
echo "<pre>";
print_r($return);
echo "<pre>";
var_dump($return);
exit;
}
}
Hope someone might feel helpful.
I've read answers to the same questions here at SO, but they haven't helped me to fix my problem.
I have the follows directory structure:
And this class:
namespace Util;
final class Autoloader
{
public static function loader($class)
{
define('PHP_FILE_EXTENSION', '.php');
$filename = '';
$file = '';
$phisicalFilePath = '';
$filename = $class . PHP_FILE_EXTENSION;
$phisicalFilePath = __DIR__ . DIRECTORY_SEPARATOR . $filename;
if (file_exists($phisicalFilePath)) {
require_once 'util/' . $filename;
}
}
}
I use the above class as follows from the bootstrap.php file:
require_once('util/Autoloader.php');
spl_autoload_register('Util\Autoloader::loader');
And I call everything from an index.php file:
require_once('bootstrap.php');
echo StringUtils::randomString(10);
But unfortunately, the SPL autoload doesn't load the class:
Fatal error: Class 'StringUtils' not found in 'xxx\index.php' on line 5
What am I doing wrong?
I have another solution. It might be useful if you want to continue with development in PHP.
Look at this articles - http://www.php-fig.org/psr/psr-0/ and http://www.php-fig.org/psr/psr-4/. They contain officially approved standards regarding classes structure for PHP-based projects.
We shouldn't investigate the wheel in such cases - so try to keep in touch with "official standards" if it's possible.
I created example project to show how to apply mentioned rules.
Project structure:
Autoloader.php was copied from here: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md#class-example. The only difference is that my autoloader was placed in the namespace Util and its class was renamed to Autoloader.
Then Account.php contains:
<?php
namespace Model;
class Account { }
And index.php:
<?php
require_once('Util/Autoloader.php');
$autoloader = new \Util\Autoloader();
$autoloader->addNamespace('\Model\\', __DIR__ . '/Model');
$autoloader->register();
$model = new \Model\Account();
I just made a project as your's and copied your files contents.
it works for me.
there is no problem with your code.