Jump to content
cobusbo

php iterate function help needed

Recommended Posts

Hi I'm currently connecting to a API to retrieve info. The info I'm retrieving is in the form of Std Class Object.
The problem I'm experiencing is that the results I get from one call is limited to 25. They say I should iterate it. But im not sure how to do so.
 
My code so far
<html> 
<?php 

require_once('MxitAPI.php'); 

try { 
$key = '************************'; 
$secret = '*****************************'; 

    $api = new MxitAPI($key, $secret); 

    if (isset($_GET) && isset($_GET['code'])) { 
$api->get_user_token($_GET['code'],       'http://********************'); 

        // Get Contacts 
        $contacts = $api->get_contact_list('@Friends', 0, 25); 
//print_r($contacts); 



foreach ($contacts->Contacts as $data) { 
    echo $data->DisplayName."<br>"; 
$invite = $data->UserId; 
$invite1 = $invite . ','; 









/* generate the message */  
$id = $_SERVER["HTTP_X_MXIT_USERID_R"]; 

$msg = "Hey! Come join me on {0}! And lets chat like a boss... :-P :-D";  
$link = array( array(  
'CreateTemporaryContact' => true,  
'TargetService' => '********',  
'Text' => '*****' )  
); 

/* send the message */  
$api->send_message($id, $invite1, $msg, 'true', $link); 



}  



    } elseif (isset($_GET) && isset($_GET['error']) && isset($_GET['error_description'])) { 
        echo "<h2>There was an error requesting access from the API</h2>"; 
        echo '<strong>Error:</strong> '. $_GET['error'] .'<br />'; 
        echo '<strong>Error Description:</string> '. $_GET['error_description'] .'<br />'; 
    } else { 
        $api->request_access('***********************', 'graph/read message/user'); 
    } 

} catch (Exception $e) { 
    echo $e->getMessage(); 
} 

?> 
<a href="page.php">Back</a>  
</html>

They say I should turn

$contacts = $api->get_contact_list('@Friends', 0, 25);  

into a loop but not sure how to do so

the 0 is the files to skip and 25 is the amount to retrieve with each call.

they say 
 

 
There is a default page limit of 26, so to access the user's complete contact list you will be iterate this call.

More info can be found here regarding the API im trying to use

 

with print_r option my object looks like

