How can I find where I will be redirected using cURL?

前端 未结 7 1838
小鲜肉
小鲜肉 2020-11-22 03:53

I\'m trying to make curl follow a redirect but I can\'t quite get it to work right. I have a string that I want to send as a GET param to a server and get the resulting URL.

7条回答
  •  情深已故
    2020-11-22 04:20

    Add this line to curl inizialization

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    

    and use getinfo before curl_close

    $redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );
    

    es:

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    $html = curl_exec($ch);
    $redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );
    curl_close($ch);
    

提交回复
热议问题