-3

I would like to test the type of phone numbers, so i found libphonenumber-for-php. But i don't understand how to use it.

In this page: https://github.com/giggsey/libphonenumber-for-php it saying that i need to install it via composer, but where? On my ubuntu server?

i just need to know if a number is a mobile number or not, like in this example: https://github.com/practo/libphonenumber-for-php/issues/15

So, i need to understand how to get an instance of the librairie.

Thanks for your help

DouzeBri DouzeBra
  • 117
  • 1
  • 2
  • 13
  • it seems that i asked a stupid question :( – DouzeBri DouzeBra Jun 11 '14 at 09:48
  • There are no stupid questions, just questions asked on a wrong place. This is obviously not a SO question. – Dexa Jun 11 '14 at 09:50
  • This question appears to be off-topic because SO it's not the place to ask "how to do things" without a specific problem to be solve (that does not involve writing a whole tutorial as an answer). – Nunser Jun 11 '14 at 19:17

2 Answers2

3

Composer is a package manager for PHP, it downloads packages from the packagist.com repository and places them in a nice structure along with an autoloader that would allow you to easily use them.

You can download composer as documented in its official website : https://getcomposer.org/download/

After doing so I usually place it in the path folder in order to be able to call it from anywhere, though that's just a preference.

Once you have Composer up and running, create a composer.json file in which you'll put the packages you'd like to use for your web application - in this case "giggsey/libphonenumber-for-php", without the quotes. The github link you posted contains the exact json structure :

{
    "require": {
        "giggsey/libphonenumber-for-php": "~6.0"
    }
}

Finally, call composer.phar install on the console and it will take care of everything for you.

To instantiate the library, first include the vendor/autoload.php file then simply follow the instructions in the "Quick examples" chapter of the github link.

Note that everything I said about composer is detailed on the official documentation : https://getcomposer.org/doc/01-basic-usage.md

Hassan
  • 609
  • 4
  • 9
2

Composer is a dependency manager. Please Read the Documentation for it: https://getcomposer.org/doc/. Basicly it does the following task for you automaticly:

An Autoloader could be:

spl_autoload_register( function ($className) {
    include str_replace('\\', DIRECTORY_SEPARATOR, $className) . DIRECTORY_SEPARATOR . '.php';
    }
});

Make sure you configure it according to your project strcuture. Look here for more Details about Auto Loading: What is Autoloading; How do you use spl_autoload, __autoload and spl_autoload_register?

You also want to make sure you register it before you use the class:

//Registering
include('Autoloader.php')

//Use the Class
$swissNumberStr = "044 668 18 00";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
try {
    $swissNumberProto = $phoneUtil->parse($swissNumberStr, "CH");
    var_dump($swissNumberProto);
} catch (\libphonenumber\NumberParseException $e) {
    var_dump($e);
}
Community
  • 1
  • 1
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111