打包PHAR

张锋 1年前 访问:270 评论:0 关注:0

https://packagist.org/packages/fabpot/goutte

Goutte 是一个 PHP 的屏幕抓取和网络爬行库。
Goutte 提供了一个很好的 API 来抓取网站并从 HTML/XML 响应中提取数据。

首先把项目源代码下载

composer require fabpot/goutte

到目录下添加文件 phar-build-script.php

<?php

// Phar构建脚本

// 当前脚本的目录
$baseDir = __DIR__ . '/';
$vendorDir = $baseDir;

// 创建Phar文件
$phar = new Phar('goutte.phar', 0, 'goutte.phar');

// 设置Phar文件的元数据
$phar->setStub("#!/usr/bin/env php\n<?php\nPhar::mapPhar();\nrequire_once 'phar://goutte.phar/vendor/autoload.php';\n__HALT_COMPILER();");

// 添加自动加载器(Composer生成的)
$phar->addFile($baseDir . 'vendor/autoload.php');

// 递归地添加所有必要的文件
$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($vendorDir, RecursiveDirectoryIterator::SKIP_DOTS),
    RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($iterator as $file) {
    // 获取文件相对于vendor目录的路径
    $relativePath = substr($file->getPathname(), strlen($vendorDir));

    // 将文件添加到Phar中
    $phar->addFile($file->getPathname(), $relativePath);
}

// 压缩Phar文件(可选)
$phar->compressFiles(Phar::GZ);

// 设置可执行权限(如果需要)
chmod('goutte.phar', 0755);

echo "Phar file created successfully.\n";

run: php phar-build-script.php

评论

还没有人评论 ~

❤❤❤❤❤❤
心情
此图名叫《暗淡蓝点》
1990年2月14日,由旅行者1号拍摄,
蓝色的点就是地球
或许你看不清,因为地球在宇宙中太小了。