Linkedin API Company Share return false. There is no error data. Same code working perfect in my local. False returns only in server. Redirect url added properly in linkedin app.
This is the code which uses access token and make the share update request.
public function postToCompany($company_id,$content=array()){
$params['url'] = 'https://api.linkedin.com/v1/companies/'.$company_id.'/shares';
$params['method']='post';
$params['headers']['Content-Type']='application/json';
$params['headers']['x-li-format']='json';
$json = array('comment'=>$content['comment'] , 'visibility'=> array('code'=>'anyone'));
if(is_array($content) AND count($content)>0) {
// If the content of the post is specified (e.g., a link to a website), add it here
$json['content'] = array();
if(isset($content['content']['title'])){
$json['content']['title'] = $content['content']['title'];
}
if(isset($content['content']['submitted-url'])){
$json['content']['submitted-url'] = $content['content']['submitted-url'];
}
if(isset($content['content']['submitted-image-url'])){
$json['content']['submitted-image-url'] = $content['content']['submitted-image-url'];
}
if(isset($content['content']['description'])){
$json['content']['description'] = $content['content']['description'];
}
}
$params['args']=json_encode($json);
$result = $this->makeRequest($params);
return json_decode($result,true);
}
protected function makeRequest($params=array()){
$this->error = '';
$method=isset($params['method'])?$params['method']:'get';
$headers = isset($params['headers'])?$params['headers']:array();
$args = isset($params['args'])?$params['args']:'';
$url = $params['url'];
$url.='?';
if($this->access_token){
$url .= $this->access_token_name.'='.$this->access_token;
}
if($method=='get'){
$url.='&'.$this->preparePostFields($args);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if($method=='post'){
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->preparePostFields($args));
}elseif($method=='delete'){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
}elseif($method=='put'){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
}
if(is_array($headers) && !empty($headers)){
$headers_arr=array();
foreach($headers as $k=>$v){
$headers_arr[]=$k.': '.$v;
}
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers_arr);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
protected function preparePostFields($array) {
if(is_array($array)){
$params = array();
foreach ($array as $key => $value) {
$params[] = $key . '=' . urlencode($value);
}
return implode('&', $params);
}else{
return $array;
}
}