stdClass Object ( [Contacts] => Array ( [0] => stdClass Object ( [DisplayName] => Zyco [FirstName] => zyco [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1426134583000+0200)/ [LastOnline] => /Date(1426139350470+0200)/ [StatusMessage] => ) [UserId] => 0769114834 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [1] => stdClass Object ( [DisplayName] => cobusbo [FirstName] => Cobus [LastName] => Botma [State] => stdClass Object ( [Availability] => 2 [IsOnline] => 1 [LastModified] => /Date(1426162593000+0200)/ [LastOnline] => /Date(1426180367476+0200)/ [Mood] => 0 [StatusMessage] => checkout spamchat ) [UserId] => 27765238453 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [2] => stdClass Object ( [DisplayName] => Bassmaker [FirstName] => Stefan [LastName] => Hattingh [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1425535612000+0200)/ [LastOnline] => /Date(1426232999571+0200)/ [StatusMessage] => Lesson learned!!!! Never fall inlove!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ) [UserId] => 27828456765 [Blocked] => [Group] => MXit [ContactType] => 0 [SubscriptionType] => 66 ) [3] => stdClass Object ( [DisplayName] => SHERIFF [FirstName] => ``ॐ☀☺ॐB.I.G5☺™♦.+LIONKIng [LastName] => ँ๏°˚ _♥_☺™♦☀LORDMINATI☀☺™♦๏° [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1420610808000+0200)/ [LastOnline] => /Date(1426171691976+0200)/ [StatusMessage] => #ff425a #ff425aLaVeyan sHaitAn Bible. ) [UserId] => m11231391002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [4] => stdClass Object ( [DisplayName] => HunterPoint. [FirstName] => Caroline [LastName] => Boot [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1426227463000+0200)/ [LastOnline] => /Date(1426240850456+0200)/ [StatusMessage] => Friend isn't about who you've know the longest it's about who walked into your life said "I'm here for you" and proved it. ) [UserId] => m18323190002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [5] => stdClass Object ( [DisplayName] => $Sugar$ $Max$ [FirstName] => $sugar$ $max$ [LastName] => Sugar$ $max$ [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1425995353000+0200)/ [LastOnline] => /Date(1426237305719+0200)/ [StatusMessage] => ) [UserId] => m19055694002 [Blocked] => [Group] => MXit [ContactType] => 0 [SubscriptionType] => 66 ) [6] => stdClass Object ( [DisplayName] => .-.-.-KID☀BIG [FirstName] => *#c0c00c.+.+.+KID #c0c0c0BIG* [LastName] => *#c0c0c0.+.+.+BUMS#00ff00HAKA* [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1424843034000+0200)/ [LastOnline] => /Date(1425665935177+0200)/ [StatusMessage] => ) [UserId] => m24542119002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [7] => stdClass Object ( [DisplayName] => Kλzεhιτsυjι [FirstName] => [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1425991633000+0200)/ [LastOnline] => /Date(1425991773513+0200)/ [StatusMessage] => #9966fb phone broken ) [UserId] => m25912500002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [8] => stdClass Object ( [DisplayName] => (H)FATIMA(H) [FirstName] => /#ff0000Fatima/ [LastName] => /#ff0000Japhta/ [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1425912639000+0200)/ [LastOnline] => /Date(1426239237672+0200)/ [StatusMessage] => Busy..... Flaming spirit-: SITTER ) [UserId] => m35370741002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [9] => stdClass Object ( [DisplayName] => ��m�H� W�YB�Y [FirstName] => Francois Barnard [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1426006669000+0200)/ [LastOnline] => /Date(1426183951042+0200)/ [StatusMessage] => ...... Ķãmãķãżī Wãřłøřđś .... If anyone knows coding pls msg me ... or if u know wapka msg me pls ((( Checkout Extreme_gamers ))) ) [UserId] => m37042581002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [10] => stdClass Object ( [DisplayName] => *#ff0000 #0000ffXena* [FirstName] => Km vnd yt [LastName] => Vra jw nek [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1424576823000+0200)/ [LastOnline] => /Date(1424577160006+0200)/ [StatusMessage] => .+#0000ff ) [UserId] => m40566397002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [11] => stdClass Object ( [DisplayName] => {{*BL!TZKR!3G#]] [FirstName] => [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1425809938000+0200)/ [LastOnline] => /Date(1425818270545+0200)/ [StatusMessage] => Cheers Galaxy Wars. ) [UserId] => m41544489002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [12] => stdClass Object ( [DisplayName] => Lord of chaose [FirstName] => [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1426188520000+0200)/ [LastOnline] => /Date(1426224169262+0200)/ [StatusMessage] => ) [UserId] => m42930422002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [13] => stdClass Object ( [DisplayName] => Morning-staR• [FirstName] => Apocalypse [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1426016522000+0200)/ [LastOnline] => /Date(1426236913282+0200)/ [StatusMessage] => D.N.A femceE's Crew be Making a one hella sick tour around DBN. Lol err don't even ask about the Males Crew but i'm happy for all my gals daddy cant wait for ya'll to come back so we can start eating some MoOLa(((LMAO))) ) [UserId] => m43239048002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [14] => stdClass Object ( [DisplayName] => #Ff0000*.+.+.+©£MP£ROR©#Ff0000*.+.+.+ [FirstName] => #Ff0000*.+.+JUMONG#Ff0000*.+.+ [LastName] => #cc00cc*.+.+DYNA§TY#Ff0000*.+.+ [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1421736155000+0200)/ [LastOnline] => /Date(1426225275110+0200)/ [StatusMessage] => #ff00ff®eally.. Visit my download portal- VENTRICK | Home Of Downloads Remember d sabbath dat 2 kip it holy. I can do all tins 2ru christ which streghteneth me! ) [UserId] => m46623264004 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [15] => stdClass Object ( [DisplayName] => kira [FirstName] => Kira [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1426111312000+0200)/ [LastOnline] => /Date(1426231189633+0200)/ [StatusMessage] => Havent! ) [UserId] => m48340699002 [Blocked] => [Group] => MXit [ContactType] => 0 [SubscriptionType] => 66 ) [16] => stdClass Object ( [DisplayName] => DEATHANGEL [FirstName] => DEATH [LastName] => ANGEL [State] => stdClass Object ( [Availability] => 3 [IsOnline] => 1 [LastModified] => /Date(1426239062000+0200)/ [LastOnline] => /Date(1426239536392+0200)/ [Mood] => 1 [StatusMessage] => ) [UserId] => m49191486002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [17] => stdClass Object ( [DisplayName] => Shadow Reaper [FirstName] => dont [LastName] => know [State] => stdClass Object ( [Availability] => 1 [IsOnline] => 1 [LastModified] => /Date(1426189573000+0200)/ [LastOnline] => /Date(1426237783400+0200)/ [Mood] => 6 [StatusMessage] => Leave me alone not in the mood...tik verby ) [UserId] => m51256041002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [18] => stdClass Object ( [DisplayName] => 《■□●◆8-)¿¥¤UNG §K@T£R¿8-)●■●♧》 [FirstName] => #fe333d??? [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1425840233000+0200)/ [LastOnline] => /Date(1425126238187+0200)/ [StatusMessage] => #669966 ) [UserId] => m52626156002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [19] => stdClass Object ( [DisplayName] => .+_*#ffff00ICON*_ [FirstName] => .+*MCKINGO #ffff00 [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1424897461000+0200)/ [LastOnline] => /Date(1425331624085+0200)/ [StatusMessage] => THE ICON NOT AND NEVER WILL BE XCON.AM THE APEX OF APEX PREDATORS ) [UserId] => m56408943004 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [20] => stdClass Object ( [DisplayName] => *N-R* [FirstName] => [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1424209196000+0200)/ [LastOnline] => /Date(1426181775833+0200)/ [StatusMessage] => ) [UserId] => m58722247002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [21] => stdClass Object ( [DisplayName] => ZIYAD [FirstName] => [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1426097756000+0200)/ [LastOnline] => /Date(1426182338901+0200)/ [StatusMessage] => I'm not here right now ) [UserId] => m60626483002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [22] => stdClass Object ( [DisplayName] => king•othello [FirstName] => [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1426175909000+0200)/ [LastOnline] => /Date(1426176930152+0200)/ [StatusMessage] => ) [UserId] => m61831903002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [23] => stdClass Object ( [DisplayName] => #c0c00c.+.+.+*Mxit hacker* [FirstName] => $Mxit hacker$ [LastName] => $Mxit hacker$ [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1423285744000+0200)/ [LastOnline] => /Date(1423285748616+0200)/ [StatusMessage] => 2015 comes with new cheat tricks tht i created [Image] [Image] ) [UserId] => m62798876002 [Blocked] => [Group] => MXit [ContactType] => 0 [SubscriptionType] => 66 ) [24] => stdClass Object ( [DisplayName] => james basson [FirstName] => [LastName] => [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1425222833000+0200)/ [LastOnline] => /Date(1425224038803+0200)/ [StatusMessage] => ¿Grim Give¿ ) [UserId] => m63089464002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) [25] => stdClass Object ( [DisplayName] => Kizer Dengram [FirstName] => Clinton [LastName] => Ifeanyichukwu [State] => stdClass Object ( [Availability] => 4 [IsOnline] => [LastModified] => /Date(1424621304000+0200)/ [LastOnline] => /Date(1426236525690+0200)/ [StatusMessage] => Ooh Lord My havenly father, as i arrange my wonderful bed (Future) so i will lie on it... But lord as i am part of your family, please lord come and help me to arrange my Bed (My Future) coz only me cant arrange a whole wide Bed, for fear of Wolves! Amen! ) [UserId] => m63748006002 [Blocked] => [Group] => Galaxy Universe Admin [ContactType] => 0 [SubscriptionType] => 66 ) ) )
this is only the first 26 people info on my contact list I need to get a way to iterate it when the call is done with the first 26 to move on to the next 26. and then the next 26 etc.
 
I looked at $contacts = $api->get_contact_list('@Friends', 0, 25) and saw the 0 for skipped items and 25 for page limit. is there maybe a way to create a loop to say with the first call skip 0 with the second recall skip the first 25 and with 3rd recall skip the first 50 till its done...

 

Share this post


Link to post
Share on other sites

pseudocode

start = 0
loop
    call get_contact_list() using start
    start += 25
    if no contacts returned
        exit loop
    end if
    process contacts
end loop
Edited by Barand

Share this post


Link to post
Share on other sites

 

pseudocode

start = 0
loop
    call get_contact_list() using start
    start += 25
    if no contacts returned
        exit loop
    end if
    process contacts
end loop

HI Barand, I'm not sure this will work because the get contact list function look as follow

   /**
     *   Get the social graph information for a MXit user with the given UserId contained in the
     *   access token.
     * 
     *   Filters: @All, @Friends, @Apps, @Invites, @Connections (Default), @Rejected, @Pending, @Deleted, @Blocked
     *
     *   Url: http://api.mxit.com/user/socialgraph/contactlist?filter={FILTER}&skip={SKIP}&count={COUNT}
     *
     *   User Token Required
     *
     *   Required scope: graph/read
     *
     *   NOTE: You might expect count to return 2 items if you specify a value of 2, but it will return 3 items
     *         So you should treat the value similar to an array value, ie specify 1 if you want 2 results
     */
    public function get_contact_list($filter='', $skip=0, $count=0) {
        $this->_check_scope('get_contact_list', 'graph/read');
        $url = $this->_base_user_api_url ."socialgraph/contactlist";
        if ($filter != '')
            $url .= '?filter='. $filter;
        if ($skip != 0) {
            $url .= ($filter == '') ? '?' : '&';
            $url .= 'skip='. $skip;
        }
        if ($count != 0) {
            $url .= ($filter == '' && $skip == 0) ? '?' : '&';
            $url .= 'count='. $count;
        }
        $this->_api_headers();
        $this->_call_api($url, 'GET');
        return $this->result;

So as you can see in this case the 0 is the skipped contacts and count will be the maximum of 26 with each call I make

Share this post


Link to post
Share on other sites

Needs a rewrite of the pseudocode in that case

skip = 0
loop
    call get_contact_list() using skip
    skip += 25
    if no contacts returned
        exit loop
    end if
process contacts
end loop

Is that better?

Share this post


Link to post
Share on other sites

Needs a rewrite of the pseudocode in that case

skip = 0
loop
    call get_contact_list() using skip
    skip += 25
    if no contacts returned
        exit loop
    end if
process contacts
end loop

Is that better?

Ok so If I want to implement this into my code in the first post  how would it look like? since im currently retrieving the contacts like this

foreach ($contacts->Contacts as $data) { 
    echo $data->DisplayName."<br>"; 
$invite = $data->UserId; 
$invite1 = $invite . ','; 

I'm not sure how to implement it? And so I do not have to specify anything in the limit(count) above and what about the filter?

Edited by cobusbo

Share this post


Link to post
Share on other sites

Ok so I tried this

$startat = 0; 
$getHowMany = 25; 
while($contacts = $api->get_contact_list('@Friends', $startat, $getHowMany) )  
{  
    $startat += $getHowMany; // advance starting point! 
    foreach ($contacts->Contacts as $data)  
    {  
         ... do whatever you need to do ... 
    } 
}  

It seems to work but the only problem I got is that the function is slow. The app im using is giving me 10 seconds to get a response from my server and it seems like the loop takes longer. Is there a way to cleanup my code to make it more stable to save processioning time?

<html>
<?php

require_once('MxitAPI.php');

try {
$key = '11111111111111111111111111111111111';
$secret = '1111111111111111111111111111111111111';

    $api = new MxitAPI($key, $secret);

    if (isset($_GET) && isset($_GET['code'])) {
$api->get_user_token($_GET['code'],       'http://*******************/chat2/share.php');

        // Get Contacts
$startat = 0; 
$getHowMany = 25; 


        while($contacts = $api->get_contact_list('@Friends', $startat, $getHowMany) ) {
//print_r($contacts);
$startat += $getHowMany; // advance starting point! 


foreach ($contacts->Contacts as $data) {
    //echo $data->DisplayName."<br>";
$invite = $data->UserId;
$invite1 = $invite . ',';

echo "It worked";







/* generate the message */ 
$id = $_SERVER["HTTP_X_MXIT_USERID_R"];

$msg = "Hey! Come join me on {0}! And lets chat like a boss... :-P :-D"; 
$link = array( array( 
'CreateTemporaryContact' => true, 
'TargetService' => 'spamchat', 
'Text' => 'Spam Chat' ) 
);

/* send the message */ 
$api->send_message($id, $invite1, $msg, 'true', $link); 


} 
}
echo "It worked";

    } elseif (isset($_GET) && isset($_GET['error']) && isset($_GET['error_description'])) {
        echo "<h2>There was an error requesting access from the API</h2>";
        echo '<strong>Error:</strong> '. $_GET['error'] .'<br />';
        echo '<strong>Error Description:</string> '. $_GET['error_description'] .'<br />';
    } else {
        $api->request_access('************************', 'graph/read message/user');
    }

} catch (Exception $e) {
    echo $e->getMessage();
}

?>
<a href="page.php">Back</a> 
</html>

Share this post


Link to post
Share on other sites

It will be slow, especially if there are a lot of returns.  Each call to the api function creates overhead.  If you call it once, twice, thrice, there may never be a problem.  You make that call ten times, you will notice it. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.