首页 > 解决方案 > Twilio - 发送未正确呈现的 vCard

问题描述

我正在尝试通过 Twilio (php) 发送联系人卡片 (.vcf),但似乎无法让 vcf 正确呈现。

.vcf 可以很好地保存到我的服务器,如果我随后从服务器下载文件并在我的 iPhone 或 Mac 上的 iMessage 中打开,它显示得很好。但是当通过 Twilio 发送时,我看到的只是纯文本的 .vcf 内容。

这是我正在使用的代码:

$account_sid = 'xxxxxxxx';
$auth_token  = 'xxxxxxxx';

$client      = new Services_Twilio($account_sid, $auth_token);

header('content-type: text/vcard');  
header('content-disposition: inline; filename= "testing2.vcf"'); 
                
try {

    $vCard = "BEGIN:VCARD\r";
    $vCard .= "VERSION:4.0\r";
    $vCard .= "N:Gump;Forrest;;Mr.;\r";
    $vCard .= "FN:Forrest Gump\r";
    $vCard .= "ORG:Bubba Gump Shrimp Co.\r";
    $vCard .= "TITLE:Shrimp Man\r";
    $vCard .= "PHOTO;MEDIATYPE=image/gif:http://www.example.com/dir_photos/my_photo.gif\r";
    $vCard .= "TEL;TYPE=work,voice;VALUE=uri:tel:+1-111-555-1212\r";
    $vCard .= "TEL;TYPE=home,voice;VALUE=uri:tel:+1-404-555-1212\r";
    $vCard .= "EMAIL:forrestgump@example.com\r";
    $vCard .= "END:VCARD";
 
    $path = '/var/www/xxxx/xxxx/testing2.vcf';
    file_put_contents($path, $vCard);

    $client->account->messages->create(array(
        'To' => $user_mobile,
        'MediaUrl' => ['https://www.example.com/vcfs/testing2.vcf'],
        'From' => "xxxxxxxxxxxx"
    ));
    
}

catch (\Services_Twilio_RestException $e) {
    
}

编辑

这是我从 MediaURL 中使用的 URL 得到的 curl 响应,表明它确实是 vcard 格式。

HTTP/1.1 200 OK
Date: Wed, 24 Feb 2021 04:13:29 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Tue, 23 Feb 2021 05:28:59 GMT
ETag: "15f-5bbfa320d710f"
Accept-Ranges: bytes
Content-Length: 351
Content-Type: text/vcard

标签: phptwiliovcf-vcard

解决方案


确保为您的 vCard 提供服务的端点设置以下标头:

  • Content-Type: text/vcard,
  • Cache-Control: no-cache
  • Content-Disposition: inline; filename="filename.vcf"

另请参阅此 SO 问题:生成 VCard 并通过 Twilio 发送


推荐阅读