How to validate a LinkedIn public profile url
Asked Answered
L

10

13

I am looking for a way to validate a link to make sure that it is pointing to a LinkedIn public profile page in PHP.

I have a website and I would like my users to be able to share their LinkedIn profile in their profile on my website.

Leifeste answered 9/12, 2011 at 19:4 Comment(1)
I created the class to validate the linkedin url.Banderole
F
5

Try something like this where $username is the linked-in username. You also can set $profileurl directly to the link given and verify with str_pos that is starts with http://www.linkedin.com/in/

$profileurl = "http://www.linkedin.com/in/".$username;

$fp = curl_init($profileurl);
$response = curl_exec($fp);
$response_code = curl_getinfo($fp, CURLINFO_HTTP_CODE);
$validprofile = ($response_code == 200);

$validprofile will be a boolean indicating if the profile is valid.

Fiberboard answered 9/12, 2011 at 19:16 Comment(1)
don't forget /in/username is only if the user has chosen a custom url. Otherwise it looks more like /pub/fname-lname-etc/4/72/8a0Bondswoman
B
6

I've found a number of ways the profile url can look like:

http://uk.linkedin.com/pub/some-name/1/1b3/b45/
http://nl.linkedin.com/pub/other-name/11/223/544
http://www.linkedin.com/in/aname/
http://www.linkedin.com/in/another-name
http://linkedin.com/in/name
http://nl.linkedin.com/in/name
http://nl.linkedin.com/in/name/

I've used this regex to describe it:

^https?://((www|\w\w)\.)?linkedin.com/((in/[^/]+/?)|(pub/[^/]+/((\w|\d)+/?){3}))$

It is not strict-strict but it got me home.

edit
- Added https support

Bearing answered 10/10, 2013 at 7:22 Comment(3)
I'm seeing a form that looks like https://www.linkedin.com/profile/view?id=12345678 now.Ced
it wont work if url is like linkedin.com/in/namehttp: linkedin.com/in/namewww. linkedin.com/in/namehttps:Readytowear
I mean what if user puts multiple http: or www. or https:Readytowear
Z
6

Following expression will help you with all patterns:

