cURL拒绝显示302重定向URL

时间:2013-11-18 22:55:56

标签: php redirect curl curl-multi

这是我的代码:

    $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);

这是我得到的(来自read_header()函数):

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

如果我print_r()curl_getinfo()我得到这个:

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
        (
        )

)

但是如果您使用chrome(或其他)转到该网址,您会看到该网址会为您提供302重定向到http://www.alssports.com/product.aspx?pf_id=10212312&avad=18967_b55919f9。我需要它给我alssports.com网址。我几乎整天都在研究这段代码。我做错了什么?

1 个答案:

答案 0 :(得分:1)

将您的关注位置设为false

CURLOPT_FOLLOWLOCATION  => false

当我直接连接到www.avantlink.com并请求页面时,我收到了这些标题......

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只是跟随Location标题而不是仅返回302响应。