簡體   English   中英

無法通過使用PHP中的file_get_content獲取網站內容

[英]unable to get the website content by using file_get_content in php

當我嘗試通過使用php中的file_get_contents從外部URL fanpop.com獲取網站內容時,我正在獲取空數據。 我用下面的代碼來獲取內容

$add_url= "http://www.fanpop.com/";  
$add_domain = file_get_contents($add_url);  
echo $add_domain;  

但是在這里,我得到了$ add_domain的空結果。 但是相同的代碼也適用於其他網址,我嘗試從瀏覽器發送請求而不是從腳本發送請求,因此它也不起作用。

以下是相同的請求,但使用CURL:

error_reporting(-1);
ini_set('display_errors','On');
$url="http://www.fanpop.com/";
$ch = curl_init();

$header=array('GET /1575051 HTTP/1.1',
'Host: adfoc.us',
'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language:en-US,en;q=0.8',
'Cache-Control:max-age=0',
'Connection:keep-alive',
'Host:adfoc.us',
'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4)            AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
);

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);
curl_setopt( $ch, CURLOPT_COOKIESESSION, true );

curl_setopt($ch,CURLOPT_COOKIEFILE,'cookies.txt');
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookies.txt');
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);

echo $ result = curl_exec($ ch);

curl_close($ch);

...但上述方法也不起作用,請問有人可以對它進行任何更改嗎?

該特定站點的問題在於,它僅提供壓縮內容,否則將引發404錯誤。

輕松解決:

$ch = curl_init('http://www.fanpop.com');
curl_setopt($ch,CURLOPT_ENCODING , "");
curl_exec($ch);

您也可以針對file_get_contents()進行此工作,但是要付出很大的努力,如本文所述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM