file_get_contents won't return the source code

257 views Asked by At

When I execute this code:

var_dump(file_get_contents('http://www.zahnarzt-gisler.ch'));

I get this error:

Warning: file_get_contents(http://www.zahnarzt-gisler.ch): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /home/httpd/vhosts/your-click.ch/httpdocs/wp-content/themes/your-click/ajax-request.php on line 146 bool(false)

I don't know why it is returning false, since when I change the url, e.g. http://www.google.com or any other url, it will work and returns the source code of the page.

I guess it must be something wrong with the url but it just seems weird to me, because it url is online and available.

2

There are 2 answers

0
PHPeter On BEST ANSWER

You can just scrape the page, but you have to set a user-agent. Curl is the way to go.

file_get_contents() is a simple screwdriver. Great for simple GET requests where the header, HTTP request method, timeout, cookiejar, redirects, and other important things do not matter.

<?php

$config['useragent'] = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';

$ch = curl_init();

// Set the url, number of GET vars, GET data
curl_setopt($ch, CURLOPT_URL, 'http://www.zahnarzt-gisler.ch');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);

// Execute request
$result = curl_exec($ch);

curl_close($ch);

echo $result;

?>
0
Siim Kallari On

Site owners can disallow you scraping their data without asking.