0

I need to use LinkedIn API to share media. I currently arrived to get token and get profile data. So the problem is I'm not able to post media in that url "https://api.linkedin.com/v2/ugcPosts".

How to share on LinkedIn using REST-API v2?

I want to do that but in my case it's not working

I created a class with methods. I can't put all my code because is too long but I can detail you what is function role, so

login_url() -> Allow me to get code

set_access_token() -> Allow me to exchange code with access_token

set_user_urn - > Allow me to set user_urn usefull to post data in share function. My urn code is like XXXXXX-XXX. Tell me if it's good format

public function share() {
    $url = "https://api.linkedin.com/v2/ugcPosts" . $this->_access_token;

    $fields = '{
        "author": "urn:li:person:' . $this->_urn_id . '",
        "lifecycleState": "PUBLISHED",
        "specificContent": {
            "com.linkedin.ugc.ShareContent": {
                "shareCommentary": {
                    "text": "Hello World! This is my first Share on LinkedIn!"
                },
                "shareMediaCategory": "NONE"
            }
        },
        "visibility": {
            "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
        }
    }';

var_dump($fields);


    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    $httpCode = curl_getinfo($curl , CURLINFO_HTTP_CODE); // this results 0 every time
    $response = curl_exec($curl);

    if ($response === false) 
        $response = curl_error($curl);

    echo stripslashes($response);

    curl_close($curl);

}

I get the following error message:

{ "author": "urn:li:person:XXXXXX-XXX", "lifecycleState": "PUBLISHED", "specificContent": { "com.linkedin.ugc.ShareContent": { "shareCommentary": { "text": "Hello World! This is my first Share on LinkedIn!" }, "shareMediaCategory": "NONE" } }, "visibility": { "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC" } }" {"message":"Request timed out","status":504}

TheWildHealer
  • 1,546
  • 1
  • 15
  • 26
WyllisTM
  • 61
  • 8
  • Might be worth checking what `var_dump($fields)` returns. Also your URL doesn't look right (but unsure), having the access token directly on the end of it withing any separator (unless there's one in the $this->_access_token string? – Jonnix May 09 '19 at 12:56
  • Thank you for your fast response, I put update at top. I tried to do what you said but there is another error : "Request timed out" – WyllisTM May 09 '19 at 13:03
  • I try to make request in postman and it work's well, so I think i made a mistake with curl. Can you tell me if you see any mistakes. Thank you a lot – WyllisTM May 09 '19 at 13:22

1 Answers1

0

I put this header :

$headers = array('Content-Type: text/plain');

According to this post = How to post raw body data with curl?, CURL set by default 'Content-Type: application/x-www-form-urlencoded'.

// share function

public function share() {
$url = "https://api.linkedin.com/v2/ugcPosts?oauth2_access_token=" . $this->_access_token;

        $headers = array('Content-Type: text/plain');

        $fields = '{
            "author": "urn:li:person:XXXXXX-XXX",
            "lifecycleState": "PUBLISHED",
            "specificContent": {
                "com.linkedin.ugc.ShareContent": {
                    "shareCommentary": {
                        "text": "Hello World! This is my first Share on LinkedIn2!"
                    },
                    "shareMediaCategory": "NONE"
                }
            },
            "visibility": {
                "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
            }
        }';

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        $httpCode = curl_getinfo($curl , CURLINFO_HTTP_CODE); // this results 0 every time
        $response = curl_exec($curl);

        if ($response === false) 
            $response = curl_error($curl);

        curl_close($curl);

        return stripslashes($response);

    }
WyllisTM
  • 61
  • 8