Undefined variable even though it is set [closed]

1

I have the following code:

<!DOCTYPE HTML>
<html lang="pt-br">
  <head>
  <meta charset="UTF-8">
    <title>Oi</title>
</head>
 <body>
<?php 
session_start();
date_default_timezone_set('America/Sao_Paulo');
require_once __DIR__ . '/vendor/autoload.php';

///////////////////////////////////////////////////////////////////////////////
$fb = new Facebook\Facebook([
  'app_id' => '58xxxx',
  'app_secret' => '04xxxxxxxxxxx879c7dx9cbbc',
  'default_graph_version' => 'v2.2',
  ]);
$token =  $_SESSION['facebook_access_token'];

//vars
$iatual = 0;
$inovo = 9;
$target_file = "uploads/img.jpg";
$texto = "TEXT TEXT TEXT..";
if (!empty($_POST['inputs'])) 
{
   $iatual = $_POST['inputs'];
   $inovo = $iatual + 10;
}
////////////////////////////////////////////////////////////////////////////
$idgrupos;


function LerArquivo($arquivo='arquivo.txt')
{
    $handlee = fopen($arquivo, 'r');
    $conteudo = fread($handlee, filesize($arquivo));
    fclose($handlee);
    $idgrupos = explode(',', $conteudo);
        return $idgrupos;
}   

echo $inovo.'<br>';
echo $iatual.'<br>';
function FazerPostagem($idgrupos)
{

  for ($i=$iatual; $i < count($idgrupos); $i++) { // LINHA 52


    try {


         // Returns a 'Facebook\FacebookResponse' object
          $response = $fb->post('/'.$idgrupos[$i].'/photos', $data, $token); //LINHA 59
        } 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;
        }finally {
          $iatual = $i;
        }

    $graphNode = $response->getGraphNode();
   echo 'Photo ID: ' . $graphNode['id'].'<br/>';

   if ($iatual == $inovo) {
    break 2;
   }

    echo $idgrupos[$i].'<br/>';
    echo $iatual.'<br/>';
  }
}
FazerPostagem(LerArquivo());

?>

<form action="phh.php" method="post">
<input type=text name="inputs"/><input type="submit">
</form> 
</body>
</head>

Errors:

  

Notice: Undefined variable: iatual in C: \ wamp \ www \ facebook-php-sdk-v4-5.0-dev \ phh.php on line 51

     

Notice: Undefined variable: fb in C: \ wamp \ www \ facebook-php-sdk-v4-5.0-dev \ phh.php on line 58

     

Fatal error: Call to a member function post () on a non-object in C: \ wamp \ www \ facebook-php-sdk-v4-5.0-dev \ phh.php on line 58

Where is the error? because from what I know the scopes of my variables are correct

EDIT: I was able to make the code work by making some edits in it (I do not change it here because I edited it in a way that invalidates the question) /pastebin.com/8R2MX2UT "> link Thanks @ juniorb2ss But now I get the error: Graph returned an error: Permissions error As it turns out, when I make 10 posts the Graph API blocks and will not let me do it anymore. Does anyone know how to fix this? I spent a lot of time on that and I do not want to give up now.

    
asked by anonymous 20.09.2015 / 19:56

1 answer

2

The variable is outside the scope of the function, so the error is triggered.

Two easy ways to solve:

1. Invoking global

In this section:

function FazerPostagem($idgrupos)
{

Add a call to global

function FazerPostagem($idgrupos)
{
    global $iatual, $fb;

2. Passing Parameter

function FazerPostagem($idgrupos, $iatual, $fb)
{

At the bottom, where you invoke the function FazerPostagem() , it would look like this:

FazerPostagem(LerArquivo(), $iatual, $fb);
    
21.01.2016 / 20:52