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

前端 未结 7 1852
小鲜肉
小鲜肉 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:39

    Lot's of regex here, despite the fact i really like them this way might be more stable to me:

    $resultCurl=curl_exec($curl); //get curl result
    //Optional line if you want to store the http status code
    $headerHttpCode=curl_getinfo($curl,CURLINFO_HTTP_CODE);
    
    //let's use dom and xpath
    $dom = new \DOMDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTML($resultCurl, LIBXML_HTML_NODEFDTD);
    libxml_use_internal_errors(false);
    $xpath = new \DOMXPath($dom);
    $head=$xpath->query("/html/body/p/a/@href");
    
    $newUrl=$head[0]->nodeValue;
    

    The location part is a link in the HTML sent by apache. So Xpath is perfect to recover it.

提交回复
热议问题