Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.