PHP - 连续三个CURL请求:为什么我的代码在收到响应后没有遵循重定向?

时间:2017-12-12 10:43:21

标签: php curl https get

我尝试向外部服务器发送一系列三个HTTP请求:前两个将产品添加到购物车,第三个(和最后一个)将添加最后一个产品,然后按照重定向到购物车,我将看到我添加的所有三种产品。 这是我的代码:

for($i=0; $i<3; $i++) {
   $ch = curl_init();
   $params = array('productCode'=> $products[$i]);
   $query = http_build_query($params);

   try{
      curl_setopt($ch, CURLOPT_URL, 'https://www.website.com/shoppingCart/addProduct?'.$query);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HEADER, 1);
      curl_setopt($ch, CURLINFO_HEADER_OUT, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $reqheaders);
      if($i!=2) {
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
      } else {
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
      }
      $resp = curl_exec($ch);
      curl_close($ch);

  } catch (Exception $e) {
      echo $e;
  }
}

我打印作为调试从服务器获得的响应,在最后一个我有一个位置,这是我需要重定向到的位置,但它没有发生任何事情,我没有在任何地方重定向。

即使我从头开始尝试将CURLOPT_FOLLOWLOCATION设置为true的单个get请求,也会发生同样的事情(无法正常工作)

整个代码是错误的还是我错过了某些东西才能进行重定向?

*****更新***** 这是回复

Response: 
Array ( [status] => HTTP/1.1 302 Found )
Array
(
[0] => HTTP/1.1 302 Found
[1] => Date: Tue, 12 Dec 2017 12:00:46 GMT
[2] => Cache-Control: no-cache, no-store, max-age=0, must-revalidate
[3] => Pragma: no-cache
[4] => Expires: 0
[5] => Strict-Transport-Security: max-age=31536000 ; includeSubDomains
[6] => X-XSS-Protection: 1; mode=block
[7] => X-Frame-Options: DENY
[8] => X-Content-Type-Options: nosniff
[9] => Set-Cookie: JSESSIONID=2607F8F0154DDE785B76124AE3C754E2; Path=/; Secure; HttpOnly
[10] => Location: https://www.website.com/
[11] => Content-Language: it
[12] => Content-Length: 0
[13] => Keep-Alive: timeout=15, max=100
[14] => Connection: Keep-Alive
[15] => Content-Type: text/plain
[16] => 
[17] => 
)

0 个答案:

没有答案