Exemplo n.º 1
0
 public function post(Post $post)
 {
     $path = 'blog/' . $this->options['base_hostname'] . '/post';
     $method = 'POST';
     $params = [];
     if (!empty($post->tags)) {
         $params['tags'] = $post->tags;
     }
     if (empty($post->media)) {
         $body = '<p>' . $post->body . '</p>';
         $body .= '<strong>' . $post->url . '</strong>';
         $params['type'] = 'text';
         $params['title'] = $post->title;
         $params['body'] = $body;
     } else {
         $caption = '<h2>' . $post->title . '</h2>';
         $caption .= '<p>' . $post->body . '</p>';
         $caption .= '<strong>' . $post->url . '</strong>';
         $params['type'] = 'photo';
         $params['caption'] = $caption;
         $params['source'] = $post->media[0];
     }
     $result = $this->request($path, $method, $params);
     $response = new Response();
     $response->setRawResponse(json_encode($result));
     $result_json = json_decode($result);
     $response->setProvider('Tumblr');
     $response->setPostId($result_json->response->id);
     return $response;
 }
Exemplo n.º 2
0
 public function post(Post $post)
 {
     $msg = $post->title;
     $msg .= "\n\n";
     $msg .= $post->body;
     $msg = trim($msg);
     if (empty($post->media)) {
         $path = '/' . $this->getUid() . '/feed';
         $params = ['description' => '', 'link' => $post->url, 'message' => $msg];
     } else {
         $path = '/' . $this->getUid() . '/photos';
         $msg .= "\n";
         $msg .= $post->url;
         $params = ['url' => $post->media[0], 'caption' => $msg];
     }
     $method = 'POST';
     $result = $this->request($path, $method, $params);
     $json_result = json_decode($result, true);
     // If there's no ID, the post didn't go through
     if (!isset($json_result['id'])) {
         $msg = "Unknown error posting to Facebook profile.";
         throw new GenericPostingException($msg, 1);
     }
     $response = new Response();
     $response->setRawResponse($result);
     // This is already JSON.
     $response->setProvider('Facebook');
     $response->setPostId($json_result['id']);
     return $response;
 }
Exemplo n.º 3
0
 public function post(Post $post)
 {
     $page_id = $post->options['page_id'];
     $path = '/companies/' . $page_id . '/shares?format=json';
     $method = 'POST';
     $params = ['visibility' => ['code' => 'anyone'], 'comment' => '', 'content' => ['title' => $post->title, 'submitted-url' => $post->url, 'description' => $post->body]];
     if (!empty($post->media)) {
         $params['content']['submitted-image-url'] = $post->media[0];
     }
     $params = json_encode($params);
     // Linkedin API requires the Content-Type header set to application/json
     $header = ['Content-Type' => 'application/json'];
     $result = $this->request($path, $method, $params, $header);
     // The response comes in JSON
     $json_result = json_decode($result, true);
     if (isset($json_result['status']) && $json_result['status'] != 200) {
         $msg = "Error posting to Linkedin page. Error code from Linkedin: %s. Error message from Linkedin: %s";
         $msg = sprintf($msg, $json_result['errorCode'], $json_result['message']);
         if ($json_result['status'] == '401') {
             throw new ExpiredTokenException($msg);
         } else {
             throw new GenericPostingException($msg, $json_result['status']);
         }
     }
     $response = new Response();
     $response->setRawResponse(json_encode($result));
     $response->setProvider(static::$provider);
     $result_json = json_decode($result);
     $response->setPostId($result_json->updateKey);
     $response->setPostUrl($result_json->updateUrl);
     return $response;
 }
