Undefined variable although it is defined

Asked

Viewed 187 times

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>

Mistakes:

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 as far as I know the scopes of my variables are correct

EDIT: I managed to make the code work by making some edits on it (I do not change here because I edited in a way that invalidates the question) for those who want to see I did so: http://pastebin.com/8R2MX2UT Thank you @juniorb2ss But now I get error: Graph returned an error: Permissions error Apparently, when I do 10 posts the Graph API blocks and doesn’t let me do it anymore. Does anyone know how to fix it? I spent all my time on it is I don’t want to give up now rsrs

  • 4

    The variable is defined outside the scope of the function. It must be defined within the scope of the function. Functions do not have access to external variable unless it is passed as parameter or defined globally.

  • Put errors as text.

  • I made some changes and I get this mistake. Someone knows how to fix it?

  • If you have a new question, please use the button Ask a question. The area you used to post the question is for answers only.

1 answer

2


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

Two easy ways to solve:

1. Invoking a global

In this passage:

function FazerPostagem($idgrupos)
{

Add a call to global

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

2. Passing by parameter

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

There at the bottom, where it invokes the function FazerPostagem(), would look like this:

FazerPostagem(LerArquivo(), $iatual, $fb);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.