Curl, follow location but only get header of the new location?

前端 未结 7 1650
情书的邮戳
情书的邮戳 2020-12-19 13:56

I know that when I set CURLOPT_FOLLOWLOCATION to true, cURL will follow the Location header and redirect to new page. But is it possible only to get header of the new page w

7条回答
  •  再見小時候
    2020-12-19 14:58

    You can get the redirect URL directly with curl_getinfo:

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_COOKIESESSION, false);
      curl_setopt($ch, CURLOPT_URL, $url); //specify your URL
      curl_setopt($ch, CURLOPT_HEADER, true); //include headers in http data
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); //don't follow redirects
      $http_data = curl_exec($ch); //hit the $url
      $redirect = curl_getinfo($ch)['redirect_url'];
      curl_close($ch);
    
      return $redirect;
    

提交回复
热议问题