I have installed a PHP wrapper library using Composer. The autoloader seems to be working fine, but I cannot call a class as it says
class 'Diffbot' not found.
I have tried numerous tricks, especially those mentioned in the Composer documentation, but I cannot get it working and I think I must share my problem here.
My composer.json
contains the following lines
{
"require": {
"swader/diffbot-php-client": "^0.4.4"
}
}
Directory structure
Vendor
---composer
---guzzlehttp
---react
---swader
---autoload.php
'swader' folder
---diffbot-php-client
---src
---Abstracts
---Api
---Entity
---Exceptions
---Factory
---Interfaces
---Traits
---Diffbot.php
I am trying to call Diffbot class under Diffbot.php
, it contains the following namespaces:
namespace Swader\Diffbot;
use Swader\Diffbot\Api\Crawl;
use Swader\Diffbot\Api\Custom;
use Swader\Diffbot\Api\Search;
use Swader\Diffbot\Exceptions\DiffbotException;
use Swader\Diffbot\Api\Product;
use Swader\Diffbot\Api\Image;
use Swader\Diffbot\Api\Analyze;
use Swader\Diffbot\Api\Article;
use Swader\Diffbot\Api\Discussion;
use GuzzleHttp\Client;
use Swader\Diffbot\Factory\Entity;
use Swader\Diffbot\Interfaces\Api;
use Swader\Diffbot\Interfaces\EntityFactory;
/**
* Class Diffbot
*
* The main class for API consumption
*
* @package Swader\Diffbot
*/
class Diffbot
{
/** @var string The API access token */
protected static $token = null;
The autoload_psr4.php
file under composer/
folder:
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Swader\\Diffbot\\' => array($vendorDir . '/swader/diffbot-php-client/src'),
'React\\Promise\\' => array($vendorDir . '/react/promise/src'),
'GuzzleHttp\\Stream\\' => array($vendorDir . '/guzzlehttp/streams/src'),
'GuzzleHttp\\Ring\\' => array($vendorDir . '/guzzlehttp/ringphp/src'),
'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'),
);
I am trying to call the Diffbot
class from a php script that resides in the same directory as the vendor/
folder in the following manner:
require_once ('vendor/autoload.php');
error_reporting(E_ALL);
$diffbot = new Diffbot();
I solved my problem. I just added the following lines. I was confused about PHP namespace.
require_once __DIR__.'/vendor/autoload.php';
$foo = new \Swader\Diffbot\Diffbot('foo');
Try
require_once __DIR__.'/vendor/autoload.php';
use Swader\Diffbot\Diffbot;
$diffbot = new Diffbot();
See PHP doc for reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With