1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Borfast\Socializr\Connectors; |
4
|
|
|
|
5
|
|
|
use Borfast\Socializr\Exceptions\ExpiredTokenException; |
6
|
|
|
use Borfast\Socializr\Exceptions\GenericPostingException; |
7
|
|
|
use Borfast\Socializr\Post; |
8
|
|
|
use Borfast\Socializr\Profile; |
9
|
|
|
use Borfast\Socializr\Page; |
10
|
|
|
use Borfast\Socializr\Response; |
11
|
|
|
use Borfast\Socializr\Connectors\AbstractConnector; |
12
|
|
|
use OAuth\Common\Storage\TokenStorageInterface; |
13
|
|
|
|
14
|
|
|
class LinkedinPage extends AbstractConnector |
15
|
|
|
{ |
16
|
|
|
public static $provider = 'linkedin'; |
17
|
|
|
|
18
|
|
|
public function post(Post $post) |
19
|
|
|
{ |
20
|
|
|
$page_id = $post->options['page_id']; |
21
|
|
|
$path = '/companies/'.$page_id.'/shares?format=json'; |
22
|
|
|
$method = 'POST'; |
23
|
|
|
$params = [ |
24
|
|
|
'visibility' => [ |
25
|
|
|
'code' => 'anyone' |
26
|
|
|
], |
27
|
|
|
'comment' => '', |
28
|
|
|
'content' => [ |
29
|
|
|
'title' => $post->title, |
30
|
|
|
'submitted-url' => $post->url, |
31
|
|
|
'description' => $post->body, |
32
|
|
|
], |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
if (!empty($post->media)) { |
36
|
|
|
$params['content']['submitted-image-url'] = $post->media[0]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$params = json_encode($params); |
40
|
|
|
|
41
|
|
|
// Linkedin API requires the Content-Type header set to application/json |
42
|
|
|
$header = ['Content-Type' => 'application/json']; |
43
|
|
|
$result = $this->request($path, $method, $params, $header); |
|
|
|
|
44
|
|
|
|
45
|
|
|
// The response comes in JSON |
46
|
|
|
$json_result = json_decode($result, true); |
47
|
|
|
|
48
|
|
View Code Duplication |
if (isset($json_result['status']) && $json_result['status'] != 200) { |
49
|
|
|
$msg = "Error posting to Linkedin page. Error code from Linkedin: %s. Error message from Linkedin: %s"; |
50
|
|
|
$msg = sprintf($msg, $json_result['errorCode'], $json_result['message']); |
51
|
|
|
|
52
|
|
|
if ($json_result['status'] == '401') { |
53
|
|
|
throw new ExpiredTokenException($msg); |
54
|
|
|
} else { |
55
|
|
|
throw new GenericPostingException($msg, $json_result['status']); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$response = new Response; |
60
|
|
|
$response->setRawResponse(json_encode($result)); |
61
|
|
|
$response->setProvider(static::$provider); |
62
|
|
|
$result_json = json_decode($result); |
63
|
|
|
$response->setPostId($result_json->updateKey); |
64
|
|
|
$response->setPostUrl($result_json->updateUrl); |
65
|
|
|
|
66
|
|
|
return $response; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
View Code Duplication |
public function getProfile() |
71
|
|
|
{ |
72
|
|
|
$path = '/people/~:(id,first-name,last-name,maiden-name,public-profile-url,formatted-name,num-connections,email-address,num-recommenders)?format=json'; |
73
|
|
|
$response = $this->service->request($path); |
74
|
|
|
$profile_json = json_decode($response, true); |
75
|
|
|
|
76
|
|
|
$mapping = [ |
77
|
|
|
'id' => 'id', |
78
|
|
|
'email' => 'emailAddress', |
79
|
|
|
'name' => 'formattedName', |
80
|
|
|
'first_name' => 'firstName', |
81
|
|
|
'middle_name' => 'maidenName', |
82
|
|
|
'last_name' => 'lastName', |
83
|
|
|
// 'username' => 'username', |
|
|
|
|
84
|
|
|
'link' => 'publicProfileUrl' |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
$profile = Profile::create($mapping, $profile_json); |
88
|
|
|
$profile->provider = static::$provider; |
89
|
|
|
$profile->raw_response = $response; |
90
|
|
|
|
91
|
|
|
return $profile; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getStats() |
95
|
|
|
{ |
96
|
|
|
$path = 'companies/'.$this->id.':(id,num-followers)?format=json'; |
97
|
|
|
$response = json_decode($this->request($path)); |
98
|
|
|
|
99
|
|
|
return $response->numFollowers; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: