4

Due to some constraints, I'm unable to install libphonenumber via composer, so I've manually added it to my project's lib directory.
I'm getting the following error when I try to use it via manual setup:
PHP Fatal error: Class 'libphonenumber\CountryCodeToRegionCodeMap' not found in /home/cellulant/CODE/MSISDNVALIDATIONAPI/lib/libphonenumber/src/PhoneNumberUtil.php on line 404

This, despite the fact that CountryCodeToRegionMap.php can be found in libphonenumber/src directory

The libphonenumber directory is in my project's lib directory. The following is my directory structure

├── docs
├── index.php
├── lib
│   └── libphonenumber
│       ├── composer.json
│       ├── docs
│       │    ...
│       ├── LICENSE
│       ├── METADATA-VERSION.txt
│       ├── README.md
│       └── src
│           ...

In my index.php, I have these:

<?php

include "lib/libphonenumber/src/PhoneNumberUtil.php";

$num = "0234567787";

try 
{
    $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();

    $numberProto = $phoneUtil->parse($num, "US");
    var_dump($numberProto);
} 
catch (Exception $ex)
{
    echo "Exception: " . $ex->getMessage() . "\n";
}
kassold
  • 469
  • 3
  • 8
  • 21

2 Answers2

3

According the libphonenumber-php doc, you can also use any PSR4 (http://www.php-fig.org/psr/psr-4/) compliant autoloader if you decide to use this without composer.

The author of this version, @giggsey, says you may need to use the Locale library (https://github.com/giggsey/Locale). This replaces the php-intl extension

Given your directory structure, your autoloader (eg. autoload.php) and assuming it is in your src/ directory can look like this:

spl_autoload_register(function ($class) {

    //namespace prefix
    $prefix = 'libphonenumber';

    // base directory for the namespace prefix
    $base_dir = __DIR__ . '/../lib/libphonenumber/src/';

    // does the class use the namespace prefix?
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        // no, move to the next registered autoloader
        return;
    }

    // get the relative class name
    $relative_class = substr($class, $len);

    // replace the namespace prefix with the base directory, replace namespace
    // separators with directory separators in the relative class name, append
    // with .php
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

    // if the file exists, require it
    if (file_exists($file)) {
        require $file;
    }
});

Then you can import it using...

require __DIR__ . "autoload.php";
try
{
   $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
   //code...
}
catch(NumberParseException $ex)
{
   //code ...
}

You may also have to load the Locale library in the autoload.php similarly. The libphonenumber-php requires mbstring extension.

Take a look at the examples in..

libphonenumber/README
libphonenumber/docs/
1

As far as I know you have 3 options:

  1. Manually require/include all the needed classes. You've already included "PhoneNumberUtil.php", but you should also include "CountryCodeToRegionCodeMap.php"

  2. Implement your own auto loader in php: http://php.net/manual/en/language.oop5.autoload.php

  3. Use the composer autoloader. If you don't have shell access you can run the commands locally and ftp everything over to your webhost

Jimbolino
  • 404
  • 3
  • 8