为什么某些LinkedIn配置文件 API 字段在 PHP 中成功返回后显示时会导致问题


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配置文件并显示结果。所以我对此感到兴奋。

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

我的代码基于 https://developer.linkedin.com/documents/code-samples 提供的示例 PHP 代码LinkedIn。(小问题,因为我是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)');

请求成功,并返回并显示配置文件字段。但是我想问的一些领域存在一些问题。

成功返回后,我使用 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会导致问题?由于配置文件字段名称是复数形式,因此可以猜测返回的内容可能不是可以用print显示的简单字符串,它们可能是数组或其他需要不同方式显示它们的东西。我没有在文档中看到有关这些特定字段值的更多定义,因此如果有人可以帮助我提供有关它们的信息,我将不胜感激。

一些"复数声音字段"只是返回空白:specialties, certifications, publications所以我猜那些具有空值,而不是一些特殊格式的值列表,以特殊方式显示。

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

至于前面有减号的值,这是什么意思?为什么我的public-profile-url被退回为-profile-url?这是否意味着隐藏字段?我看不出我什至可以在哪里隐藏它。

如果人们可以提供任何信息,我将不胜感激。

你有一个问题(在这个网站上通常不鼓励),但我会尝试解决每一个问题。

为什么这些特定的个人资料字段:职位、语言、技能和教育会导致问题?由于配置文件字段名称是复数形式,因此猜测返回的内容可能不是可以通过打印显示的简单字符串,它们可能是数组或其他需要不同方式显示它们的东西。我没有在文档中看到有关这些特定字段值的更多定义,因此如果有人可以帮助我提供有关它们的信息,我将不胜感激。

你是对的;它们可能是数组,或者可能是对象。取而代之的是:

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

尝试var_dump($user->positions); 。这将向您显示您正在使用的内容,因此您将知道如何正确处理它。这可能在 API 文档中有所介绍,尽管我无法加载您发布的链接。

一些

"复数声音字段"只是返回空白:专业、认证、出版物,所以我猜这些字段具有空值,而不是一些特殊格式的值列表,以特殊方式显示。

也许吧;也许不是。应验证数据。首先,使用var_dumpprint_r查看您拥有的内容。

至于前面有减号的值,这是什么意思?为什么我的公共个人资料网址返回为 -个人资料网址?这是否意味着隐藏字段?我看不出我什至可以在哪里隐藏它。

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

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

这是一个演示