how can i make foreach() fetch the item that has been loaded, one by one?

Discussion in 'PHP' started by free-designer, Jul 9, 2010.

  1. #1
    hey guys,

    Im using a regexp to get all images from an url, here is the code

    
    $url = "http://www.smashingmagazine.com/2010/06/30/desktop-wallpaper-calendar-july-2010/";
    
    preg_match_all('/(<img (.*))\s (src="((.*))")/isxmU', file_get_contents($url), $images);
    		  
    foreach($images[4] as $image_url):
        $valid_files = substr($image_url, -3);
        if(!parse_url($image_url, PHP_URL_HOST)){
    	 $image_url = preg_replace("/$\//", "", $image_url);
    	 $image_url = "http://".parse_url($url, PHP_URL_HOST)."/".$image_url;
        }
        echo $image_url."<br />";
    endforeach;
    
    PHP:
    My problem is that this code will have to wait for all of the images to be loaded and then fetch all of them at once, i don't want it like that,

    What i want it is:

    let's say i have 10 images, and the first one has loaded so fetch it, then the second had loaded so fetch it, ONE BY ONE, Not all of them at once

    so please any suggestions, I'll be thankful
     
    free-designer, Jul 9, 2010 IP
  2. XDMCoder

    XDMCoder Peon

    Messages:
    50
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $url = 'http://www.smashingmagazine.com/2010/06/30/desktop-wallpaper-calendar-july-2010/';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($curl);
    preg_match_all('/<img(.*?)src="(?P<url>.*?)"/ims', $content, $matches);
    foreach($matches['url'] as $key => $url) {
       echo $url . '<br />';
    }
    Code (php):
     
    XDMCoder, Jul 10, 2010 IP
  3. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think this is exactly what i need thanks.
    but i'll ask you another question if i want to echo the loaded item just echo it once it get loaded, can that be done with php or i have to use jquery or something like that??

    thanks so mush
     
    free-designer, Jul 10, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Try:

    ob_implicit_flush(true);
    $url = 'http://www.smashingmagazine.com/2010/06/30/desktop-wallpaper-calendar-july-2010/';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($curl);
    preg_match_all('/<img(.*?)src="(?P<url>.*?)"/ims', $content, $matches);
    foreach($matches['url'] as $key => $url) {
       echo $url . '<br />';
      ob_flush();
      flush();
    }
    PHP:
     
    danx10, Jul 10, 2010 IP
  5. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well, that exactly what i need thank you very mush.
    But there is a problem, when i try the code on my localhost it's works very good and the loaded item get fetch, but the problem is when i try to put it into a remote server it dose not load one by one and fetch the loaded items, it just load all of them at once

    u can test this and tell me if it works fine, but please try more then one time..

    examble:
    http://vademo.net/flush-test.php

    notice that it will be a little bit slow cuz im doing a validate for the height and width with getimagesize(); here is the code

    
    ob_implicit_flush(true);
    		$url = 'http://www.smashingmagazine.com/';
    		$curl = curl_init();
    		curl_setopt($curl, CURLOPT_URL, $url);
    		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    		$content = curl_exec($curl);
    		$max_width = 150;
    		$max_height = 150;
    		preg_match_all('/<img(.*?)src="(?P<url>.*?)"/ims', $content, $matches);
    		foreach($matches['url'] as $key => $url) {
    			list($width, $height) = getimagesize($url);
    			if($width >= $max_width && $height >= $max_height){
    		    	echo $url . '<br />';
    		    	ob_flush();
    		    	flush();
    			}
    		}
    
    PHP:
    other question: dose the flush use the memory of the server, and if there is 100 user getting images like this, is that going to affect the server??

    thank u very mush
     
    free-designer, Jul 10, 2010 IP
  6. XDMCoder

    XDMCoder Peon

    Messages:
    50
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Use this script just for parsing info and put parsed links in DB.
    Show your users already parsed data from your DB.
     
    XDMCoder, Jul 10, 2010 IP
  7. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No, This is not what im planning to do. im trying to get images links and then validate the height and width for every image with getimagesize(), but the problem is that getimagesize(), have to download every single image and if there is a 10 images or something and every image is 2mb or something then it will be soooo slow...

    do you know any other way to get the a remote image height and width with out downloading it???

    thanks
     
    free-designer, Jul 10, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    The reason getimagesize() may be slow is because the image may not exist, use the following it will check it exists then proceed...also will display sequently...

    <?php
      ob_start();
      //edit accordingly...
      $max_width = 30;
      $max_height = 40;
      function remote_file_size($url)
      {
      global $max_width, $max_height;
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_NOBODY, true);
          curl_setopt($ch, CURLOPT_FAILONERROR, true);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          
          //check image exists...
          if (curl_exec($ch) !== false) {
              //check image size...
              list($width, $height) = getimagesize($url);
              if ($width >= $max_width && $height >= $max_height) {
                  return true;
              } else
                  return false;
          } else {
              return false;
          }
      }
      $url = 'http://www.smashingmagazine.com/2010/06/30/desktop-wallpaper-calendar-july-2010/';
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      $content = curl_exec($curl);
      preg_match_all('~<img.+?src\s*=\s*["\']+(.+?)["\']+~is', $content, $matches);
      foreach ($matches[1] as $url) {
          if (remote_file_size($url)) {
              echo $url . "\n<br />\n";
              //display sequently/one by one...
              ob_flush();
              flush();
              usleep(9000);
          }
      }
    ?>
    PHP:
     
    Last edited: Jul 10, 2010
    danx10, Jul 10, 2010 IP
  9. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    @danx10:

    i really appreciate what you doing with me thank you very very mush.
    but it works very very good with my localhost but on the remote server it doesn't work, it just load all of them at once Not sequently/one by one.
    i added the last code you just provided.
    Please test it: http://vademo.net/flush-test.php

    i really need to solve this, so any ideas?? :)
     
    free-designer, Jul 10, 2010 IP
  10. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #10
    Works for me? -> http://dan.x10.bz/flush-test.php
     
    danx10, Jul 10, 2010 IP
  11. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    @danx10:

    Well, it's working.
    So why do you think it's not working.

    so now im running out of options.
    but there is two things that i should ask you..
    first: is there anything should be changed with php.ini?
    second: the hosting from bluehost.com hosting do you think the problem from them?

    thank you so mush
     
    free-designer, Jul 10, 2010 IP
  12. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #12
    It may be your host, has disabled output buffering or disabled particular functions which are used within the code;

    Create a new file named phpinfo.php on your hosting (http://vademo.net) and paste the following code:

    <?php
    phpinfo();
    ?>
    PHP:
    And then reply with a link to it.
     
    danx10, Jul 10, 2010 IP
  13. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    @danx:
    thanks again
    i have sent to you the link as a private message. :)
     
    free-designer, Jul 10, 2010 IP
  14. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    @danx:
    Thanks so mush for helping me, and giving me this code.
    i solved the problem and it's working very good, i had to turn on zlib.output_compression.
    Case is closed. :)
     
    free-designer, Jul 11, 2010 IP