I must be making a simple error, I have not used Composer before now. I have followed the instructions on the GitHub page, but I'm getting a Class 'ZCRMRestClient' not found error when I load the page.
composer.json:
{
"require": {
"zohocrm/php-sdk": "^2.0"
}
}
PHP is
require 'vendor/autoload.php';
$configuration = array(
'client_id' => '1000.***',
'client_secret' => '***',
'redirect_uri' => '***',
'currentUserEmail' => '***',
);
ZCRMRestClient::initialize($configuration);
$contacts = ZCRMRestClient::getModule(“Contacts”);
echo "<pre>\n";
print_r($contacts);
echo "\n</pre>";
I've tried \ZCRMRestClient::initialize($configuration) but that hasn't helped.
There is new sdk 3.0 available, and the old sdk 2.0 is moved to:
"require": {
"zohocrm/php-sdk-archive": "^2.0"
}
I'm going to assume you've executed the composer require zohocrm/php-sdk command, or potentially added the requirement straight into your project's composer.json by hand.
Make sure you're importing the correct namespace for the library - in this case it seems to be zcrmsdk\crm\setup\restclient\ZCRMRestClient
You should write use zcrmsdk\crm\setup\restclient\ZCRMRestClient; at the top of the file then invoke methods as you currently do.
Alternatively, you can invoke methods as in the following example: \zcrmsdk\crm\setup\restclient\ZCRMRestClient::initialize($configuration);
After that the most likely problem is that your autoload file doesn't contain a reference to your library.
composer dump-autoload should fix that (from the command line.)
And finally perhaps you are not requiring the vendor folder correctly!
Related
I want to use namespaces class to manual one without using autoload.php to be included. Because I don't want to use all function the class.
I am using this project https://github.com/codenix-sv/coingecko-api to get it's function in my php function.
In the example of using is like this
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
$client = new CoinGeckoClient();
$data = $client->ping();
But I want to change it to require_once. So I put all src folder in my php folder and create this to call the function
require_once 'libs/Api/CoinGeckoClient.php';
$client = new Codenixsv\CoinGeckoApi\CoinGeckoClient;
$data = $client->simple();
First I got this error when trying to access the page.
Fatal error: Uncaught Error: Class 'GuzzleHttp\Client' not found in
C:\xampp\htdocs\te.st\libs\Api\CoinGeckoClient.php:35
Then I try to remove the line "use GuzzleHttp\Client" in CoinGeckoClient.php file.
And got with this error
Fatal error: Uncaught Error: Class 'Codenixsv\CoinGeckoApi\Client' not
found in C:\xampp\htdocs\te.st\libs\Api\CoinGeckoClient.php:35
Is there any way to just use the "simple" function of coingecko only in my php file.
https://github.com/codenix-sv/coingecko-api/blob/master/src/Api/Simple.php
Here is the way I fix this.
load in composer.json like
{
"require": {
"codenix-sv/coingecko-api": "^1.0",
"guzzlehttp/guzzle": "~6.0"
}
}
then do composer update in command window.
In my php file. make sure
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
is placed in top of file. Then do the rest.
Thanks all
That package is prepared to works with composer.
Composer delivered autoloader, to make you work simplier.
If you remove use GuzzleHttp\Client line from CoinGeckoClient.php, there will be no way to send request to the server.
The best option is include composer autoload in your project file, it means you should:
Create composer.json file for you project
Add required library dependency using command: composer require guzzlehttp/guzzle
Add require library dependency using command: composer require codenix-sv/coingecko-api
Inside your project file add folowing line:
require_once(dirname(__FILE__) . '/vendor/autoload.php');
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
$client = new CoinGeckoClient();
$data = $client->ping();
In other way, that will be necessarily to import all files manually. And of course, you haven't to forget about imports for Guzzle client.
I have a CodeIgniter application. I've been trying to set it up to work with Facebook ADS SDK ( https://github.com/facebook/facebook-php-ads-sdk ).
I want to use a simple lines in my controller like:
use FacebookAds\Object\CustomAudience;
use FacebookAds\Object\Fields\CustomAudienceFields;
use FacebookAds\Object\Values\CustomAudienceSubtypes;
$audience = new CustomAudience(null, 'act_123123');
I have created composer.json file:
{
"require": {
"facebook/php-ads-sdk": "2.8.*"
}
}
I let it through command
php composer.phar install --no-dev
And everything worked fine. It installed me autoload.php with external facebook folder.
Now when it comes to the part where I need to make it work with CodeIgniter I constantly get errors.
I tried two different approaches:
First was to include it in my index.php Like this:
include_once 'application/vendor/autoload.php';
require_once BASEPATH.'core/CodeIgniter.php';
However I was getting errors for not loading my classes that are in application/core folder (Back_Controller is extending CI_Controller and I am using Back_Controller in my every controller, I also have My_Model and Front_Controller there).
Second aproach:
To use CodeIgniter's feature to use Auto-loader together with Composer. So I changed in the config:
$config['composer_autoload'] = TRUE;
Both approaches returned me the same errors:
"Fatal error: Class 'Back_Controller' not found in /public_html/application/controllers/admin/Shops.php on line 4
Warning: include(): open_basedir restriction in effect.
File(application/errors/html/error_php.php) is not within the allowed
path(s): () in /public_html/system/core/Exceptions.php on line 269"
How can I set it up so Facebook SDK actually works together with CodeIgniter?
Any help will be very appreciated. Thank you.
I am thinking that one of the solutions would be to add "autoload" to composer.json so it loads my core classes but I can't figure out how should it be. I tried:
"autoload": {
"psr-4": {
"Back_Controller\\":"core/"
}
}
Try the following:
Include the main Api file
include APPPATH.'vendor/wherever_your_sdk_is/FacebookAds/Api.php';
Then do the following... as per documentation...
use FacebookAds\Api;
// Initialize a new Session and instanciate an Api object
Api::init($app_id, $app_secret, $access_token);
// The Api object is now available trough singleton
$api = Api::instance();
Figured it out.
I just added "autoload" to composer.json to autoload my core files and now both solutions that I tried before - works.
"autoload": {
"psr-4": {
"":"core/"
}
}
I got no clue why AWeberAPI is not found. Any help is appreciated.
php code:
require('vendor/autoload.php');
new PHPExcel;
new AWeberAPI;
composer.json:
{
"require": {
"aweber/aweber": "^1.1",
"phpoffice/phpexcel": "^1.8"
}
}
The problem
The module doesn't appear to be properly configured for use/autoloading with composer. They may have just added the composer configuration to allow you to easily install it, but not to use it within the composer autoloader.
The generic convention for it is that AWeberAPI should match the package's PSR-4 autoloader format, which says "look in aweber_api", then it will look for a class named AWeberAPI.php. You can test this behaviour is correct by adding this file:
<?php
// File: vendor/aweber/aweber/aweber_api/AWeberAPI.php
class AWeberAPI {
public function __construct() {
die('yeah, it works now...');
}
}
Then try your script again, the class will exist now.
What can I do?
Well - you could submit a pull request to their repository to fix it, but it looks like it would involve renaming the classes and filenames which would be a breaking change so I probably wouldn't bother.
You can get it to work by requiring the actual source of the API library instead of the composer autoloader in this case:
require_once 'vendor/aweber/aweber/aweber_api/aweber_api.php';
I've downloaded the paypal-core-sdk for php from GitHub manually, my server does not have 'composer' and I've just manually copied the files to an include directory outside the document root. Problem is when I go call on some of the classes the server borks and doesn't know what I'm talking about ... i.e.
Fatal error: Class 'PPApiContext' not found in /home/rctoronto/public_html/start.php on line 143
if I include once on the file providing PPApiContext, it moves along to another error and so on eventually proceeds when I've included all the ones it complains about. But then the sdk has internal dependency issues.
How do I formally declare the sdk library so my app can use it - I've scoured the web and haven't managed to find a good answer to this question. I've tried manually altering my include_path using my php.ini and still it can't use the classes of the sdk. I don't have access to /usr/lib/php or /usr/local/lib/php as I am on a shared server.
this is the 'getAuthorize' url for paypal (which I've gotten to work with manually include_once on the files...
$apicontext = new PPApiContext(array('mode' => 'live'));
$clientId = PP_CLIENTID;
$clientSecret = PP_SECRET;
$scope = array('openid', 'email');
$redirectUri = 'https://maskedurl.ca/login?ltype=pp&loginSubmit=true';
$pp_authUrl = PPOpenIdSession::getAuthorizationUrl($redirectUri, $scope , $clientId, $apicontext);
But how do we properly declare the sdk library in whole?
From: http://paypal.github.io/sdk/
If you do not want to use composer, you can grab the SDK zip, unzip the file to your application folder and require the 'vendor/autoload.php' file. This file registers a custom autoloader that can autload the PayPal SDK files.
Download SDK zip
Here's how I solved it. Update composer.json as follows:
{
"name": "PayPal Test",
"require": {
"paypal/sdk-core-php": "dev-stable"
},
"autoload": {
"psr-0": {"PayPal\\": "vendor/paypal/sdk-core-php/lib/"}
}
}
This will do the job:
include('paypal-sdk-core/lib/common/PPApiContext.php');
include('paypal-sdk-core/lib/PPConstants.php');
include('paypal-sdk-core/lib/PPConfigManager.php');
include('paypal-sdk-core/lib/auth/openid/PPOpenIdSession.php');
If you need the full stack to make the example on https://devtools-paypal.com/guide/openid/php?interactive=ON&env=sandbox work, use this:
include('paypal-sdk-core/lib/common/PPApiContext.php');
include('paypal-sdk-core/lib/PPHttpConfig.php');
include('paypal-sdk-core/lib/common/PPUserAgent.php');
include('paypal-sdk-core/lib/PPHttpConnection.php');
include('paypal-sdk-core/lib/PPLoggingLevel.php');
include('paypal-sdk-core/lib/exceptions/PPConnectionException.php');
include('paypal-sdk-core/lib/common/PPModel.php');
include('paypal-sdk-core/lib/auth/IPPThirdPartyAuthorization.php');
include('paypal-sdk-core/lib/handlers/IPPHandler.php');
include('paypal-sdk-core/lib/PPConstants.php');
include('paypal-sdk-core/lib/PPConfigManager.php');
include('paypal-sdk-core/lib/auth/openid/PPOpenIdSession.php');
include('paypal-sdk-core/lib/auth/PPTokenAuthorization.php');
include('paypal-sdk-core/lib/auth/openid/PPOpenIdTokeninfo.php');
include('paypal-sdk-core/lib/transport/PPRestCall.php');
include('paypal-sdk-core/lib/PPLoggingManager.php');
include('paypal-sdk-core/lib/handlers/PPOpenIdHandler.php');
Is it possible to include a package that was not specifically designed for L4 in the framework?
If so, how is it done? I know I need to add the package to my composer.json which adds it to the vendor folder, but can I register it somehow in the providers array? are there any other steps necessary?
I would like to use the Google checkout package originally designed for Yii
Using third party composer packages with Laravel 4
When developers create composer packages, they should map the auto-loading using PSR-0 or PSR-4 standards. If this is not the case there can be issues loading the package in your Laravel application. The PSR-0 standard is:
{
"autoload": {
"psr-0": { "Acme": "src/" }
}
}
And the PSR-4 standard:
{
"autoload": {
"psr-4": { "Acme\\": "src/" }
}
}
Basically the above is a standard for telling composer where to look for name-spaced files. If you are not using your own namespaces you dont have to configure anything else.
SCENARIO 1
PSR-0 standard following packages (with autoload classmap) in Laravel
This is a simple one, and for example i will use the facebook php sdk, that can be found:
https://packagist.org/packages/facebook/php-sdk
Step 1:
Include the package in your composer.json file.
"require": {
"laravel/framework": "4.0.*",
"facebook/php-sdk": "dev-master"
}
Step 2:
run: composer update
Step 3:
Because the facebook package uses a class map its working out of the box, you can start using the package instantly. (The code example below comes straight from a normal view. Please keep your logic out from views in your production app.)
$facebook = new Facebook(array(
'appId' => 'secret',
'secret' => 'secret'
));
var_dump($facebook); // It works!
SCENARIO 2
For this example i will use a wrapper from the instagram php api. Here there need to be made some tweaks to get the package loaded. Lets give it a try!
The package can be found here:
https://packagist.org/packages/fishmarket/instaphp
Step 1:
Add to composer .json
"require": {
"laravel/framework": "4.0.*",
"fishmarket/instaphp": "dev-master"
}
Then you can update normally (composer update)
Next try to use the package like you did with the facebook package. Again, this is just code in a view.
$instagramconfig = array(
'client_id' => 'secret',
'client_secret'=> 'secret',
'access_token' => 'secret'
);
$api = Instaphp::Instance(null, $instagramconfig);
var_dump($api); // Epic fail!
If you try the above example you will get this error:
FatalErrorException: Error: Class 'Instaphp' not found in ...
So we need to fix this issue. To do this we can examine the instagram composer.json, that has its autoload diffrent than the facebook php sdk had.
"autoload": {
"psr-0": { "Instaphp": "." }
}
Compared to the facebook composer.json:
"autoload": {
"classmap": ["src"]
}
(Composer handles different kinds of autoloading, from files and class-maps to PSR. Take a look at your vendor/composer/ folder to see how its done.)
Now we will have to load the class, manually. Its easy, just add this (top of your controller, model or view):
use Instaphp\Instaphp;
composer dump-autoload, and it works!
step2 (optional)
Another method is (if you dont want to use the "use" statement, you can simply tell composer to look for the files straight from your code. Just change the Instance like so:
// reference the name-spaced class straight in the code
$api = Instaphp\Instaphp::Instance(null, $instagramconfig);
var_dump($api); // It works
However I suggest using the usestatement to make it clear to other developers (and your future self) what (external) classes/packages are used in the program.
SCENARIO 3
Here we use the Laravels built in IOC container to register service providers. Please note that some packages might not be suitable for this method. I will use the same Instagram package as in scenario 2.
Quick and dirty
If you don't care about design patterns and service providers you can bind a class like this:
App::bind('Instaphp', function($app)
{
return new Instaphp\Instaphp;
});
And you resolve it like this.
App::make('Instaphp');
Quick and dirty end
If you're working on a bigger project, and you make use of interfaces you should probably abstract the bindings further.
Step 1:
Create a folder inside your app folder, for example a 'providers' folder.
app/providers
Make sure Laravel auto-loads that folder, you can pass in some additional info to composer.json, like this:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/providers" // this was added
]
},
Now create a File inside the new folder called Instagram.php and place this inside:
<?php
use Illuminate\Support\ServiceProvider;
class InstagramServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind('Instaphp', function()
{
return new Instaphp\Instaphp;
});
}
}
Now run composer dump-autoload again, and you can use the package. Note that the instagram package has a final private function __construct(), this means you cannot use that package outside the original class without changing the construct method to public. I'm not saying this is a good practice, and i suggest to use the scenario 2, in the case of the instagram package.
Anyway, after this you can use the package like this:
$instagramInstance = App::make('Instaphp');
$instagramconfig = array(
'client_id' => 'secret',
'client_secret'=> 'secret',
'access_token' => 'secret'
);
$instagram = new $instagramInstance();
$userfeed = $instagram->Users->feed($instagramconfig);
var_dump($userfeed); // It works!
Add "tvr/googlecheckout": "dev-master" this to your composer.json.
Run composer install, then you can use the IoC container. Some code examples can be found in the official docs for Laravel 4: http://four.laravel.com/docs/ioc#basic-usage