Jump to content

Curl returns 301 code when trying to retrieve page


stevesimo

Recommended Posts

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);

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.