stevesimo Posted August 20, 2008 Share Posted August 20, 2008 Hi, I am trying to use Curl to download the html content of a webpage of a specified URL and save to server. However when I check the source code of the saved page it is simply a page containing a redirect link to the same URL. The return code is 301 which is a redirect - moved permenantly code. Does anyone know how I can get around this problem? Here is my code: //make sure slash is included initially $url = 'myurl.com/'; // create a new curl resource $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // grab URL, and return output $output = curl_exec($ch); // Get response code $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); echo 'Response Code: '.$response_code; // Not found? if ($response_code == '404') { echo 'Page doesn\'t exist'; } // close curl resource, and free up system resources curl_close($ch); //save webpage to server $webpage = fopen('exchangerates.htm',"w"); fwrite($webpage,$output); fclose($webpage); Quote Link to comment https://forums.phpfreaks.com/topic/120499-curl-returns-301-code-when-trying-to-retrieve-page/ Share on other sites More sharing options...
JonnoTheDev Posted August 20, 2008 Share Posted August 20, 2008 These lines should follow the 301: curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); Is this webpage using a proper 301 redirect or just displaying a link to click? Quote Link to comment https://forums.phpfreaks.com/topic/120499-curl-returns-301-code-when-trying-to-retrieve-page/#findComment-621033 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.