这是我的代码:
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在我检查时持有正确的 URL。这不是使用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)
}
试试这段代码。
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";
}
$html = file_get_contents($url);
就足够了吗?正如记录所示,它没有=)
编辑以用合法答案总结对话;
将卷曲更改为以下内容,包含 FOLLOWLOCATION 指令,可选择使用 MAXREDIRS 约束卷曲
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;
}
或者,由于您似乎不止一次地这样做,因此返回到您的链接.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);