由于一些限制,我无法通过作曲家安装libphonenumber,所以我手动将其添加到项目的lib目录中。
当我尝试通过手动设置使用它时,我收到以下错误:
PHP 致命错误:在/home/cellulant/CODE/MSISDNVALIDATIONAPI/lib/libphonenumber/src/PhoneNumberUtil.php 第 404 行中找不到类 'libphonenumber\CountryCodeToRegionCodeMap'
尽管CountryCodeToRegionMap.php可以在libphonenumber/src目录中找到
。libphonenumber 目录位于我项目的 lib 目录中。以下是我的目录结构
├── docs
├── index.php
├── lib
│ └── libphonenumber
│ ├── composer.json
│ ├── docs
│ │ ...
│ ├── LICENSE
│ ├── METADATA-VERSION.txt
│ ├── README.md
│ └── src
│ ...
在我的索引.php中,我有这些:
<?php
include "lib/libphonenumber/src/PhoneNumberUtil.php";
$num = "0234567787";
try
{
$phoneUtil = libphonenumberPhoneNumberUtil::getInstance();
$numberProto = $phoneUtil->parse($num, "US");
var_dump($numberProto);
}
catch (Exception $ex)
{
echo "Exception: " . $ex->getMessage() . "n";
}
根据libphonenumber-php文档,如果您决定在没有作曲家的情况下使用它,您也可以使用任何符合PSR4(http://www.php-fig.org/psr/psr-4/(的自动加载器。
此版本的作者@giggsey说您可能需要使用区域设置库(https://github.com/giggsey/Locale(。这取代了 php-intl 扩展
给定您的目录结构,您的自动加载器(例如 autoload.php(并假设它位于您的 src/目录中可能如下所示:
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;
}
});
然后,您可以使用...
require __DIR__ . "autoload.php";
try
{
$phoneUtil = libphonenumberPhoneNumberUtil::getInstance();
//code...
}
catch(NumberParseException $ex)
{
//code ...
}
您可能还必须在自动加载中加载区域设置库.php类似。libphonenumber-php 需要 mbstring 扩展名。
看看中的例子。
libphonenumber/README
libphonenumber/docs/
据我所知,您有 3 个选择:
-
手动要求/包含所有需要的类。您已经包含"PhoneNumberUtil.php",但您还应该包括"CountryCodeToRegionCodeMap.php">
-
在 php 中实现自己的自动加载器:http://php.net/manual/en/language.oop5.autoload.php
-
使用作曲家自动加载器。如果您没有shell访问权限,则可以在本地运行命令并将所有内容ftp到Web主机