如何在 PHP 中处理连字符LinkedIn人员配置文件字段


How should hyphenated LinkedIn People profile fields be handled in PHP?

我的代码基于LinkedIn在 https://developer.linkedin.com/documents/code-samples 提供的示例PHP代码。

他们的代码和我的代码之间的差异很小。

示例代码仅请求两个配置文件字段:

$user = fetch('GET', '/v1/people/~:(firstName,lastName)');

我想要更多,所以我请求了 https://developer.linkedin.com/documents/profile-fields 中定义的所有这些配置文件字段:

$user = fetch('GET', '/v1/people/~:(firstName,lastName,headline,industry,specialties,summary,positions,public-profile-url,email-address,interests,publications,languages,skills,certifications,educations,num-recommenders,date-of-birth,honors-awards)');

请求成功并返回配置文件字段,我可以打印其中的大部分以显示。但是带连字符的字段(例如public-profile-url)似乎没有返回可用数据。我一定做错了什么,因为所有带连字符的配置文件字段都是空的,这不可能是巧合。

成功返回后,我尝试使用 print 语句来显示每个字段的结果:

print "<br />headline: $user->headline ";
print "<br />industry: $user->industry ";
print "<br /><br />public-profile-url: " . $user->{'public-profile-url'};
print "<br /><br />email-address: " . $user->{'email-address'};

等。

这适用于所有简单字符串的字段。对于某些人,我需要使用print_r因为它们是对象。

但是我根本没有获得连字符字段的任何数据。

如果我尝试print_r:

print_r($user->{'public-profile-url'});

结果为空白。

如果我尝试var_dump:

var_dump($user->{'public-profile-url'});

结果为 NULL。

对于我尝试过的所有带连字符的配置文件字段,这都是相同的:

公共配置文件网址电子邮件地址推荐人数量出生日期荣誉-奖项

只有带连字符的配置文件字段都是 NULL,这似乎太巧合了,所以我想我一定做错了其他事情。

有什么想法吗?

谢谢

道格

你的方法会起作用,但可以肯定的是...首先将字段分配给变量,然后将变量用于引用。

<?php
    $field = 'public-profile-url';
    echo $user->$field; // works
    echo $user->{'public-profile-url'}; // works also
?>

如果它们以 NULL 返回,则它们必须是 NULL

LinkedIn始终返回驼峰大小写字段,首字母小写,也称为驼峰回表示法。所以你会有publicProfileUrl,等等。相反,请记住,当您需要它们时,为每个字段使用符号first-name,last-name:用逗号分隔的复合词。不要在逗号之前或之后使用任何空格。

通过使用print_r($user);,您应该看到public-profile-url和其他人映射到的内容。