PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 11, 2022

[FIXED] Facebook api , Recursive Function and 500 error

 February 11, 2022     facebook, facebook-graph-api, facebook-php-sdk, php, recursion     No comments   

Issue

this is my first project using facebook api and facebook PHP sdk , basically i'm trying to get all user statuses. I wrote a script that should work , but i got an 500 error (even if i changed max execution times or set time limit (0)), but only when i use a recursive function inside, take a look to the code :

 $request = new FacebookRequest(
 $session,
 'GET',
 '/me/statuses'
 );
$response = $request->execute();
$graphObject = $response->getGraphObject();
$x = $graphObject->getProperty('data');
$y = $x->asArray(); //now i got an array
$paging = $graphObject->getProperty('paging'); // i pick paging with "next" and "prevoiuos "
$paged = $paging->asArray();  //as array
$counter = 0 ;              
foreach ($y as $el){
        echo ('<h3>'.$y[$counter]->message.'</h3>');
        echo "<br/>";
        echo "<br/>";
        $counter++;
}

$next = $paged['next']; // now i got url for load 20 more statuses
$response = file_get_contents($next); // get content of url

//recoursive function every time i use looper with content of next
function looper($response){
    $array = json_decode($response, true);  
    $secondarray = ($array['data']);
    $paging = ($array['paging']); // again i pick url for load  next statuses
    $next =  $paging['next'];// again i pick url for load  next statuses
    $nextResponse = file_get_contents($next);// again i pick url for load  next      statuses and i will use this.
    $counter2 = 0 ;             
        foreach ($secondarray as $el){  //  put on page next 20 statuses

                echo ('<h3>'. $secondarray[$counter2]['message'] .'</h3>');
                echo "<br/>";
                echo "<br/>";
                $counter2++;


        }   
        if ( is_null($nextResponse) == false ){ // if in next call i  got 20 more statuses(not empty) call again this function
            looper($nextResponse);
        } else { echo "last message" ; die();}  //else stop.

}

looper($response);

}

If i dont recall the function (basically i comment out the if statement) script works fine and prints 20+20 statuses , else it give me 500 internal error. As i said i tried changin max execution time or set_time_limit(0), but nothing happens. I'm not sure if problem is my hosting (godaddy) , or if my script is not good / not efficent. any help? Thanks Nico


Solution

I think I found your issue. You are asigning $nextResponse the value returned by file_get_contents. See http://php.net/manual/es/function.file-get-contents.php it returns false in case of content couldnt be retrieved. Try checking for false instead of null:

        ..........

        if ( false != $nextResponse ){ // if in next call i  got 20 more statuses(not empty) call again this function
            looper($nextResponse);
        } else { echo "last message" ; die();}  //else stop.


Answered By - angeldelrio
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing