2015-06-24 42 views
1

的file_get_contents不會返回的源代碼

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)

我不知道爲什麼它返回假的,因爲當我更改網址,例如http://www.google.com或任何其他網址,它將工作並返回頁面的源代碼。

我想它一定是有問題的網址,但它只是對我來說很奇怪,因爲它的網址是可用的。

+3

http://stackoverflow.com/questions/11680709/file-get-contents-give-me-403-forbidden –

回答

2

你可以只抓取頁面,但你必須設置一個用戶代理。捲曲是要走的路。

file_get_contents()是一個簡單的螺絲刀。非常適合簡單的GET請求,其中頭部,HTTP請求方法,超時,cookiejar,重定向和其他重要的事情無關緊要。

<?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

謝謝你,我會嘗試。 –

3

網站所有者可能會禁止您在不詢問的情況下抓取他們的數據。