我想知道创建vCard的最佳方法是什么。我不需要将它存储在我的服务器上,只需在用户想要下载时生成它。下面的代码有效,但对我来说似乎很笨拙,还有更好的方法吗?
<?php
function raf_create_vcard(){
$format_name = utf8_encode($name);
$format_email = utf8_encode($email);
$format_tel = utf8_encode($tel);
$format_fax = utf8_encode($fax);
$format_www = utf8_encode($www);
$format_address = utf8_encode($address);
return 'BEGIN%3AVCARD%0D%0AVERSION%3A4.0%0D%0AN%3A%3B'.$format_name.'%3B%3B%3B%0D%0AFN%3A'.$format_name.'%0D%0AEMAIL%3A'.$format_email.'%0D%0AORG%3A'.$format_name.'%0D%0ATEL%3A'.$format_tel.'%0D%0ATEL%3Btype%3DFAX%3A'.$format_fax.'%0D%0AURL%3Btype%3Dpref%3A'.$format_www.'%0D%0AADR%3A%3B'.$format_address.'%3B%3B%3B%3B%3BSpain%0D%0AEND%3AVCARD';
}
?>
生成链接:
<a href="data:text/plain;charset=UTF-8,<?php echo raf_create_vcard(); ?>" download="contact.vcf">Download</a>
答案 0 :(得分:1)
没有图书馆,你可以这样写:
function raf_create_vcard(){
return utf8_encode('BEGIN:VCARD
VERSION:4.0
N:;'.$name.';;;
FN:'.$name.'
EMAIL:'.$email.'
ORG:'.$name.'
TEL:'.$tel.'
TEL;type=FAX:'.$fax.'
URL;type=PREF:'.$www.'
ADR:;'.$address.';;;;;Spain
END:VCARD');
}
但是你应该为此目的使用PHP库。看看这个:https://github.com/jeroendesloovere/vcard。
use JeroenDesloovere\VCard\VCard;
$vcard = new VCard();
$vcard->addName($name);
$vcard->addCompany($name);
$vcard->addEmail($email);
$vcard->addPhoneNumber($tel);
$vcard->addPhoneNumber($fax, 'FAX');
$vcard->addURL($www, 'PREF');
$vcard->addAddress($address, null, null, null, null, null, 'Spain');
$vcardAsString = $vcard->getOutput();
答案 1 :(得分:0)
在 <a>
标签中添加网址链接,以生成类似Vcard的
<a href="vcard.php?id=1">Generate VCard</a>
您直接在php文件中编写代码 将以下代码保存在 vcard.php 文件
中<?php
require_once('config.php');
$sql="SELECT * FROM USER WHERE id=".$_GET['id'];
$result=mysql_fetch_row(mysql_query($sql));
// define here all the variable like $name,$image,$company_name & all other
header('Content-Type: text/x-vcard');
header('Content-Disposition: inline; filename= "'.$name.'.vcf"');
if($image!=""){
$getPhoto = file_get_contents($image);
$b64vcard = base64_encode($getPhoto);
$b64mline = chunk_split($b64vcard,74,"\n");
$b64final = preg_replace('/(.+)/', ' $1', $b64mline);
$photo = $b64final;
}
$vCard = "BEGIN:VCARD\r\n";
$vCard .= "VERSION:3.0\r\n";
$vCard .= "FN:" . $name . "\r\n";
$vCard .= "TITLE:" . $company_name . "\r\n";
if($email){
$vCard .= "EMAIL;TYPE=internet,pref:" . $email . "\r\n";
}
if($getPhoto){
$vCard .= "PHOTO;ENCODING=b;TYPE=JPEG:";
$vCard .= $photo . "\r\n";
}
if($mobile_no){
$vCard .= "TEL;TYPE=work,voice:" . $mobile_no . "\r\n";
}
$vCard .= "END:VCARD\r\n";
echo $vCard;
?>
答案 2 :(得分:0)
对于 Laravel
使用包安装....
composer require jeroendesloovere/vcard dev-master
然后在您的控制器中
public function downloadVcard(/* Request $request */)
{
$vcard = new VCard();
// define variables
$lastname = 'Desloovere';
$firstname = 'Jeroen';
$additional = '';
$prefix = '';
$suffix = '';
// add personal data
$vcard->addName($lastname, $firstname, $additional, $prefix, $suffix);
// add work data
$vcard->addCompany('Siesqo');
$vcard->addJobtitle('Web Developer');
$vcard->addRole('Data Protection Officer');
$vcard->addEmail('info@jeroendesloovere.be');
$vcard->addPhoneNumber(1234121212, 'PREF;WORK');
$vcard->addPhoneNumber(123456789, '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();
}
在 web.php 中路由
Route::get('/downloadcard', 'App\Http\Controllers\VCardController@downloadVcard');