params = $params; $this->httpClient = $httpClient; $this->requestStack = $requestStack; } /** * @param $listValueId * * @return mixed */ public function getUser() { try { $client = new Client(['base_uri' => 'https://graph.facebook.com']); $response = $client->request('GET', '/oauth/access_token', [ 'query' => [ "client_id" => $this->params->get('facebook_id'), "client_secret" => $this->params->get('facebook_secret'), "grant_type" => "client_credentials" ] ]); $data = json_decode($response->getBody()->getContents(), true); $result = $data['access_token']; } catch(Exception $e) { $result = $e; } return $result; } /** * @param $listValueId * * @return mixed */ public function getToken() { $userToken = $this->getUser(); try { $client = new Client(['base_uri' => 'https://graph.facebook.com']); $response = $client->request('GET', '/109453961359599', [ 'query' => [ "fields" => "access_token", "access_token" => $userToken ] ]); $data = json_decode($response->getBody()->getContents(), true); /*$page = array_filter($data['data'], function ($d) { return $d['id']; });*/ $result = $data; } catch(Exception $e) { $result = $e; } return $result; } public function post(string $message, array $medias) { $config = [ "app_id"=> $this->params->get('facebook_id'), "app_secret"=> $this->params->get('facebook_secret'), "default_access_token"=> $this->params->get('facebook_token'), "page_id"=> "109453961359599", "enable_beta_mode"=> true, "default_graph_version"=> "v2.8", # Optional, also supports "mcrypt" and "urandom". Default uses the latest graph version. "persistent_data_handler"=> "memory", # Optional, also supports "session". Default is "memory". "pseudo_random_string_generator"=> "openssl", # Optional, also supports "mcrypt" and "urandom". Default is "openssl". "http_client_handler"=> "curl" ]; $fb = new Facebook($config); $data['message'] = $message; if(is_array($filePath) && !empty($filePath)){ $data["source"] = $fb->fileToUpload($filePath); } try { $result = $fb->post('/me/photos', $data); } catch(Facebook\Exceptions\FacebookSDKException $e) { $result = $e->getMessage(); } return $result; } }