-1

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;
    }
}
  • What is the function definition of `makeRequest`? Check: http://stackoverflow.com/questions/8227909/curl-exec-always-returns-false – ʰᵈˑ Dec 13 '16 at 13:52
  • Thanks for the reply. I have added makeRequest function. The same functions working perfect in my local. Whenever some data miss or access token goes wrong, getting proper errors. But not in my server – Afsal Yoonus Dec 14 '16 at 05:04
  • thanks.. I got this error ''SSL certificate problem: unable to get local issuer certificate'' ''60'' – Afsal Yoonus Dec 14 '16 at 05:23

1 Answers1

0

Linkedin was looking for ssl certificate. So i added the code for disabling ssl check.

curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49