Exemplo n.º 4
0
 /**
  * @param Post $post
  * @return Response
  * @throws LinkedinForbiddenException
  * @throws LinkedinPostingException
  */
 public function post(Post $post)
 {
     $group_id = $post->options['group_id'];
     try {
         $token = $this->service->getStorage()->retrieveAccessToken('Linkedin')->getAccessToken();
     } catch (TokenNotFoundException $e) {
         throw new AuthorizationException();
     }
     $path = '/groups/' . $group_id . '/posts?format=json&oauth2_access_token=' . $token;
     $params = ['title' => $post->title, 'summary' => '', 'content' => ['title' => $post->title . ' @', 'submitted-url' => $post->url, 'description' => $post->body]];
     // Add media files, if they were sent.
     if (isset($post->media) && array_key_exists(0, $post->media)) {
         $params['content']['submitted-image-url'] = $post->media[0];
     }
     $params = json_encode($params);
     $url = 'https://api.linkedin.com/v1' . $path;
     // Linkedin API requires the Content-Type header set to application/json
     $options = ['headers' => ['Content-Type' => 'application/json'], 'body' => $params];
     $client = new Guzzle();
     try {
         $result = $client->post($url, $options);
     } catch (ClientException $e) {
         if ($e->getCode() >= 400) {
             throw new LinkedinForbiddenException($e);
         } else {
             throw $e;
         }
     }
     if ($result->getStatusCode() > 300) {
         $msg = "Error posting to Linkedin group. Error code from Linkedin: %s. Error message from Linkedin: %s";
         $msg = sprintf($msg, $result->status_code, json_decode($result->body)->message);
         throw new LinkedinPostingException($msg, $result->status_code);
     }
     $response = new Response();
     $response->setRawResponse($result);
     // This is already JSON.
     $response->setProvider(static::$provider);
     //$response->setPostId($result->getHeader('x-li-uuid'));
     // As amazing as it may sound, there's a three year old bug that LinkedIn
     // knows of but doesn't fix, which is simply the group posts URL is not
     // returned when we create the post, and when the post endpoint is queried
     // it returns a URL containing an incorrect domain: api.linkedin.com
     // instead of www.linkedin.com. They acknowledge this in the "Known Issues"
     // section of the groups API documentation and say the workaround is simple:
     // just swap the domains. Well, thanks for nothing. Would it be so hard for
     // them to return a public URL along with the response of the creation?...
     // So we need to make another API call to fetch the correct URL, because
     // it's not even possible to generate it manually.
     // Moderated groups don't return a 'location' header, so let's skip it if that's the case.
     $location = $result->getHeader('Location');
     if (!empty($location)) {
         $url = $location . ':(id,site-group-post-url)?format=json&oauth2_access_token=' . $token;
         $result = $client->get($url);
         $json = $result->json();
         $post_url = str_replace('api.linkedin.com/v1', 'www.linkedin.com', $json['siteGroupPostUrl']);
         $response->setPostUrl($post_url);
     }
     return $response;
 }
Exemplo n.º 5
0
 public function post(Post $post)
 {
     $path = '/statuses/update.json';
     $method = 'POST';
     $params = ['status' => $post->body];
     if (!empty($post->media)) {
         $params['media_ids'] = implode(',', $post->media);
     }
     $result = $this->request($path, $method, $params);
     $response = new Response();
     $response->setRawResponse(json_encode($result));
     $result_json = json_decode($result);
     $response->setProvider('Twitter');
     $response->setPostId($result_json->id_str);
     return $response;
 }
Exemplo n.º 6
0
 public function post(Post $post)
 {
     $path = '/people/~/shares?format=json';
     $method = 'POST';
     $params = ['visibility' => ['code' => 'anyone'], 'comment' => '', 'content' => ['title' => $post->title, 'submitted-url' => $post->url, 'description' => $post->body]];
     if (!empty($post->media)) {
         $params['content']['submitted-image-url'] = $post->media[0];
     }
     $params = json_encode($params);
     $result = $this->request($path, $method, $params);
     $response = new Response();
     $response->setRawResponse($result);
     // This is already JSON.
     $response->setProvider(static::$provider);
     $result_json = json_decode($result);
     $response->setPostId($result_json->updateKey);
     $response->setPostUrl($result_json->updateUrl);
     return $response;
 }