这是我的代码:
function get_data($url)
{
$ch = curl_init();
$timeout = 15;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
//Grab HTML
$urllist = fopen("links.txt", "r+");
for ($j = 0; $j <= 50; $j++)
{
$post = rtrim(fgets($urllist));
echo $post;
$html = get_data($post);
echo $html;
问题:当我使用get_data("http://url.com")
时,我在html中获得了正确的数据。但是当我使用变量传递url时,$html
不返回任何内容。
$ post在检查时保留了正确的网址。这不是使用get_data($post);
卷曲信息给出:
我明白了:
array(20) {
["url"]=> string(68) "http://secret-url.com"
["content_type"]=> string(9) "text/html"
["http_code"]=> int(301)
["header_size"]=> int(255)
["request_size"]=> int(340)
["filetime"]=> int(-1)
["ssl_verify_result"]=> int(0)
["redirect_count"]=> int(0)
["total_time"]=> float(0.095589)
["namelookup_time"]=> float(0.012224)
["connect_time"]=> float(0.049399)
["pretransfer_time"]=> float(6.5E-5)
["size_upload"]=> float(0)
["size_download"]=> float(0)
["speed_download"]=> float(0)
["speed_upload"]=> float(0)
["download_content_length"]=> float(0)
["upload_content_length"]=> float(0)
["starttransfer_time"]=> float(0.095534)
["redirect_time"]=> float(0)
}
答案 0 :(得分:2)
尝试使用此代码。
function get_data($url)
{
$ch = curl_init();
$timeout = 15;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
// Edit: Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
var_dump(curl_getinfo($ch));
curl_close($ch);
return $data;
}
//Grab HTML
$urllist = fopen("links.txt", "r+");
for ($j = 0; $j <= 50; $j++)
{
if($post = rtrim(fgets($urllist)))
{
echo $post;
echo get_data($post);
}
else
{
echo "No URL provided!";
}
echo "\n<hr>\n";
}
答案 1 :(得分:0)
$html = file_get_contents($url);
会满足吗?正如记录所示,它没有=)
编辑以总结与合法答案的对话;
将你的curl改为以下,包含FOLLOWLOCATION指令,可选择用MAXREDIRS约束curl
function get_data($url) {
$ch = curl_init();
$timeout = 15;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// follow Location: newurl.tld - i.e. HTTP 30X status codes
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// give up follow location if circular after 5 tries
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
$data = curl_exec($ch);
// if in doubt, whats going on, look through info of curl_getinfo($ch);
// var_dump(curl_getinfo($ch));
curl_close($ch);
return $data;
}
//Grab HTML
$urllist = fopen("links.txt", "r+");
for ($j = 0; $j <= 50; $j++) {
$post = rtrim(fgets($urllist));
echo $post;
$html = get_data($post);
echo $html;
}
可选地,因为你好像这样做了一次,回到你的links.txt页面 - 设置一个cookie容器,允许访问知道你以前去过那里 - 并在连续运行时重用这些信息
// filehandle, writeable by httpd user:
$cookie_file = "/tmp/cookie/cookie1.txt";
// set request to parse cookies and send them with corresponding host requests
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
// set response cookies to be saved
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);