PHP cURL redirects to localhost

前端 未结 1 490
挽巷
挽巷 2021-01-16 17:05

I\'m trying to login to an external webpage using a php script with cURL. I\'m new to cURL, so I feel like I\'m missing a lot of pieces. I found a few examples and modifie

相关标签:
1条回答
  • 2021-01-16 17:37

    Answering part of your question:

    From http://php.net/manual/en/function.curl-setopt.php :

    CURLOPT_RETURNTRANSFER TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

    In other words - doing exactly what you described. It's returning the response to a string and you echo it to see it. As requested...

    ----- EDIT-----

    As for the second part of your question - when I change the last three lines of the script to

    $output = curl_exec($ch);
    header('Location:'.$website);
    echo $output;
    

    The address of the page as displayed changes to $website - which in my case is the variable I use to store my equivalent of your 'https://www.websiteurl.com/login'

    I am not sure that is what you wanted to do - because I'm not sure I understand what your next steps are. If you were getting redirected by the login site, wouldn't the new address be part of the header that is returned? And wouldn't you need to extract that address in order to perform the next request (wget or whatever) in order to download the file you wanted to get?

    To do so, you need to set CURLOPT_HEADER to TRUE,

    You can get the URL where you ended up from

    $last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 
    

    (see cURL , get redirect url to a variable ).

    The same link also has a useful script for completely parsing the header information (returned when CURLOPT_HEADER==true. It's in the answer by nico limpica.

    Bottom line: CURL gets the information that your browser would have received if you had pointed it to a particular site; that doesn't mean your browser behaves as though you pointed it to that site...

    0 讨论(0)
提交回复
热议问题