1

Here's my code:

    $urls = array('http://www.avantlink.com/click.php?p=62629&pw=18967&pt=3&pri=152223&tt=df');
  $curl_multi = curl_multi_init();
  $handles = array();

  $options = $curl_options + array(
        CURLOPT_HEADER                  => true,
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_NOBODY                  => true,
        CURLOPT_FOLLOWLOCATION  => true,
        CURLOPT_HEADERFUNCTION  => 'read_header',
        CURLOPT_USERAGENT               => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36',
        CURLOPT_HTTPHEADER          => array(
            'Accept-Language: en-US,en;q=0.8',
            'Accept-Encoding: gzip,deflate,sdch',
            'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36',
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Connection: keep-alive',
            'Host: www.avantlink.com',
        ));

  foreach($urls as $i => $url) {
    $handles[$i] = curl_init($url);
    curl_setopt_array($handles[$i], $options);
    curl_multi_add_handle($curl_multi, $handles[$i]);
  }

  $active = null;
    do {
        $status = curl_multi_exec($curl_multi, $active);
        sleep(1);
        error_log('loading redirect: '.$active.' left');
    }
    while (!empty($active) && $status == CURLM_OK);

  do {
    $status = curl_multi_exec($curl_multi, $active);
  } while ($status == CURLM_CALL_MULTI_PERFORM);

  while ($active && ($status == CURLM_OK)) {
    if (curl_multi_select($curl_multi) != -1) {
      do {
        $status = curl_multi_exec($curl_multi, $active);
      } while ($status == CURLM_CALL_MULTI_PERFORM);
    }
  }

  if ($status != CURLM_OK) {
    trigger_error("Curl multi read error $status\n", E_USER_WARNING);
  }

  $results = array();
  foreach($handles as $i => $handle) {
    $results[$i] = curl_getinfo($handle);
    curl_multi_remove_handle($curl_multi, $handle);
    curl_close($handle);    
  }
  curl_multi_close($curl_multi);

Here's what it gets me (from the read_header() function):

HTTP/1.1 200 OK
Date: Mon, 18 Nov 2013 22:42:29 GMT
Server: Apache
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 20
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

If I print_r() the curl_getinfo() I get this:

Array
(
    [url] => http://www.avantlink.com/click.php?p=62629&pw=18967&pt=3&pri=152223&tt=df
    [content_type] => text/html; charset=utf-8
    [http_code] => 200
    [header_size] => 247
    [request_size] => 402
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 2.391713
    [namelookup_time] => 0.388584
    [connect_time] => 1.389628
    [pretransfer_time] => 1.389645
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 20
    [upload_content_length] => 0
    [starttransfer_time] => 2.391203
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

)

But if you go to that URL in chrome (or whatever) you'll see that the URL gives you a 302 redirect to http://www.alssports.com/product.aspx?pf_id=10212312&avad=18967_b55919f9. I need it to give me the alssports.com url. I've been working on this code for almost the entire day. What am I doing wrong?

user2188915
  • 37
  • 1
  • 5

1 Answers1

1

Set your follow location to false

CURLOPT_FOLLOWLOCATION  => false

When I connected diretly to www.avantlink.com and requested the page, I received these headers...

HTTP/1.1 302 Found
Date: Mon, 18 Nov 2013 23:04:12 GMT
Server: Apache
Set-Cookie: merchant_id_10240=18967_a55923c7-_-10240-df-62629-18967-152223-84%7E; expires=Thu, 17-Apr-2014 23:04:12 GMT; path=/; domain=.avantlink.com
P3P: CP="NOI DSP LAW NID LEG"
Location: http://www.alssports.com/product.aspx?pf_id=10212312&avad=18967_a55923c7
Vary: Accept-Encoding,User-Agent
Content-Length: 0
Content-Type: text/html; charset=utf-8

Curl is simply following the Location header instead of just returning the 302 response.

Andy Jones
  • 6,205
  • 4
  • 31
  • 47