Curl not showing all results

Discussion in 'Programming' started by Mahir1505, Jan 30, 2010.

  1. #1
    Hellow ppl,

    I got a problem with cURL,
    I want to get some info from another site and save it in database.

    Regex is 100% right but it still doesn't shows all the results


    <?php
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://site.com/');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
        curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
        $pagina = curl_exec($ch);
        curl_close($ch);
    
        if(empty($page))
        {
           echo "Nothing!";
        }
        else
        {
           if(preg_match_all("/<td\salign=\"right\"[^>]*><a href=\"([^<]*)\"[^>]*><img src=\"([^<]*)\" [^>]*><\/a><br \/>([^<]*)<\/td>/is", $page, $matches))
           {
              echo '<pre>';
              var_dump($matches);
              echo '</pre>';
    $i = $i++;
    foreach($matches as $i => $match)
    {
    $match = $match;
    
        echo '<br>' . $matches[1][$i];
    
    
    
    }
           }
           else
           {
              echo "error!";
           }
        }
        ?>
    PHP:
    the var_dump shows like 30-40 results

    but foreach() only 4-5

    what am i doing wrong?


    Please help someone :)
     
    Mahir1505, Jan 30, 2010 IP
  2. aljosabre

    aljosabre Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why is there $match = $match ? And haven't you tried going through one-dimensional array ?
    Perhaps doing this:
    foreach($matches as $blah)
    {
    echo $blah;
    }
    I'm not an expert with php but I would def. try something like that ..
     
    aljosabre, Jan 30, 2010 IP
  3. Mahir1505

    Mahir1505 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for reply...

    Ya you right $match = $match is totally useless
    But what you say will not work $blah[1]; will give only the first result...
    What you mean with one-dimensional array? can you explain in maybe please?
     
    Mahir1505, Jan 30, 2010 IP
  4. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #4
    Give this a go:

    
    
    <?php
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://site.com/');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
        curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
        $pagina = curl_exec($ch);
        curl_close($ch);
    
        if(empty($page))
        {
           echo "Nothing!";
        }
        else
        {
           if(preg_match_all("/<td\salign=\"right\"[^>]*><a href=\"([^<]*)\"[^>]*><img src=\"([^<]*)\" [^>]*><\/a><br \/>([^<]*)<\/td>/is", $page, $matches))
           {
              echo '<pre>';
              var_dump($matches);
              echo '</pre>';
    		  $i++;
    			
    			foreach($matches[1] as $match)
    			{			
    				echo '<br>' . $match;
    			}
           }
           else
           {
              echo "error!";
           }
        }
    ?>
    
    PHP:
     
    Silver89, Jan 30, 2010 IP
  5. Mahir1505

    Mahir1505 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $matches[1] will show only the first "([^<]*)"
    $matches[0] will show everything right...
    but i can't add it like that to the db
     
    Mahir1505, Jan 30, 2010 IP
  6. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #6
    Have you tried it?
     
    Silver89, Jan 30, 2010 IP
  7. Mahir1505

    Mahir1505 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    yes i tried it shows only the link (in my case)

    foreach($matches[1] as $url)
    foreach($matches[2] as $image)

    this will give the url and img...
    but it duplicates it to many times...
    i rly don't know what i'm doing wrong...
     
    Mahir1505, Jan 30, 2010 IP