Asked  7 Months ago    Answers:  5   Viewed   46 times

Using the following code to display a list of friends from my twitter profile. Id like to only load a certain number at a time, say 20, then provide pagination links at the bottom for First 1-2-3-4-5(however many divided by limit) Last

$xml = simplexml_load_string($rawxml);

foreach ($xml->id as $key => $value) 
{
    $profile           = simplexml_load_file("https://twitter.com/users/$value");
    $friendscreenname  = $profile->{"screen_name"};
    $profile_image_url = $profile->{"profile_image_url"};

    echo "<a href=$profile_image_url>$friendscreenname</a><br>";
}

******update******

if (!isset($_GET['i'])) {
    $i = 0;
} else {
    $i = (int) $_GET['i'];
}

$limit  = $i + 10;
$rawxml = OauthGetFriends($consumerkey, $consumersecret, $credarray[0], $credarray[1]);
$xml    = simplexml_load_string($rawxml);

foreach ($xml->id as $key => $value)
{

    if ($i >= $limit) {
        break;
    }

    $i++;
    $profile           = simplexml_load_file("https://twitter.com/users/$value");
    $friendscreenname  = $profile->{"screen_name"};
    $profile_image_url = $profile->{"profile_image_url"};

    echo "<a href=$profile_image_url>$friendscreenname</a><br>";
}

echo "<a href=step3.php?i=$i>Next 10</a><br>";

This works, just have to offset the output starting at $i. Thinking array_slice?

 Answers

89

A very elegant solution is using a LimitIterator:

$xml = simplexml_load_string($rawxml);
// can be combined into one line
$ids = $xml->xpath('id'); // we have an array here
$idIterator = new ArrayIterator($ids);
$limitIterator = new LimitIterator($idIterator, $offset, $count);
foreach($limitIterator as $value) {
    // ...
}

// or more concise
$xml = simplexml_load_string($rawxml);
$ids = new LimitIterator(new ArrayIterator($xml->xpath('id')), $offset, $count);
foreach($ids as $value) {
    // ...
}
Wednesday, March 31, 2021
 
Yarin
answered 7 Months ago
76

You can download the data, using something like file_get_contents ; it'll get you the whole XML in a single PHP string.
For instance :

$xml = file_get_contents('http://twitter.com/statuses/user_timeline/31139114.rss');

$xml now contains the XML string.


Then, you can write that string to a file, using file_put_contents.
For instance :

file_put_contents('/home/squale/developpement/tests/temp/test.xml', $xml);

And, to check the file, from the command-line :

$ cat test.xml                                                                                                                                                                                                   
<?xml version="1.0" encoding="UTF-8"?>                                                                                                                                                                           
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">                                                                                                                                                     
  <channel>                                                                                                                                                                                                      
    <title>Twitter / eBayDailyDeals</title>                                                                                                                                                                      
    <link>http://twitter.com/eBayDailyDeals</link>                                                                                                                                                               
    <atom:link type="application/rss+xml" href="http://twitter.com/statuses/user_timeline/31139114.rss" rel="self"/>                                                                                             
    <description>Twitter updates from eBay Daily Deals / eBayDailyDeals.</description>                                                                                                                           
    <language>en-us</language>                                                                                                                                                                                   
    <ttl>40</ttl>                                                                                                                                                                                                
    <item> 
...
...


After that, you can use simplexml_load_file to read from that file.
For instance :

$data = file_get_contents('/home/squale/developpement/tests/temp/test.xml');

And $data now contains your XML string ;-)


Considering the XML you get from the remote server is a string, no need to serialize it ; and file_put_contents is easier that fopen+fwrite+fclose ;-)

Wednesday, March 31, 2021
 
altermativ
answered 7 Months ago
24

simplexml_load_string() (as the name suggest) load xml from a string and returns an object of SimepleXMLElement. There is no difference between this and using just the usual constructor of the class.

Update:

SimpleXML::__construct() has an additional parameter (the third one) bool $data_is_url = false. If this is true the behavior is the same as simplexml_load_file() (in conjunction with the common stream wrappers). Thus both simplexml_load_*()-functions cover the same functionality as SimpleXML::__construct().

Additional the functions simplexml_load_*() has a second parameter string $class_name = "SimpleXMLElement" to specify the class of the object to get returned. Thats not a specific feature of the functions, because you can "use" something very similar with usual instanciation too

class MyXML extends SimpleXMLElement {}
$a = new MyXML($xml);
$b = simplexml_load_string($xml, 'MyXML');

A difference between the OOP- and the procedural approach is, that the functions return false on error, but the constructor throws an Exception.

Saturday, May 29, 2021
 
rlanvin
answered 5 Months ago
65

You can use continue to skip the current iteration of a loop.

$exclude = array(3, 4, 8, 19);

for ($i=1; $i<=27; $i++)
{
    if (in_array($i, $exclude)) continue;
    echo "<option value=$i>$i</option>";
}

Documentation.

Saturday, May 29, 2021
 
turson
answered 5 Months ago
99
s = ""
for i in range(3):
    s += 'Hi'
print(s)
Sunday, August 15, 2021
 
CBroe
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :