当我试图通过在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);
...但是上面也没有用,任何一个人都可以告诉我有什么变化吗?
答案 0 :(得分:2)
这个特定网站的问题是它只提供压缩内容,否则会抛出404错误。
轻松修复:
$ch = curl_init('http://www.fanpop.com');
curl_setopt($ch,CURLOPT_ENCODING , "");
curl_exec($ch);
您也可以file_get_contents()
为public class LocalHttpServer extends NanoHTTPD {
public static final int SERVER_PORT = 5987;
private String mUrl;
private InputStream input;
private FileOutputStream output;
public LocalHttpServer(String url) {
super(SERVER_PORT);
mUrl = url;
}
private File createFile(String url) {
File path = new File(MyApplication.getContext().getFilesDir(), "audio/");
path.mkdirs();
return new File(path, Util.md5(url));
}
@Override
public Response serve(IHTTPSession session) {
input = null;
output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(mUrl);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return new Response(Response.Status.BAD_REQUEST, "audio/mpeg3", null, 0);
}
int fileLength = connection.getContentLength();
input = connection.getInputStream();
output = new FileOutputStream(createFile(mUrl));
new Thread(new Runnable() {
@Override
public void run() {
byte data[] = new byte[4096];
int count;
try {
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
if (input != null)
//input.close(); don't close it
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
return new Response(Response.Status.OK, "audio/mpeg3", input, fileLength);
} catch (IOException e) {
e.printStackTrace();
}
return new Response(Response.Status.BAD_REQUEST, "audio/mpeg3", null, 0);
}
}
工作,但需要花费大量精力,如this article中所述。