((https?:\/\/)?((www|\w\w)\.)?linkedin\.com\/)((([\w]{2,3})?)|([^\/]+\/(([\w|\d-&#?=])+\/?){1,}))$

Demo

https://regex101.com/r/oT7iM2/5

Zhukov answered 20/9, 2015 at 12:45 Comment(1)
LinkedIn profile URLs can contain non-latin characters, and can be provided to you both as plain text or URL-encoded by the user. I.e. https://www.linkedin.com/in/святий-миколай-bbc1aaa or https://linkedin.com/in/%D1%81%D0%B2%D1%8F%D1%82%D0%B8%D0%B9-%D0%BC%D0%B8%D0%BA%D0%BE%D0%BB%D0%B0%D0%B9-bbc1aaa In both these cases this expression will fail. It works well for latin use cases though. For the url-encoded URLs, simply adding the % to the character class [\w|\d-&#?=] can help.Tjirebon
F
5

Try something like this where $username is the linked-in username. You also can set $profileurl directly to the link given and verify with str_pos that is starts with http://www.linkedin.com/in/

$profileurl = "http://www.linkedin.com/in/".$username;

$fp = curl_init($profileurl);
$response = curl_exec($fp);
$response_code = curl_getinfo($fp, CURLINFO_HTTP_CODE);
$validprofile = ($response_code == 200);

$validprofile will be a boolean indicating if the profile is valid.

Fiberboard answered 9/12, 2011 at 19:16 Comment(1)
don't forget /in/username is only if the user has chosen a custom url. Otherwise it looks more like /pub/fname-lname-etc/4/72/8a0Bondswoman
H
4

I use another regular expression more permisive:

^(http(s)?:\/\/)?([\w]+\.)?linkedin\.com\/(pub|in|profile)

It includes URLs without squeme and all samples from other answers. You can make any variation here http://regex101.com/r/vE8tV7

Hardnett answered 28/5, 2014 at 16:25 Comment(2)
This breaks for search urls like linkedin.com/pub/dir/+/semnani/us-49-greater-los-angeles-areaAmbivert
What about this one: regex101.com/r/vE8tV7/38 Only pub is missing as until now, I haven't found any url like that.Saveall
T
3

This is tested regular expression that I use on my website. I fit all the variations that currently exist.

var linkedin=/(https?)?:?(\/\/)?(([w]{3}||\w\w)\.)?linkedin.com(\w+:{0,1}\w*@)?(\S+)(:([0-9])+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
Termite answered 20/11, 2013 at 19:23 Comment(2)
How can you make http/https/ftp protocol optional in this? what if someone just enters www.linkedin.com/their-profile-name, without http. Apart from that this works.Lanchow
@NalinAgrawal You can use any URL combination and you will get right result. I change a bit code but pattern is the same. If you understand regex, you will se that work with or without http, https and wwwGasper
V
2

There a very nice examples on the developer API pages http://developer.linkedin.com/

This http://developer.linkedin.com/plugins/member-profile-plugin could be what your looking for.

Vasilek answered 9/12, 2011 at 19:18 Comment(2)
FYI: URL of developer page is now invalidCloudcapped
Downvoted for invalid link.Lycia
C
2

This covers most LinkedIn profile URLs (Javascript RegEx):

// Generic RegEx exact match validator
export const isRegexExactMatch = (value, regexp) => {
  const res = value.match(regexp);
  return res && res[0] && res[0] === res.input;
};

const isLinkedinProfileUrl = (value) => {
  const linkedInProfileURLRegExp =
    '(https?:\\/\\/(www.)?linkedin.com\\/(mwlite\\/|m\\/)?in\\/[a-zA-Z0-9_.-]+\\/?)';
  return !!isRegexExactMatch(value, linkedInProfileURLRegExp);
};
Circular answered 4/8, 2020 at 23:11 Comment(1)
is show me error when I put it in my code, I think you add extra "/" updated regex it /https?:\/\/(www.)?linkedin.com\/(mwlite\/|m\/)?in\/[a-zA-Z0-9_.-]+\/?/Quackery
Q
1

I have this for all kind of URLs for Linkedin for javascript

export const isValidLinkedinUrl = (url) => {
  return /(https?:\/\/(www.)|(www.))?linkedin.com\/(mwlite\/|m\/)?in\/[a-zA-Z0-9_.-]+\/?/.test(url);
};

only RegExp

/(https?:\/\/(www.)|(www.))?linkedin.com\/(mwlite\/|m\/)?in\/[a-zA-Z0-9_.-]+\/?/
Quackery answered 27/6, 2021 at 20:53 Comment(0)
H
0

Use a regex to ensure that the link matches the form taken by linkedin public profiles.

Hombre answered 9/12, 2011 at 19:13 Comment(0)
M
0

I've simply used the following regex:

http(s)?:\/\/([w]{3}\.)?linkedin\.com\/in\/([a-zA-Z0-9-]{5,30})\/?

According to the latest Linkedin documentation custom profile url can have 5-30 letters or numbers.

It works for the following list of url:

https://www.linkedin.com/in/your-profile-5-30-length/
https://linkedin.com/in/your-profile-5-30-length/
http://www.linkedin.com/in/your-profile-5-30-length/
http://linkedin.com/in/your-profile-5-30-length/
Mishandle answered 22/6, 2017 at 9:20 Comment(2)
The documentation still says this, but it is outdated or inaccurate. linkedin.com/in/eee linkedin.com/in/abcSelfness
Note that https://www.linkedin.com/in/%D0%B0%D0%BB%D0%B5%D0%BA%D1%81%D0%B0%D0%BD%D0%B4%D1%80-%D0%B1%D0%BE%D0%B1%D1%8B%D0%BB%D0%B5%D0%B2-45456994/ is valid, which (since most systems will parse the UTF-8 cyrillic unicode chars as, say, %NN chars) is way more than 30 chars, or even 90 chars.Rockaway

© 2022 - 2024 — McMap. All rights reserved.