How to implement php packages - php

Ok so I have been learning vanilla php and wanted to delve into making use of packages. so I tried implementing fabpot\Goutte to the teeth.
I did install the package using Composer and placed the src code in the root folder where the Composer-generated files sit. As I run the script, I get an initializing class not found error.
How do I get around with this? I have followed the instructions and it's making me crazyyyyyyyyyyyy
Code I got:
<?php
use Goutte\Client;
// Initialize object
$client = new Client();
// Issue GET request to URI
$crawler = $client->request("GET", "http://www.symfony.com/blog");
$client->getClient()->setDefaultOption("config/curl".CURLOPT_TIMEOUT, 60);
// Navigate the client through the use of links
$link = $crawler->selectLink("Security Advisories")->link();
$crawler = $client->click($link);
// Extract data
$crawler->filter("h2 > a")->each(function($node) {
print $node->text()."\n";
});
?>
The error I am getting:
Fatal error: Class 'Goutte\Client' not found in **line 6**

On top of your script.. You need to include composer's autoloader like this:
require __DIR__ . "/vendor/autoload.php";

Related

Include library PHP Codeigniter

I want to include the Threema library in my Codeigniter project, but I have trouble doing it. This is the library.
I followed the instructions, created an autoload with composer, but I don't know how to make it work. I have an helper that looks like this :
require_once APPPATH . "libraries/Threema/vendor/autoload.php";
//define your connection settings
$settings = new ConnectionSettings(
'*xxxxx',
'xxxxxx'
);
$publicKeyStore = new Threema\MsgApi\PublicKeyStores\File("pathTo/threema_key.php");
$connector = new Connection($settings, $publicKeyStore);
I have the error :
Class 'ConnectionSettings' not found
I tried to do as the guide by using the keyword "use" but that doesn't work either :
use Threema\MsgApi\Connection;
use Threema\MsgApi\ConnectionSettings;
use Threema\MsgApi\Receiver;
What is the simple way to install this library in a CodeIgniter environment ?
try to change this line
require $SERVER['DOCUMENT_ROOT'] ./'libraries/Threema/vendor/autoload.php';

Class 'Psr\Log\NullLogger' not found in C:\xampp\htdocs\evernote\evernote-cloud-sdk-php\src\Evernote\Client.php on line 156

When creating a Client in Evernote sandbox sample:
$client = new \Evernote\Client($token, $sandbox);
I'm getting following error:
Fatal error: Class 'Psr\Log\NullLogger' not found in C:\xampp\htdocs\evernote\evernote-cloud-sdk-php\src\Evernote\Client.php on line 156
I know, this is because of missing: Psr\Log, files but I do not know where I should add them?
I don't want to use composer because I'm not sure if I will be able to use it in production. Anyway the settings is as follows: https://github.com/evernote/evernote-cloud-sdk-php/blob/master/composer.json
Does anyone know how to add the Psr\Log into Evernote PHP SDK API please?
Thank you!
After some testing I found the solution as follows:
download ZIP file of: Psr\Log, from: https://github.com/php-fig/log
save folder: Psr, into folder: evernote-cloud-sdk-php/src
change file: autoload.php, within folder: evernote-cloud-sdk-php/src, as follows:
Add new value into array:
$namespaces = array(
'EDAM',
'Thrift',
'Evernote',
'Psr'
);
Create new function:
function psrAutoload($className, $lastNsPos)
{
return genericAutoload($className, $lastNsPos);
}
Seems to be working so far, hope it will help someone to safe time.

How to create object of class inside function in function.php of wordpress

I am integrating google spreadsheet in my project. I have to save form data into google spreadsheet.
For this i am using asimqt php-google-spreadsheet-client library.
I have single form on site that form is submitting using ajax. For this i have written function in function.php.
Getting error when initializing object of DefaultServiceRequest class.
Error: Fatal error: Class 'Google\Spreadsheet\DefaultServiceRequest' not found
require '/vendor/autoload.php';
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;
function spreadsheet_feeds()
{
$access_tok = 'xyz-token';
$serviceRequest = new DefaultServiceRequest($access_tok); // Getting error
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
}
add_action( 'wp_ajax_nopriv_spreadsheet_data', 'spreadsheet_feeds' );
add_action('wp_ajax_spreadsheet_data','spreadsheet_feeds');
Any help why this error is occurring because class is already include using "use" statement ?
This seems to be an issue with the location of your vendor folder.
Make sure that the vendor folder is accessible by the file
Considering your folder hierarchy to be
-wp-content
--themes
---your-theme
----functions.php
If you have your vendor folder in your-theme/vendor then in your functions.php
you should write
require 'vendor/autoload.php';
and if vendor is in the root directory of WP make sure you traverse back to the root folder using something like :
require __DIR__ .'/../../../../vendor/autoload.php';
Just replace require '/vendor/autoload.php'; to require_once('vendor/autoload.php');
Just now I tried this here for you myself and it works.

Laravel 5 add nusoap

I am unable to install nusoap to my existing laravel 5 application. Ive done the following:
Created a new Folder in App/Http/Controllers/ - namend "Soap" - and copied the libary into it.
use
composer dump-autoload
So i am able to use
use nusoap_client;
But i am always getting an error:
Class 'App\Http\Controllers\nusoap_client' not found
I thought that laravel automatically load all classes from the "app" directory, but how can i use it here?
Tried with:
$wsdl = "test.xx/_vti_bin/lists.asmx?wsdl";
$client = new nusoap_client($wsdl, true);
Thanks for any help!
Just add these lines to your controller:
include 'nusoap.php';
use nusoap_client;
As a simple way, you can add a backslash in front of nusoap_client, like this:
$client = new \nusoap_client($wsdl, true);

PHP Use Keyword Namespace Not Finding Class

Weirdness. Any ideas why it can't find the class?
Directory Tree:
test2.php
- src
- Google
- Spreadsheet
DefaultServiceRequest.php
ServiceRequestInterface.php
Google_Client.php
...
test2.php:
namespace src\Google\Spreadsheet;
require_once 'src/Google/Spreadsheet/ServiceRequestInterface.php';
require_once 'src/Google/Spreadsheet/DefaultServiceRequest.php';
require_once 'src/Google/Spreadsheet/Google_Client.php';
use Google\Spreadsheet\ServiceRequestInterface;
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;
function getGoogleTokenFromKeyFile($clientId, $clientEmail, $pathToP12File) {
require 'src/Google/Spreadsheet/Google_Client.php';
$client = new Google_Client();
$client->setClientId($clientId);
$cred = new Google_Auth_AssertionCredentials(
$clientEmail,
array('https://spreadsheets.google.com/feeds'),
file_get_contents($pathToP12File)
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service_token = json_decode($client->getAccessToken());
return $service_token->access_token;
}
$serviceRequest = new DefaultServiceRequest(getGoogleTokenFromKeyFile(..., ..., ...));
ServiceRequestFactory::setInstance($serviceRequest);
Not sure if this is Google API related or what. Something weird is the ServiceRequest classes wouldn't work until I required them. When I didn't, it said it couldn't find it... And when I tried adding src/ to the use path, didn't work, and I tried removing the path all together, all did nothing.
Error: Fatal error: Class 'src\Google\Spreadsheet\Google_Client' not found in test2.php on line 15
looks like you are using this library
if you used composer to install the library you need to include the vendor/autoload.php file in your code
require 'vendor/autoload.php';
it is recomneded that you use composer to install this library but if you don't want to use composer, you need to create autoloader and require it in your code

Categories