Use Custom Library in Controller of Laravel - php

I wanted to use nusoap_client class so decided to add nusoap library to my laravel project. Like other libraries, i used github readme page to get it done.
composer require econea/nusoap:^0.9.6
This library was added to ../vendor/econea/nusoap/src/nusoap.php. Then i wanted to use it in my controller but i tried many ways to use it like below :
use SoapClient;
use vendor\econea\nusoap\src\nusoap.php;
use nusoap.php;
use nusoap;
I even tried to load library in autoload in composer.json file without any luck. laravel kept repeating same error.
Class 'App\Http\Controllers\nusoap_client' not found
I appreciate any help given that suggests a way to use nusoap_client in my code or the correct way to use the library in my laravel project.
thanks.
my code:
$client = new nusoap_client('example.com/api/v1', 'wsdl');

The proper syntax at the top of your controller should be:
use nusoap_client;

This is happening because this class does not contain a valid namespace.
So, you need to import this PHP file into your script using the require function.
To solve this in laravel, just import your file using composer like:
"autoload": {
"files": ["path/to/vendor/script"]
}
With autoloaded file, you can instantiate the class in any script class of your project.
But, I don't think that's a good way to work with modern PHP.
I know another library that works with SOAP: https://github.com/artisaninweb/laravel-soap
It's a wrapper for SoapClient http://php.net/manual/pt_BR/class.soapclient.php, a PHP core class that provides a soap client.
Hope my contribution can be useful and my english understandable.

Related

How to stop Laravel (5.4) thinking a 3rd party API call is a Laravel controller?

I'm trying to integrate a 3rd party library (this video site API: https://github.com/SproutVideo/sproutvideo-php ) with my Laravel site. But I think this is a generic question.
The video API instructs me to call it like so:
SproutVideo::$api_key = 'abcd1234';
or
$token = SproutVideo\UploadToken::create_upload_token();
I put these calls inside a function in one of my Laravel controllers. Unfortunately when this function is called, I get the following kind of error:
(for the first example call above)
FatalThrowableError
Class 'App\Http\Controllers\SproutVideo' not found
(or for the second:)
FatalThrowableError
Class 'App\Http\Controllers\SproutVideo\UploadToken' not found
(I think) I've included the Sproutvideo library at the top of the Controller file:
require '../vendor/autoload.php'; // Load SproutVideo API library
So how do I get Laravel to stop thinking the Sproutvideo API calls are Laravel controller calls, and just let them pass through to the Sproutvideo library?
Thanks
You have to import it at the top with
use (Path To Lib in vendor directory)
Then it's namespaced and you can use it. Otherwise use the fully qualified namspace directly in the code.
Although you import it via autoload (which really should happen not in the controller class itself when using Laravel btw *), you have to tell the controller class the namespace.
Have a look here
The autoloader sits in /bootstrap/autoload.php and references /vendor/autoload.php again. It is called in /public/index.php. It gets called with every request anyway :)

How can I use a third party api in the vendors directory of symfony from my controller

I am working with an existing controller in symfony and need to use an api that I have in my vendors directory. Needless to say I am entering a pre-existent, large scale project and with minimum experience in symfony I am not too sure of how to use the vendor directory. When I have tried using "use vendor\fullcontact\sdk*********;" symfony tells me that there is no class called this in my bundle. I have looked for information on using the vendor directory but have came up dry. Any information regarding how I can start using my vendor directory would be appreciated.
Seems you are using the PHP Library for FullContact API.
This library don't use namespace so you can simply try to use it adding an initial slash to the class when you are using it. As Example:
$this->name = new \Services_FullContact_Name($apikey);
Hope this help

How to include external library in Zend Framework 2?

I played with the zf2-tutorial successfully, but I was totally confused when trying to integrate an external library like "jpgraph". I know I must do this with autoload or servicemanager but it won't work.
The php-files of jpgraph are in the vendor/graph directory. I use a module called Jpgraph, in the controller indexAction I try:
$graph = new Graph($width,$height);
this gives me an error:
Fatal error: Class 'Jpgraph\Controller\Graph' not found in ...
the jpgraph library don't use namespaces.
i also tried this way without success
what's the best way to integrate such things?
I would be glad for every tip or help
Add the library to your composer.json and add the class with Classmap and/or the include path as phpunit does
https://github.com/sebastianbergmann/phpunit/blob/master/composer.json#L48
One option, as Maks3w pointed out, is to use Composer. If you've never heard of or used composer before it's definitely worth a look. I was surprised how easy it was to set up and use 3rd party libraries. It's also very easy to set up your own library to work with composer, and use any source controlled (git or svn) library of your own - works well with GitHub repos - just add a composer.json file.
On the other hand, you do not need to use composer to do what you want, it would make it very easy, but it may be overkill. Zend Framework 2 has a very flexible autoloader system, and although it works well with PSR-0, you can have any class autoloading sytem that you like. Take a look at the different components of Zend\Loader, in particular I think the ClassMapAutoloader will be the one to suit your needs.

Using third party code within Zend Framework

I want to use a small library within the Zend Framework (simple_php_dom, for what it's worth).
Should I just stick it in library/, include it where I want to use it (like in a specific controller) like include('library/foo.php'); and have at it?
If not, how should I do it? What's the "Zend Framework" way of doing something like this?
Since the library doesnt support PEAR conventions its not really easy to hook it up to the autoloader, so i would just manually require_once it in the controller or model that uses it. If it was used extensively I might make a wrapper class to proxy calls through and autoload that (that class having the require_once).

Google API PHP Client and Codigniter

I want to include Google Cloud in one of my webprojects which is based on Codeiginiter.
Now my big question is, how to implement the Google API PHP client into CI?
Since CI is a MVC framework it wouldn't make sense including it directly with require_once in the view, so I thought about creating a library but that means a lot of work.
Does anyone know a better solution or maybe a fitting CI library?
Thanks.
This can be implemented by creating a simple library by extending the Google_Api
some thing like..
require_once /path/to/Google_Api/file_name.php
Class my_google_api extend Google_Api
{
// constructor
}
Now load this library to your controller and the access all the methods from Google_api
You can use composer. CI supports autoloading of third party libraries with composer. Besides, usage of composer is a preferred method to install Google APIs Client Library. In this way you won't need to include the library with require_once, all the modules will be loaded automaticly by composer's autoloader.

Categories