PHP, Curl - stuck in redirect loop

Discussion in 'PHP' started by ollyno1uk, Feb 5, 2009.

  1. #1
    I am having a really strange issue that has been driving me mad for days.

    I am trying to follow a redirected affiliate link to using curl to then locate a product model number and return it to the database.

    This script works fine for normal links but not for the affiliate link. I assumed because of the redirect.

    I am now in a position where I seem to be stuck in a permanent loop when I am accessing the script and the page is never returned.

    here is an example link:

    http://pdt.tradedoubler.com/click?a(1394350)p(46927)prod(59715571)ttid(5)

    The really weird thing is that when I tried it on my office PC I get the loop, on my home PC it does actually function. The script is on my server so what possible effect could the PC have on redirects?

    It is baffling me and any help would be appreciated. here is some example code.

    
    
       1. $sql = new mysqli('localhost', 'username', 'password', 'database');
       2. $qry = 'SELECT * FROM table where title is null';
       3. $res = $sql->query($qry);
       4. $script_start = time();
       5. $ch = curl_init();
       6.  
       7. while($row = $res->fetch_object()) {
       8.    
       9.     //curl_redir_exec($ch);
      10.     curl_setopt($ch, CURLOPT_HEADER, true);
      11.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
      12.     curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
      13.  
      14.    
      15.     curl_setopt($ch,CURLOPT_URL, $row->url);
      16.    
      17.     $content = curl_exec($ch);
      18.    
      19. }
      20. echo $content;
      21. curl_close($ch);
    
    Code (markup):
     
    ollyno1uk, Feb 5, 2009 IP
  2. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #2
    how does that work ?
    fetch_object()
    Code (markup):
     
    hip_hop_x, Feb 5, 2009 IP
  3. ollyno1uk

    ollyno1uk Active Member

    Messages:
    946
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #3
    it just runs through the rows in the table as far as I know, checking for each entry
     
    ollyno1uk, Feb 6, 2009 IP
  4. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can stop infinite loops by using CURLOPT_MAXREDIRS

    You could also add in a curl_error check and then break the loop if it exists.

    However, the way you have the loop, it is overwriting every curl data set in the while loop and then outputting the last 1 to the browser.
     
    lfhost, Feb 7, 2009 IP