在 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)');
请求成功,配置文件字段已 returned,我可以打印其中的大部分来显示。但是带连字符的字段,例如 public-profile-url
似乎不是 returning 可用数据。我一定是做错了什么,因为 所有 带连字符的个人资料字段都是空的,这不可能是巧合。
成功后return我尝试使用打印语句显示每个字段的结果:
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。
这对于 所有 我试过的带连字符的配置文件字段都是一样的:
public-简介-url
电子邮件地址
推荐人数
出生日期
荣誉奖项
只是带连字符的配置文件字段都为 NULL,这似乎太巧合了,所以我想我一定是做错了什么。
有什么想法吗?
谢谢,
道格
您的方法可行,但要确定...首先将字段分配给变量,然后使用该变量作为引用。
<?php
$field = 'public-profile-url';
echo $user->$field; // works
echo $user->{'public-profile-url'}; // works also
?>
如果它们返回为 NULL,则它们必须为 NULL
通过使用 print_r($user);
,您应该看到 public-profile-url
和其他映射到什么。
LinkedIn returns 始终使用驼峰式字段,首字母小写,也称为 camelBack 表示法。所以你会有 publicProfileUrl
,等等。请记住,当你需要它们时,对每个字段使用符号 first-name,last-name
:用逗号分隔的复合词。不要在逗号前后使用任何 space。
我的代码基于 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)');
请求成功,配置文件字段已 returned,我可以打印其中的大部分来显示。但是带连字符的字段,例如 public-profile-url
似乎不是 returning 可用数据。我一定是做错了什么,因为 所有 带连字符的个人资料字段都是空的,这不可能是巧合。
成功后return我尝试使用打印语句显示每个字段的结果:
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。
这对于 所有 我试过的带连字符的配置文件字段都是一样的:
public-简介-url 电子邮件地址 推荐人数 出生日期 荣誉奖项
只是带连字符的配置文件字段都为 NULL,这似乎太巧合了,所以我想我一定是做错了什么。
有什么想法吗?
谢谢,
道格
您的方法可行,但要确定...首先将字段分配给变量,然后使用该变量作为引用。
<?php
$field = 'public-profile-url';
echo $user->$field; // works
echo $user->{'public-profile-url'}; // works also
?>
如果它们返回为 NULL,则它们必须为 NULL
通过使用 print_r($user);
,您应该看到 public-profile-url
和其他映射到什么。
LinkedIn returns 始终使用驼峰式字段,首字母小写,也称为 camelBack 表示法。所以你会有 publicProfileUrl
,等等。请记住,当你需要它们时,对每个字段使用符号 first-name,last-name
:用逗号分隔的复合词。不要在逗号前后使用任何 space。