为什么某些 LinkedIn 个人资料 API 字段在 PHP 中成功 return 后显示时会出现问题?

Why are certain LinkedIn Profile API fields causing problems when being displayed after a successful return in PHP?

首先我要承认我在 PHP 方面没有经验,而且对 LinkedIn API 还是个新手。但是我昨天设置了一个新的 Apache/PHP 服务器并成功地让用户 (1) 授予访问他们的 r_fullprofile 和 r_emailaddress 的权限,(2) 交换身份验证代码一个 60 天的访问令牌,以及 (3) 进行初始调用以检索用户的 LinkedIn 个人资料并显示结果。所以我对此很兴奋。

但并非一切都有效,这就是我想问的问题。

我的代码基于 LinkedIn 在 https://developer.linkedin.com/documents/code-samples 上提供的示例 PHP 代码。 (小问题,因为我是 PHP 的新手,但在我看来他们在代码末尾遗漏了一个结束 ?> 。也许在 PHP文件?反正我放着以防万一。)

他们的代码和我的代码差别不大。

(1) 他们的代码以这种方式定义范围:

define('SCOPE',        'r_basicprofile r_emailaddress');

但是我要求用户访问他们的完整个人资料,所以我这样定义范围:

define('SCOPE',        'r_fullprofile r_emailaddress');

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

$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)');

请求成功,配置文件字段已 return 编辑并显示。但是我想问的一些字段有一些问题。

成功后return我用print语句显示结果:

print "<br />Hello $user->firstName $user->lastName. We have successfully retrieved the following    information from your LinkedIn Profile.<br />";
print "<br />headline: $user->headline ";
print "<br />industry: $user->industry ";
print "<br />specialties: $user->specialties ";
print "<br />summary: $user->summary ";
// print "<br />positions: $user->positions ";
print "<br />public-profile-url: $user->public-profile-url ";
print "<br />email-address: $user->email-address ";
print "<br />publications: $user->publications ";
// print "<br />languages: $user->languages ";
// print "<br />skills: $user->skills ";
print "<br />certifications: $user->certifications ";
// print "<br />educations: $user->educations ";
print "<br />num-recommenders: $user->num-recommenders ";
print "<br />date-of-birth: $user->date-of-birth ";
print "<br />honors-awards: $user->honors-awards ";
print "<br /><br />Thanks. We hope this helps make your experience more professionally rewarding for you.";

上面所有打印语句的显示结果如下(尽管为了简洁起见我在这里缩短了摘要):

Hello Doug Lerner. We have successfully retrieved the following information from your LinkedIn Profile.

headline: Founder & CEO, Elliptics, Ltd.
industry: Computer Software
specialties:
summary: Although I am a U.S. citizen, since 1983 I have resided in Tokyo, where I hold permanent residency. During my first 7 years in Japan, I worked for Fujitsu, the largest computer company in Japan. At Fujitsu I was Technical Director for Fujitsu's 1990 Osaka World's Fair Omnimax/IMAX production "Echoes of the Sun". For 14 years at Nippon Electronics College, I lectured in Mathematics and Scientific Simulation in the Department of Computer Graphics, where I also served as the Director of the Virtual Reality Seminar program.
public-profile-url: -profile-url
email-address: -address
publications:
certifications:
num-recommenders: -recommenders
date-of-birth: -of-birth
honors-awards: -awards

Thanks. We hope this helps make your experience more professionally rewarding for you.

我有一个主要问题是关于我在上面注释掉的打印语句。最后一次打印是对我的确认,我已成功到达脚本的末尾。但是,如果包含任何这些注释掉的打印语句,脚本会立即停止,不会再打印任何内容。

正如我提到的,我对 PHP 没有那么多经验,所以一个问题是我不知道那些特定的打印语句有什么问题。没有显示错误消息(我怀疑默认设置会抑制错误消息,因此它们不会显示给用户)。所以我想知道一件事,尤其是在测试时,遇到了什么错误。

为什么那些特定的配置文件字段:positions, languages, skills and educations 会导致问题?由于配置文件字段名称是复数,一个猜测是 returned 可能不是一个可以用 print 显示的简单字符串,它们可能是数组或其他需要不同方式的东西显示它们。我没有在文档中看到有关这些特定字段的值的更多定义,因此如果有人可以帮助我提供有关它们的信息,我将不胜感激。

一些 "plural sounding fields" 只是 returned 空白:specialties, certifications, publications 所以我猜那些有空值而不是一些特殊格式的值列表以特殊方式显示。

其他打印语句有效,并显示实际的配置文件内容(例如,我的 industry 打印为 Computer Software(但显然不是 PHP),或者在某些情况下是一个字符串前面有一个减号。例如,public-profile-url: -profile-url

前面有负号的数值是什么意思?为什么我的 public-profile-url return 被编辑成 -profile-url?是隐藏字段的意思吗?我不知道我什至可以把它藏在哪里。

如果有人能提供任何信息,我将不胜感激。

你有一大堆问题(本网站通常不鼓励这样做),但我会尽力一一解决。

Why would those particular profile fields: positions, languages, skills and educations cause a problem? Since the profile field names are plurals, one guess is that maybe what is returned is not a simple string which can be shown with print and they are maybe arrays or something else which requires a different way to display them. I didn't see more definitions about the values of those particular fields in the docs, so if somebody could help me with information about them I would be grateful.

你是对的;它们可能是数组,也可能是对象。而不是这个:

print "<br />positions: $user->positions

尝试var_dump($user->positions);。这会告诉你你在做什么,所以你会知道如何正确处理它。这可能包含在 API 文档中,尽管我无法获取您发布的 link 以加载。

Some "plural sounding fields" just returned blanks: specialties, certifications, publications so I'm guessing those had null values rather than some specially formatted list of values to be shown in a special way.

也许;也许不吧。您应该验证数据。作为起点,使用 var_dumpprint_r 看看你有什么。

As for the values with a minus in front of it, what does that mean? Why is my public-profile-url returned as -profile-url? Does it mean a hidden field? I don't see where I can even hide that.

你得到连字符 (-) 是因为你没有使用正确的语法。例如,

$user->date-of-birth

不正确。它被视为

{$user->date}-of-birth

即变量$user->date后跟文字-of-birth,这是错误的。要么您使用了错误的 属性 名称,要么您需要使用以下语法:$user->{'date-of-birth'}.

要在 printecho 中使用带连字符的名称 属性,您确实应该将其连接成一个字符串。尝试内联处理它,如下所示:

echo "blah $user->{'hyphenated-prop'}";

效果不佳。试试这个,而不是:

print "<br/>text " . $user->{'hyphenated-prop'};

Here's a demo.