如何使用PHP来获得一个网页到一个变量 [英] How to use PHP to get a webpage into a variable
问题描述
我想从网上下载一个网页,它是允许的,当你使用的是简单的浏览器如Firefox做的,但是当我用的file_get_contents服务器拒绝,并回复它理解命令,但不允许出现这样下载。
I want to download a page from the web, it's allowed to do when you are using a simple browser like Firefox, but when I use "file_get_contents" the server refuses and replies that it understands the command but don't allow such downloads.
那么怎么办?我想我在一些脚本(Perl的)的方式看到了通过创建一个用户代理和饼干,这使得服务器认为你的脚本是一个真正的Web浏览器,使你的脚本像一个真正的浏览器。
So what to do? I think I saw in some scripts (on Perl) a way to make your script like a real browser by creating a user agent and cookies, which makes the servers think that your script is a real web browser.
有没有人有这样的想法,它可以怎么做?
Does anyone have an idea about this, how it can be done?
推荐答案
使用卷曲。
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// set the UA
curl_setopt($ch, CURLOPT_USERAGENT, 'My App (http://www.example.com/)');
// Alternatively, lie, and pretend to be a browser
// curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>
(从 http://uk.php.net/manual/ EN / curl.examples-basic.php )
这篇关于如何使用PHP来获得一个网页到一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!