公司股票的Linkedin API返回false

时间:2016-12-13 12:37:24

标签: php linkedin-api

Linkedin API公司分享返回false。没有错误数据。相同的代码在我的本地工作完美。 False仅在服务器中返回。在linkedin app中正确添加了重定向网址。

这是使用访问令牌并发出共享更新请求的代码。

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

1 个答案:

答案 0 :(得分:0)

Linkedin正在寻找ssl证书。 所以我添加了禁用ssl检查的代码。

curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);