/** * Upload Image. * * @param \Userdesk\Submission\Classes\SubmissionImageItem $item; * * @return \Userdesk\Submission\Classes\SubmissionResult; */ public function uploadImage(SubmissionImageItem $item) { $facebook = $this->providerFromToken(); $params = array('source' => $facebook->fileToUpload($item->getImage()), 'title' => $item->getTitle(), 'message' => $item->getDescription()); try { $userId = $facebook->getUser(); $response = $facebook->post('/' . $userId . '/videos', $params); } catch (FacebookResponseException $e) { throw new InvalidUploadException($e->getMessage()); } catch (FacebookSDKException $e) { throw new InvalidUploadException($e->getMessage()); } catch (Exception $e) { throw new InvalidUploadException($e->getMessage()); } $graphNode = $response->getGraphNode(); if (!empty($graphNode['id'])) { $url = "https://www.facebook.com/photo.php?v=" . $graphNode['id']; return new SubmissionResult(true, '', $url); } return new SubmissionResult(false, 'Image Upload Failed'); }
/** * Upload Image. * * @param \Userdesk\Submission\Classes\SubmissionImageItem $item; * * @return \Userdesk\Submission\Classes\SubmissionResult; */ public function uploadImage(SubmissionImageItem $item) { $tw = $this->providerFromToken(); $image = file_get_contents($item->getImage()); $media_data = $tw->request('https://upload.twitter.com/1.1/media/upload.json', 'POST', array('command' => 'INIT', 'total_bytes' => strlen($image), 'media_type' => $item->getType())); $media = json_decode($media_data, true); $media_id = $media['media_id']; if ($media_id) { $tw->request('https://upload.twitter.com/1.1/media/upload.json', 'POST', array('command' => 'APPEND', 'segment_index' => 0, 'media_id' => $media_id, 'media_data' => base64_encode($image))); $tw->request('https://upload.twitter.com/1.1/media/upload.json', 'POST', array('command' => 'FINALIZE', 'media_id' => $media_id)); $response = $tw->request('statuses/update.json', 'POST', array('status' => $item->getTitle(), 'media_ids' => $media_id)); $status = json_decode($response, true); if (!empty($status['id']) && !empty($status['user'])) { $url = sprintf('https://twitter.com/%s/status/%s', $status['user']['screen_name'], $status['id']); return new SubmissionResult(true, '', $url); } return new SubmissionResult(false, 'Image Upload Failed'); } else { throw new InvalidUploadException($e->getMessage()); } }