PHP(ThinkPHP)批量导出vcf(vcard)名片

thinkphp 使用类库导出vcf

GitHub:vcard
安装

composer require jeroendesloovere/vcard

例子

<?php
namespace app\index\controller;

use JeroenDesloovere\VCard\VCard as RequVcard;
use think\Db;

class Vcard
{
    public function index()
    {
        // 保存位置
        $myfile = fopen(ROOT_PATH . "vcard.vcf", "w") or die("Unable to open file!");
        // 
        DB::table('info2')->chunk(100, function ($datas) use ($myfile) {
            foreach ($datas as $data) {
                $all_str = $this->make_vcard($data['name'], $data['mobile']);
                fwrite($myfile, $all_str);
            }
        });
        fclose($myfile);
    }
    public function make_vcard($name = '', $mobile = '')
    {
        if (empty($mobile)) {
            return '';
        }
        $vcard = new RequVcard();
        // define variables
        $lastname          = '';
        $firstname         = "$name";
        $additional        = '';
        $prefix            = '';
        $suffix            = '';
        // 这里可以不要,默认UTF-8,导出来windows可能会中文乱码,但是手机上不影响
        $vcard->setCharset = "GB2312";
        $vcard->addName($lastname, $firstname, $additional, $prefix, $suffix);
        $vcard->addCompany('Siesqo');
        $vcard->addJobtitle('Web Developer');
        $vcard->addRole('Data Protection Officer');
        $vcard->addEmail('[email protected]');
        $vcard->addPhoneNumber((int) $mobile, 'PREF;WORK');
        $vcard->addPhoneNumber((int) $mobile, 'WORK');
        $vcard->addAddress(null, null, 'street', 'worktown', null, 'workpostcode', 'Belgium');
        $vcard->addLabel('street, worktown, workpostcode Belgium');
        $vcard->addURL('http://www.jeroendesloovere.be');

        // $vcard->addPhoto(__DIR__ . '/landscape.jpeg');

        // return vcard as a string
        return $vcard->getOutput();
        // return vcard as a download
        // return $vcard->download();
    }
}

保存的文件vcard.vcf

BEGIN:VCARD
VERSION:3.0
REV:2018-06-13T09:37:24Z
N;CHARSET=utf-8:Desloovere;Jeroen;;;
FN;CHARSET=utf-8:Jeroen Desloovere
ORG;CHARSET=utf-8:Siesqo
TITLE;CHARSET=utf-8:Web Developer
ROLE;CHARSET=utf-8:Data Protection Officer
EMAIL;INTERNET:info@jeroendesloovere.be
TEL;PREF;WORK:1234121212
TEL;WORK:123456789
ADR;WORK;POSTAL;CHARSET=utf-8:;;street;worktown;;workpostcode;Belgium
LABEL:street, worktown, workpostcode Belgium
URL:http://www.jeroendesloovere.be
END:VCARD

猜你喜欢

转载自blog.csdn.net/hd2killers/article/details/80676727