Ejemplo n.º 1
0
if (count($partialRequests) > 0) {
    $count = 1;
    $completeRequests = [];
    foreach ($partialRequests as $request) {
        $key = 'request-' . $count;
        if ($request['method'] == 'GET') {
            $completeRequests[$key] = $fb->request($request['method'], $request['entity']);
        } else {
            if ($request['method'] == 'POST') {
                $completeRequests[$key] = $fb->request($request['method'], $request['entity'], ['message' => $request['message']]);
            }
        }
        $count++;
    }
    try {
        $responses = $fb->sendBatchRequest($completeRequests);
    } catch (Facebook\Exceptions\FacebookResponseException $e) {
        echo json_encode(['error' => $e->getMessage()]);
        exit(1);
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
        echo json_encode(['error' => $e->getMessage()]);
        exit(1);
    }
    $result = [];
    foreach ($responses as $key => $response) {
        if ($response->isError()) {
            $e = $response->getThrownException();
            echo json_encode(['error' => $e->getMessage()]);
            exit(1);
        } else {
            $arr = json_decode($response->getBody());
Ejemplo n.º 2
0
 function getPage($user_model)
 {
     if (!$user_model instanceof \xepan\marketing\Model_SocialPosters_Base_SocialUsers) {
         throw new \Exception("must pass instance of social user");
     }
     // get Facebook Config
     $config_model = $user_model->appConfig();
     $config = array('app_id' => $config_model['appId'], 'app_secret' => $config_model['secret'], 'default_graph_version' => $this->default_graph_version);
     $this->fb = $facebook = $fb = new \Facebook\Facebook($config);
     if (!$this->fb) {
         return "Configuration Problem";
     }
     $fb->setDefaultAccessToken($user_model['access_token']);
     // get all  pages
     // https://graph.facebook.com/$user_id_returned/accounts/?access_token=$access_token
     $requests = [$facebook->request('GET', '/' . $user_model['userid_returned'] . '/accounts')];
     try {
         $responses = $fb->sendBatchRequest($requests);
     } catch (Facebook\Exceptions\FacebookResponseException $e) {
         echo 'Graph returned an error: ' . $e->getMessage();
         exit;
     } catch (Facebook\Exceptions\FacebookSDKException $e) {
         echo 'Facebook SDK returned an error: ' . $e->getMessage();
         exit;
     }
     // decode all the data
     $response_data = $responses->getDecodedBody();
     if ($response_data[0]['code'] === 200) {
         return $response_data[0]['body'];
     } else {
         throw new \Exception("some thing wrong return code = " . $response_data[0]['code']);
     }
 }